"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainHandler = void 0;
const lodash_1 = require("../../../lodash.js");
const logger_1 = require("../../../logger.js");
class MainHandler {
    static log(opts) {
        const prefix = '[Autocomplete]';
        if (opts.event === 'destroy') {
            logger_1.logger.log(`${prefix} Destroy`);
            return;
        }
        const { action, item } = opts;
        const msg = [
            prefix,
            (0, lodash_1.capitalize)(opts.event),
            `Trigger="${action.trigger}"`,
            `Filter=${action.filter !== undefined ? `"${action.filter}"` : 'None'}`,
            `Type=${action.type ? `"${action.type.name}"` : 'None'}`,
            `Handler=${item ? `"${item.trigger.name}"` : 'None'}`,
        ].join(' ');
        logger_1.logger.log(msg);
    }
    config;
    constructor(config) {
        this.config = config;
    }
    onOpen(action) {
        const item = this.getItem(action);
        MainHandler.log({ event: 'open', action, item });
        return item?.handler.onOpen?.(action) ?? false;
    }
    onClose(action) {
        const item = this.getItem(action);
        MainHandler.log({ event: 'close', action, item });
        return item?.handler.onClose?.(action) ?? false;
    }
    onArrow(action) {
        const item = this.getItem(action);
        MainHandler.log({ event: 'arrow', action, item });
        return item?.handler.onArrow?.(action) ?? false;
    }
    onEnter(action) {
        const item = this.getItem(action);
        MainHandler.log({ event: 'enter', action, item });
        return item?.handler.onEnter?.(action) ?? false;
    }
    onFilter(action) {
        const item = this.getItem(action);
        MainHandler.log({ event: 'filter', action, item });
        return item?.handler.onFilter?.(action) ?? false;
    }
    onDestroy() {
        MainHandler.log({ event: 'destroy' });
        for (const item of this.config) {
            item.handler.onDestroy?.();
        }
    }
    getItem({ trigger, type }) {
        let predicate;
        if (type) {
            predicate = (item) => item.trigger.name === type.name;
        }
        else {
            predicate = (item) => {
                const itemTrigger = item.trigger.trigger;
                if ((0, lodash_1.isString)(itemTrigger))
                    return itemTrigger === trigger;
                return itemTrigger.test(trigger);
            };
        }
        return this.config.find(predicate);
    }
}
exports.MainHandler = MainHandler;
//# sourceMappingURL=handler.js.map
