import fs from "node:fs";
import path from "node:path";
import { z } from "zod";
import { ensureConfigDir, getConfigDir } from "../config/index.js";
export const SetupStateSchema = z.object({
    gmail: z.string().optional(),
    gmailPassword: z.string().optional(),
    googleOAuthComplete: z.boolean().default(false),
    githubAuthComplete: z.boolean().default(false),
    githubUsername: z.string().optional(),
    tunnelName: z.string().optional(),
    tunnelHostname: z.string().optional(),
    ethAddress: z.string().optional(),
    ethPrivateKey: z.string().optional(),
    ethMnemonic: z.string().optional(),
    configSaved: z.boolean().default(false),
    botIdentityComplete: z.boolean().default(false),
    /** WeChat (Weixin) bot QR binding completed in Setup wizard */
    weixinBotComplete: z.boolean().default(false),
    /** User chose to skip optional WeChat setup step */
    weixinBotSkipped: z.boolean().default(false),
    /** ilink bot id after optional WeChat QR bind */
    weixinAccountId: z.string().optional(),
});
function getSetupStateFile() {
    return path.join(getConfigDir(), "setup-state.json");
}
export function loadSetupState() {
    const file = getSetupStateFile();
    if (!fs.existsSync(file)) {
        return SetupStateSchema.parse({});
    }
    try {
        const raw = JSON.parse(fs.readFileSync(file, "utf-8"));
        return SetupStateSchema.parse(raw);
    }
    catch {
        return SetupStateSchema.parse({});
    }
}
export function saveSetupState(state) {
    ensureConfigDir();
    fs.writeFileSync(getSetupStateFile(), JSON.stringify(state, null, 2), "utf-8");
}
export function updateSetupState(patch) {
    const nextState = SetupStateSchema.parse({
        ...loadSetupState(),
        ...patch,
    });
    saveSetupState(nextState);
    return nextState;
}
export function deleteSetupState() {
    const file = getSetupStateFile();
    if (fs.existsSync(file)) {
        fs.unlinkSync(file);
    }
}
//# sourceMappingURL=setup-state.js.map