"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadRepoRegistry = loadRepoRegistry;
exports.writeRepoRegistry = writeRepoRegistry;
exports.registerRepo = registerRepo;
exports.detachRepo = detachRepo;
exports.createEmptyRepoRegistry = createEmptyRepoRegistry;
const promises_1 = require("node:fs/promises");
const node_path_1 = __importDefault(require("node:path"));
const zod_1 = require("zod");
const catalog_1 = require("../i18n/catalog");
const file_readers_1 = require("./file-readers");
const paths_1 = require("./paths");
const REPO_REGISTRY_VERSION = 3;
const repoRegistryEntrySchema = zod_1.z
    .object({
    cwd: zod_1.z.string().min(1),
    enabled: zod_1.z.boolean(),
    addedAt: zod_1.z.string().min(1),
})
    .strict();
const repoRegistrySchema = zod_1.z
    .object({
    version: zod_1.z.literal(REPO_REGISTRY_VERSION),
    repos: zod_1.z.array(repoRegistryEntrySchema),
})
    .strict();
async function loadRepoRegistry(homeDirectory, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const registryPath = (0, paths_1.getRepoRegistryPath)(homeDirectory);
    const text = await (0, file_readers_1.readTextFileIfExists)(registryPath);
    if (text === null) {
        return createEmptyRepoRegistry();
    }
    const parsed = JSON.parse(text);
    try {
        return repoRegistrySchema.parse(parsed);
    }
    catch {
        throw new Error((0, catalog_1.translate)(locale, "repoRegistry.readFailed", { registryPath }));
    }
}
async function writeRepoRegistry(registry, homeDirectory) {
    const registryPath = (0, paths_1.getRepoRegistryPath)(homeDirectory);
    const normalized = repoRegistrySchema.parse(registry);
    await (0, promises_1.mkdir)(node_path_1.default.dirname(registryPath), { recursive: true });
    await (0, promises_1.writeFile)(registryPath, `${JSON.stringify(normalized, null, 2)}\n`, "utf8");
    return registryPath;
}
async function registerRepo(input, homeDirectory) {
    const registry = await loadRepoRegistry(homeDirectory);
    const normalizedCwd = node_path_1.default.resolve(input.cwd);
    const existing = registry.repos.find((entry) => node_path_1.default.resolve(entry.cwd) === normalizedCwd);
    if (existing) {
        return { kind: "noop", entry: existing };
    }
    const entry = {
        cwd: normalizedCwd,
        enabled: input.enabled ?? true,
        addedAt: input.addedAt ?? new Date().toISOString(),
    };
    await writeRepoRegistry({
        version: REPO_REGISTRY_VERSION,
        repos: [...registry.repos, entry],
    }, homeDirectory);
    return { kind: "added", entry };
}
async function detachRepo(input, homeDirectory) {
    const registry = await loadRepoRegistry(homeDirectory);
    const normalizedCwd = node_path_1.default.resolve(input.cwd);
    const existing = registry.repos.find((entry) => node_path_1.default.resolve(entry.cwd) === normalizedCwd);
    if (!existing) {
        return { kind: "missing" };
    }
    await writeRepoRegistry({
        version: registry.version,
        repos: registry.repos.filter((entry) => node_path_1.default.resolve(entry.cwd) !== normalizedCwd),
    }, homeDirectory);
    return { kind: "removed", entry: existing };
}
function createEmptyRepoRegistry() {
    return {
        version: REPO_REGISTRY_VERSION,
        repos: [],
    };
}
