/*!
 * KoliBri - The accessible HTML-Standard
 */
import { delegateClick as delegateClickImpl, setClick } from "./element-click";
import { delegateFocus as delegateFocusImpl, setFocus } from "./element-focus";
export function createCtaRef() {
    const ref = ((el) => {
        ref.el = el;
    });
    ref.el = undefined;
    return ref;
}
function makeMethodDecorator(fn) {
    return (_target, _key, descriptor) => {
        descriptor.value = async function () {
            return fn(this);
        };
        return descriptor;
    };
}
function makeFocusDecorator(fn) {
    return (_target, _key, descriptor) => {
        descriptor.value = async function (options) {
            return fn(this, options);
        };
        return descriptor;
    };
}
export function directFocus(refPropName) {
    return makeFocusDecorator((self, options) => {
        var _a;
        const element = (_a = self[refPropName]) === null || _a === void 0 ? void 0 : _a.el;
        return element ? setFocus(element, options) : Promise.resolve();
    });
}
export function directClick(refPropName) {
    return makeMethodDecorator((self) => {
        const element = self[refPropName].el;
        return element ? setClick(element) : Promise.resolve();
    });
}
export function delegateFocus(refPropName) {
    return makeFocusDecorator((self, options) => {
        const host = self['host'];
        if (!host)
            return Promise.resolve();
        return delegateFocusImpl(host, () => {
            var _a;
            const element = (_a = self[refPropName]) === null || _a === void 0 ? void 0 : _a.el;
            return element ? setFocus(element, options) : Promise.resolve();
        });
    });
}
export function ctrlFocus(ctrlPropName) {
    return makeFocusDecorator(async (self, options) => {
        var _a;
        const ctrl = self[ctrlPropName];
        await ((_a = ctrl === null || ctrl === void 0 ? void 0 : ctrl.focus) === null || _a === void 0 ? void 0 : _a.call(ctrl, options));
    });
}
export function delegateClick(refPropName) {
    return makeMethodDecorator((self) => delegateClickImpl(self['host'], () => {
        const element = self[refPropName].el;
        return element ? setClick(element) : Promise.resolve();
    }));
}
//# sourceMappingURL=element-interaction.js.map
