import { access, mkdir, writeFile } from "node:fs/promises";
import { basename, join } from "node:path";
export class TenantRuntime {
    options;
    constructor(options) {
        this.options = options;
    }
    projectRootFor(userId) {
        return join(this.options.dataRoot, "users", userId, "project");
    }
    async resolveContext(user) {
        const projectRoot = this.projectRootFor(user.id);
        await ensureTenantProjectInitialized(projectRoot);
        return {
            user,
            projectRoot,
            isAdmin: user.role === "admin",
        };
    }
}
async function exists(path) {
    try {
        await access(path);
        return true;
    }
    catch {
        return false;
    }
}
export async function ensureTenantProjectInitialized(projectRoot) {
    await mkdir(projectRoot, { recursive: true });
    await Promise.all([
        mkdir(join(projectRoot, "books"), { recursive: true }),
        mkdir(join(projectRoot, "radar"), { recursive: true }),
        mkdir(join(projectRoot, ".inkos", "sessions"), { recursive: true }),
    ]);
    const configPath = join(projectRoot, "inkos.json");
    if (!await exists(configPath)) {
        await writeFile(configPath, JSON.stringify({
            name: basename(projectRoot),
            version: "0.1.0",
            language: "zh",
            notify: [],
            inputGovernanceMode: "v2",
            daemon: {
                schedule: {
                    radarCron: "0 */6 * * *",
                    writeCron: "*/15 * * * *",
                },
                maxConcurrentBooks: 3,
            },
        }, null, 2), "utf-8");
    }
}
//# sourceMappingURL=tenant.js.map