repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/lazy/src/modes.ts

Raw
File outline
export const LAZY_MODES = ["off", "lite", "full", "ultra"] as const;
export type LazyMode = (typeof LAZY_MODES)[number];
export type ActiveLazyMode = Exclude<LazyMode, "off">;

export type LazyCommand =
	| { kind: "set"; mode: LazyMode }
	| { kind: "status" }
	| { kind: "default"; mode: LazyMode }
	| { kind: "help" }
	| { kind: "invalid"; message: string };

export function isLazyMode(value: unknown): value is LazyMode {
	return typeof value === "string" && LAZY_MODES.includes(value as LazyMode);
}

export function normalizeMode(value: unknown): LazyMode | undefined {
	if (typeof value !== "string") return undefined;
	const mode = value.trim().toLowerCase();
	return isLazyMode(mode) ? mode : undefined;
}

export function modeFromBareDefault(defaultMode: LazyMode): LazyMode {
	return defaultMode === "off" ? "full" : defaultMode;
}

export function parseLazyCommand(
	args: string,
	defaultMode: LazyMode,
): LazyCommand {
	const parts = args.trim().split(/\s+/).filter(Boolean);
	if (parts[0]?.toLowerCase().replace(/^\//u, "") === "lazy") parts.shift();
	const first = parts[0]?.toLowerCase();
	if (!first) return { kind: "set", mode: modeFromBareDefault(defaultMode) };
	if (first === "status") return { kind: "status" };
	if (first === "help" || first === "--help" || first === "-h") {
		return { kind: "help" };
	}
	if (first === "default") {
		const mode = normalizeMode(parts[1]);
		if (!mode || parts.length !== 2) {
			return {
				kind: "invalid",
				message: "usage: /lazy default off|lite|full|ultra",
			};
		}
		return { kind: "default", mode };
	}
	const mode = normalizeMode(first);
	if (mode && parts.length === 1) return { kind: "set", mode };
	return {
		kind: "invalid",
		message: "usage: /lazy [off|lite|full|ultra|status|default <mode>]",
	};
}

export function isStandaloneDeactivation(input: string): boolean {
	const normalized = input
		.trim()
		.toLowerCase()
		.replace(/[.!?。!?]+$/u, "")
		.trim();
	return normalized === "stop lazy" || normalized === "normal mode";
}