export const permissionProfileNames = ["safe", "dev", "browser", "open"];
const profiles = {
    safe: {
        name: "safe",
        description: "Read, edit, and validate project files while keeping shell and browser control off.",
        allowShell: false,
        allowBrowser: false,
    },
    dev: {
        name: "dev",
        description: "Enable local shell commands for builds and tests; browser control stays off.",
        allowShell: true,
        allowBrowser: false,
    },
    browser: {
        name: "browser",
        description: "Enable browser/CDP actions for UI checks; shell execution stays off.",
        allowShell: false,
        allowBrowser: true,
    },
    open: {
        name: "open",
        description: "Enable shell and browser actions for trusted local development sessions.",
        allowShell: true,
        allowBrowser: true,
    },
};
const modeAliases = {
    default: "safe",
    plan: "safe",
    acceptEdits: "dev",
    acceptedits: "dev",
    dontAsk: "open",
    dontask: "open",
    bypassPermissions: "open",
    bypasspermissions: "open",
};
export function listPermissionProfiles() {
    return permissionProfileNames.map((name) => profiles[name]);
}
export function getPermissionProfile(name) {
    const normalized = normalizeProfileName(name);
    return normalized ? profiles[normalized] : null;
}
export function normalizeProfileName(name) {
    const trimmed = name.trim();
    if (!trimmed)
        return null;
    if (isPermissionProfileName(trimmed))
        return trimmed;
    const lower = trimmed.toLowerCase();
    const alias = modeAliases[trimmed] ?? modeAliases[lower];
    return alias ?? null;
}
export function applyPermissionProfile(state, name) {
    const profile = getPermissionProfile(name);
    if (!profile) {
        throw new Error(`Unknown permission profile: ${name}`);
    }
    state.allowShell = profile.allowShell;
    state.allowBrowser = profile.allowBrowser;
    state.profile = profile.name;
    return state;
}
export function markCustomPermissions(state) {
    state.profile = "custom";
    return state;
}
export function describeRuntimePermissions(state) {
    return [
        `profile: ${state.profile ?? inferProfile(state)}`,
        `shell: ${state.allowShell ? "on" : "off"}`,
        `browser: ${state.allowBrowser ? "on" : "off"}`,
    ].join("\n");
}
export function inferProfile(state) {
    const match = listPermissionProfiles().find((profile) => profile.allowShell === state.allowShell &&
        profile.allowBrowser === state.allowBrowser);
    return match?.name ?? "custom";
}
function isPermissionProfileName(value) {
    return permissionProfileNames.includes(value);
}
