import { capitalize, isString } from "../../../lodash.js";
import { globalLogger } from "../../../logger.js";
export class MainHandler {
    static log(logger, opts) {
        const prefix = '[Autocomplete]';
        if (opts.event === 'destroy') {
            logger.log(`${prefix} Destroy`);
            globalLogger.log(`${prefix} Destroy`);
            return;
        }
        const { action, item } = opts;
        const msg = [
            prefix,
            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.log(msg);
        globalLogger.log(msg);
    }
    config;
    logger;
    constructor(config, logger) {
        this.config = config;
        this.logger = logger.nested({
            module: 'autocomplete-main-handler',
        });
    }
    onOpen(action) {
        const item = this.getItem(action);
        MainHandler.log(this.logger, { event: 'open', action, item });
        return item?.handler.onOpen?.(action) ?? false;
    }
    onClose(action) {
        const item = this.getItem(action);
        MainHandler.log(this.logger, { event: 'close', action, item });
        return item?.handler.onClose?.(action) ?? false;
    }
    onArrow(action) {
        const item = this.getItem(action);
        MainHandler.log(this.logger, { event: 'arrow', action, item });
        return item?.handler.onArrow?.(action) ?? false;
    }
    onEnter(action) {
        const item = this.getItem(action);
        MainHandler.log(this.logger, { event: 'enter', action, item });
        return item?.handler.onEnter?.(action) ?? false;
    }
    onFilter(action) {
        const item = this.getItem(action);
        MainHandler.log(this.logger, { event: 'filter', action, item });
        return item?.handler.onFilter?.(action) ?? false;
    }
    onDestroy() {
        MainHandler.log(this.logger, { 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 (isString(itemTrigger))
                    return itemTrigger === trigger;
                return itemTrigger.test(trigger);
            };
        }
        return this.config.find(predicate);
    }
}
//# sourceMappingURL=handler.js.map
