function normalizeFilter(filter) {
    const value = filter?.trim().toLowerCase();
    return value && value.length > 0 ? value : null;
}
export function createHookApi(options) {
    return {
        contracts(filter) {
            const query = normalizeFilter(filter);
            const contracts = options.listContracts();
            if (!query) {
                return contracts;
            }
            return contracts.filter((contract) => (contract.pattern.toLowerCase().includes(query)
                || contract.description.toLowerCase().includes(query)
                || contract.authority.toLowerCase().includes(query)
                || contract.executionMode.toLowerCase().includes(query)));
        },
        dispatcher: {
            listHooks() {
                return options.dispatcher.listHooks();
            },
            listChains() {
                return options.dispatcher.listChains();
            },
        },
        workbench: {
            getFilePath() {
                return options.workbench.getHooksFilePath();
            },
            listManagedHooks() {
                return options.workbench.listManagedHooks();
            },
            listManagedChains() {
                return options.workbench.listManagedChains();
            },
            listRecentActions(limit = 8) {
                return options.workbench.listRecentActions(limit);
            },
            load(path) {
                options.workbench.loadManagedConfig(path);
            },
            reload(path) {
                return options.workbench.loadAndApplyManagedHooks(path);
            },
            async scaffoldHook(name, match, type) {
                options.workbench.loadManagedConfig();
                const hook = options.workbench.scaffoldHook(name, match, type);
                await options.workbench.saveManagedConfig();
                await options.workbench.loadAndApplyManagedHooks();
                return hook;
            },
            async scaffoldChain(name, matches) {
                options.workbench.loadManagedConfig();
                const chain = options.workbench.scaffoldChain(name, matches);
                await options.workbench.saveManagedConfig();
                await options.workbench.loadAndApplyManagedHooks();
                return chain;
            },
            async remove(name) {
                options.workbench.loadManagedConfig();
                const removed = options.workbench.removeManagedEntry(name);
                if (!removed) {
                    return false;
                }
                await options.workbench.saveManagedConfig();
                await options.workbench.loadAndApplyManagedHooks();
                return true;
            },
            async toggle(name, enabled) {
                options.workbench.loadManagedConfig();
                const changed = options.workbench.toggleManagedHook(name, enabled);
                if (!changed) {
                    return false;
                }
                await options.workbench.saveManagedConfig();
                await options.workbench.loadAndApplyManagedHooks();
                return true;
            },
            simulate(eventPath, payload = {}) {
                options.workbench.loadManagedConfig();
                return options.workbench.simulate(eventPath, payload);
            },
            inspect(path) {
                return options.workbench.inspectManagedConfig(path);
            },
            export(path) {
                options.workbench.loadManagedConfig();
                return options.workbench.exportManagedConfig(path);
            },
            async import(path, strategy = 'merge') {
                options.workbench.loadManagedConfig();
                options.workbench.importManagedConfig(path, strategy);
                await options.workbench.saveManagedConfig();
                await options.workbench.loadAndApplyManagedHooks();
            },
        },
    };
}
