"use strict";
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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileWatcher = void 0;
const chokidar_1 = __importDefault(require("chokidar"));
const events_1 = require("events");
const path = __importStar(require("path"));
class FileWatcher extends events_1.EventEmitter {
    constructor() {
        super();
        this.watcher = null;
        this.watchPath = '';
        this.ignorePatterns = [
            '**/node_modules/**',
            '**/.git/**',
            '**/dist/**',
            '**/build/**',
            '**/.next/**',
            '**/coverage/**',
            '**/.cache/**',
            '**/tmp/**',
            '**/.DS_Store'
        ];
    }
    start(watchPath, customIgnorePatterns) {
        if (this.watcher) {
            this.stop();
        }
        this.watchPath = path.resolve(watchPath);
        if (customIgnorePatterns) {
            this.ignorePatterns = [...this.ignorePatterns, ...customIgnorePatterns];
        }
        console.log(`Starting file watcher on: ${this.watchPath}`);
        console.log(`Ignoring patterns:`, this.ignorePatterns);
        this.watcher = chokidar_1.default.watch(this.watchPath, {
            ignored: this.ignorePatterns,
            persistent: true,
            ignoreInitial: false,
            awaitWriteFinish: {
                stabilityThreshold: 300,
                pollInterval: 100
            },
            depth: 99
        });
        this.watcher
            .on('add', (filePath, stats) => {
            this.emitChange('add', filePath, stats);
        })
            .on('change', (filePath, stats) => {
            this.emitChange('change', filePath, stats);
        })
            .on('unlink', (filePath) => {
            this.emitChange('unlink', filePath);
        })
            .on('error', (error) => {
            console.error('Watcher error:', error);
            this.emit('error', error);
        })
            .on('ready', () => {
            console.log('Initial scan complete. Ready for changes.');
            this.emit('ready');
        });
    }
    emitChange(type, filePath, stats) {
        const event = {
            type,
            path: filePath,
            stats: stats ? {
                size: stats.size,
                mtime: stats.mtime
            } : undefined
        };
        this.emit('fileChange', event);
    }
    stop() {
        if (this.watcher) {
            console.log('Stopping file watcher');
            this.watcher.close();
            this.watcher = null;
        }
    }
    getWatchPath() {
        return this.watchPath;
    }
    isWatching() {
        return this.watcher !== null;
    }
}
exports.FileWatcher = FileWatcher;
//# sourceMappingURL=FileWatcher.js.map