// License: MPL-2.0
// (c) 2022 Julian Hurst <ark@mansus.space>

use encoding::utf8;

type keycode = (specialkey, []u8);
//const BACKSPACECODE: keycode = (specialkey::BACKSPACE, [127]);
//const RIGHTCODE: keycode = (specialkey::RIGHT, [27, 91, 67]);
const keycodes: []keycode = [
	(specialkey::BACKSPACE, [127]),
	(specialkey::RIGHT, [27, 91, 67]),
];

export type specialkey = enum {
	BACKSPACE,
	RIGHT,
};

fn iskey(r: rune, key: keycode) bool = {
	const u = utf8::encoderune(r);
	for (let i = 0z; i < len(u); i += 1) {
		if (u[i] != key.1[i]) {
			return false;
		};
	};
	return true;
};

export fn getkey(r: rune) (specialkey | rune) = {
	const u = utf8::encoderune(r);
	for (let i = 0z; i < len(keycodes); i += 1) {
		let b = true;
		for (let j = 0z; j < len(u) && b; j += 1) {
			if (len(keycodes[i].1) != len(u) || keycodes[i].1[j] != u[j]) {
				b = false;
			};
		};
		if (b) {
			return keycodes[i].0;
		};
	};
	return r;
};
