import { readFileSync, writeFileSync, renameSync, unlinkSync, existsSync, mkdirSync } from 'node:fs';
import { dirname } from 'node:path';
import { setupStateGuard } from '../../../../modules/setup-wizard/entities/setupState/setupState.guard.js';
export class SetupStateFileSystemGateway {
    deps;
    constructor(deps) {
        this.deps = deps;
    }
    load() {
        if (!existsSync(this.deps.filePath)) {
            return { state: null, corrupted: false };
        }
        let raw;
        try {
            raw = readFileSync(this.deps.filePath, 'utf-8');
        }
        catch {
            return { state: null, corrupted: true };
        }
        let parsed;
        try {
            parsed = JSON.parse(raw);
        }
        catch {
            return { state: null, corrupted: true };
        }
        const result = setupStateGuard.safeParse(parsed);
        if (!result.success) {
            return { state: null, corrupted: true };
        }
        return { state: result.data, corrupted: false };
    }
    save(state) {
        const dir = dirname(this.deps.filePath);
        if (!existsSync(dir)) {
            mkdirSync(dir, { recursive: true });
        }
        const tempPath = `${this.deps.filePath}.tmp`;
        writeFileSync(tempPath, JSON.stringify(state, null, 2), 'utf-8');
        renameSync(tempPath, this.deps.filePath);
    }
    reset() {
        if (existsSync(this.deps.filePath)) {
            unlinkSync(this.deps.filePath);
        }
    }
}
//# sourceMappingURL=setupState.fileSystem.gateway.js.map