"use strict";
/**
 * File Watcher (Phase 04)
 *
 * Watches ~/.ccs/ directory for config file changes using chokidar.
 * Broadcasts changes to WebSocket clients for real-time sync.
 */
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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFileWatcher = void 0;
const chokidar_1 = __importDefault(require("chokidar"));
const path = __importStar(require("path"));
const config_loader_facade_1 = require("../config/config-loader-facade");
function createFileWatcher(onChange) {
    const ccsDir = (0, config_loader_facade_1.getCcsDir)();
    const watcher = chokidar_1.default.watch([
        path.join(ccsDir, 'config.json'), // Legacy config
        path.join(ccsDir, 'config.yaml'), // Unified config
        path.join(ccsDir, '*.settings.json'),
        path.join(ccsDir, 'profiles.json'),
        path.join(ccsDir, 'cliproxy', 'sessions.json'), // Proxy session tracking
    ], {
        persistent: true,
        ignoreInitial: true,
        awaitWriteFinish: {
            stabilityThreshold: 100,
            pollInterval: 50,
        },
    });
    watcher.on('change', (filePath) => {
        const basename = path.basename(filePath);
        let type;
        if (basename === 'config.json' || basename === 'config.yaml') {
            type = 'config-changed';
        }
        else if (basename === 'profiles.json') {
            type = 'profiles-changed';
        }
        else if (basename === 'sessions.json') {
            type = 'proxy-status-changed';
        }
        else {
            type = 'settings-changed';
        }
        onChange({
            type,
            path: filePath,
            timestamp: Date.now(),
        });
    });
    watcher.on('add', (filePath) => {
        onChange({
            type: 'config-changed',
            path: filePath,
            timestamp: Date.now(),
        });
    });
    watcher.on('unlink', (filePath) => {
        onChange({
            type: 'config-changed',
            path: filePath,
            timestamp: Date.now(),
        });
    });
    return watcher;
}
exports.createFileWatcher = createFileWatcher;
//# sourceMappingURL=file-watcher.js.map