"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const np = __importStar(require("path"));
function watchDirectory(dirname, options, listener) {
    let { persistent = true, interval = 50, recursive = true, include, exclude = { node_modules: true } } = options;
    let initial = options.initial ? "initial" : null;
    function matches(name, filter, defaultValue) {
        if (filter == null) {
            return defaultValue;
        }
        if (Array.isArray(filter)) {
            return filter.indexOf(name) >= 0;
        }
        if (typeof filter === "string") {
            let ext = filter;
            return name.indexOf(ext, name.length - ext.length) >= 0;
        }
        if (typeof filter.test === "function") {
            return filter.test(name);
        }
        if (typeof filter === "function") {
            return filter(name);
        }
        return filter[name] === true;
    }
    function filter(name) {
        if (matches(name, exclude, false)) {
            return false;
        }
        return matches(name, include, true);
    }
    let watchedFiles = {};
    function notifyListener(filename, current, previous, change) {
        if (filter(filename)) {
            listener(filename, current, previous, change);
        }
    }
    function unwatchFile(filename) {
        fs.unwatchFile(filename, watchedFiles[filename]);
        delete watchedFiles[filename];
    }
    function watchFile(filename, depth = 0, stats = fs.statSync(filename)) {
        if (stats.nlink > 0) {
            if (stats.isDirectory()) {
                if (!matches(filename, exclude, false)) {
                    if (depth === 0 || recursive) {
                        for (let child of fs.readdirSync(filename)) {
                            watchFile(np.join(filename, child), depth + 1);
                        }
                    }
                }
            }
            if (watchedFiles[filename] == null) {
                let boundListener = fsListener.bind(null, filename, depth);
                watchedFiles[filename] = boundListener;
                if (persistent) {
                    fs.watchFile(filename, { persistent, interval }, boundListener);
                }
                if (initial != null) {
                    notifyListener(filename, stats, stats, initial);
                }
            }
        }
    }
    let fsListener = (filename, depth, current, previous) => {
        let change = "modified";
        if (current.nlink === 0) {
            change = "deleted";
        }
        if (previous.nlink === 0) {
            change = "created";
        }
        notifyListener(filename, current, previous, change);
        if (change !== "deleted") {
            watchFile(filename, depth, current);
        }
        else {
            unwatchFile(filename);
        }
    };
    watchFile(dirname);
    initial = "created";
    // returns a function which unwatches everything
    return () => {
        for (let filename in watchedFiles) {
            unwatchFile(filename);
        }
    };
}
exports.default = watchDirectory;
