"use strict";
// backend/src/backup/config.ts
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBackupConfig = getBackupConfig;
exports.saveBackupConfig = saveBackupConfig;
exports.getBackupHistory = getBackupHistory;
exports.addBackupHistory = addBackupHistory;
exports.getBuiltInOAuth = getBuiltInOAuth;
exports.setBuiltInOAuth = setBuiltInOAuth;
exports.getBackupState = getBackupState;
exports.saveBackupState = saveBackupState;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const DATA_DIR = process.env.CCWEB_DATA_DIR || path.join(__dirname, '../../../data');
const BACKUP_CONFIG_FILE = path.join(DATA_DIR, 'backup-config.json');
const BACKUP_HISTORY_FILE = path.join(DATA_DIR, 'backup-history.json');
function atomicWriteSync(filePath, data) {
    const tmpPath = filePath + `.tmp.${process.pid}`;
    fs.writeFileSync(tmpPath, data, 'utf-8');
    fs.renameSync(tmpPath, filePath);
}
const DEFAULT_CONFIG = {
    providers: [],
    schedule: { enabled: false, intervalMinutes: 60 },
    excludePatterns: ['node_modules', '.git', 'dist', 'build', '*.log', '.DS_Store', '*.tmp', '.venv', '__pycache__', '.env'],
};
function getBackupConfig() {
    if (!fs.existsSync(BACKUP_CONFIG_FILE)) {
        return {
            ...DEFAULT_CONFIG,
            providers: [],
            schedule: { ...DEFAULT_CONFIG.schedule },
            excludePatterns: [...DEFAULT_CONFIG.excludePatterns],
        };
    }
    try {
        return JSON.parse(fs.readFileSync(BACKUP_CONFIG_FILE, 'utf-8'));
    }
    catch {
        return {
            ...DEFAULT_CONFIG,
            providers: [],
            schedule: { ...DEFAULT_CONFIG.schedule },
            excludePatterns: [...DEFAULT_CONFIG.excludePatterns],
        };
    }
}
function saveBackupConfig(config) {
    atomicWriteSync(BACKUP_CONFIG_FILE, JSON.stringify(config, null, 2));
}
function getBackupHistory() {
    if (!fs.existsSync(BACKUP_HISTORY_FILE))
        return [];
    try {
        return JSON.parse(fs.readFileSync(BACKUP_HISTORY_FILE, 'utf-8'));
    }
    catch {
        return [];
    }
}
function addBackupHistory(entry) {
    const history = getBackupHistory();
    history.unshift(entry);
    if (history.length > 100)
        history.length = 100;
    atomicWriteSync(BACKUP_HISTORY_FILE, JSON.stringify(history, null, 2));
}
function getBuiltInOAuth(type) {
    const config = getBackupConfig();
    return config.builtInOAuth?.[type] ?? null;
}
function setBuiltInOAuth(type, oauth) {
    const config = getBackupConfig();
    if (!config.builtInOAuth)
        config.builtInOAuth = {};
    config.builtInOAuth[type] = oauth;
    saveBackupConfig(config);
}
function getBackupState(projectFolderPath) {
    const file = path.join(projectFolderPath, '.ccweb', 'backup-state.json');
    if (!fs.existsSync(file))
        return { lastBackupTime: null, files: {} };
    try {
        return JSON.parse(fs.readFileSync(file, 'utf-8'));
    }
    catch {
        return { lastBackupTime: null, files: {} };
    }
}
function saveBackupState(projectFolderPath, state) {
    const dir = path.join(projectFolderPath, '.ccweb');
    fs.mkdirSync(dir, { recursive: true });
    atomicWriteSync(path.join(dir, 'backup-state.json'), JSON.stringify(state, null, 2));
}
//# sourceMappingURL=config.js.map