export const DEFAULT_LAUNCH_PROFILE_ID = "default";
export function isCodexSandboxMode(value) {
    return value === "read-only" || value === "workspace-write" || value === "danger-full-access";
}
export function isCodexApprovalPolicy(value) {
    return value === "never" || value === "on-request" || value === "on-failure" || value === "untrusted";
}
export function createLaunchProfile(input) {
    return {
        ...input,
        unsafe: isUnsafeLaunchProfile(input.sandboxMode),
    };
}
export function createDefaultLaunchProfile(sandboxMode, approvalPolicy) {
    return createLaunchProfile({
        id: DEFAULT_LAUNCH_PROFILE_ID,
        label: "Default",
        sandboxMode,
        approvalPolicy,
    });
}
export function createBuiltinLaunchProfiles(defaultProfile, options) {
    const profiles = [
        defaultProfile,
        createLaunchProfile({
            id: "readonly",
            label: "Read Only",
            sandboxMode: "read-only",
            approvalPolicy: "never",
        }),
        createLaunchProfile({
            id: "review",
            label: "Review",
            sandboxMode: "workspace-write",
            approvalPolicy: "on-request",
        }),
    ];
    if (options?.includeFullAccess) {
        profiles.push(createLaunchProfile({
            id: "full-access",
            label: "Full Access",
            sandboxMode: "danger-full-access",
            approvalPolicy: "never",
        }));
    }
    return profiles;
}
export function parseLaunchProfilesJson(raw) {
    let parsed;
    try {
        parsed = JSON.parse(raw);
    }
    catch (error) {
        throw new Error(`Invalid CODEX_LAUNCH_PROFILES_JSON: ${error instanceof Error ? error.message : String(error)}`);
    }
    if (!Array.isArray(parsed)) {
        throw new Error("Invalid CODEX_LAUNCH_PROFILES_JSON: expected a JSON array");
    }
    return parsed.map((entry, index) => parseLaunchProfileEntry(entry, index));
}
export function findLaunchProfile(profiles, profileId) {
    if (!profileId) {
        return undefined;
    }
    return profiles.find((profile) => profile.id === profileId);
}
export function formatLaunchProfileBehavior(profile) {
    return `${profile.sandboxMode} / ${profile.approvalPolicy}`;
}
export function formatLaunchProfileLabel(profile, isCurrent = false) {
    const prefix = profile.unsafe ? "⚠️" : "🛡️";
    const selected = isCurrent ? " ✓" : "";
    return `${prefix} ${profile.label} · ${formatLaunchProfileBehavior(profile)}${selected}`;
}
export function isUnsafeLaunchProfile(profileOrSandboxMode) {
    const sandboxMode = typeof profileOrSandboxMode === "string" ? profileOrSandboxMode : profileOrSandboxMode.sandboxMode;
    return sandboxMode === "danger-full-access";
}
function parseLaunchProfileEntry(entry, index) {
    if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
        throw new Error(`Invalid CODEX_LAUNCH_PROFILES_JSON entry at index ${index}: expected an object`);
    }
    const rawId = readStringField(entry, "id", index);
    if (!/^[a-z0-9][a-z0-9_-]*$/.test(rawId)) {
        throw new Error(`Invalid CODEX_LAUNCH_PROFILES_JSON entry at index ${index}: id must match /^[a-z0-9][a-z0-9_-]*$/`);
    }
    const rawLabel = readStringField(entry, "label", index);
    const rawSandboxMode = readStringField(entry, "sandboxMode", index);
    if (!isCodexSandboxMode(rawSandboxMode)) {
        throw new Error(`Invalid CODEX_LAUNCH_PROFILES_JSON entry at index ${index}: unsupported sandboxMode "${rawSandboxMode}"`);
    }
    const rawApprovalPolicy = readStringField(entry, "approvalPolicy", index);
    if (!isCodexApprovalPolicy(rawApprovalPolicy)) {
        throw new Error(`Invalid CODEX_LAUNCH_PROFILES_JSON entry at index ${index}: unsupported approvalPolicy "${rawApprovalPolicy}"`);
    }
    return createLaunchProfile({
        id: rawId,
        label: rawLabel,
        sandboxMode: rawSandboxMode,
        approvalPolicy: rawApprovalPolicy,
    });
}
function readStringField(entry, field, index) {
    const value = Reflect.get(entry, field);
    if (typeof value !== "string" || value.trim().length === 0) {
        throw new Error(`Invalid CODEX_LAUNCH_PROFILES_JSON entry at index ${index}: missing ${field}`);
    }
    return value.trim();
}
