/*!
 * KoliBri - The accessible HTML-Standard
 */
import { f as KolFormTag } from './component-names-Dy77vq43.js';
import { J as getExperimentalMode, j as devHint, M as setEventTarget, a as KoliBriDevHelper, L as Log } from './common-DEEjER7G.js';

const searchFormElement = (el) => {
    if (getExperimentalMode()) {
        devHint(`↓ Search form element start.`);
        Log.debug(el);
    }
    while (el instanceof HTMLElement && el.tagName !== 'FORM' && el.tagName !== KolFormTag.toUpperCase()) {
        try {
            if (el.parentElement instanceof HTMLElement) {
                el = el.parentElement;
            }
            else if (el.parentNode instanceof ShadowRoot) {
                el = el.parentNode.host;
            }
            else {
                el = null;
            }
        }
        catch (_a) {
        }
        if (getExperimentalMode()) {
            Log.debug(el);
        }
    }
    if (getExperimentalMode()) {
        devHint(`↑ Search form element finished.`);
    }
    return el;
};
const propagateResetEventToForm = (options = {}) => {
    var _a, _b;
    const form = searchFormElement(options.form);
    if (form instanceof HTMLElement) {
        const event = new Event('reset', {
            bubbles: true,
            cancelable: true,
        });
        if (form.tagName === 'FORM') {
            setEventTarget(event, form);
            form.dispatchEvent(event);
        }
        else if (form.tagName === KolFormTag.toUpperCase()) {
            setEventTarget(event, KoliBriDevHelper.querySelector('form', form));
            const kolForm = form;
            if (typeof ((_a = kolForm._on) === null || _a === void 0 ? void 0 : _a.onReset) === 'function') {
                (_b = kolForm._on) === null || _b === void 0 ? void 0 : _b.onReset(event);
            }
        }
    }
};
const propagateSubmitEventToForm = (options = {}) => {
    const form = searchFormElement(options.form);
    if (form instanceof HTMLElement) {
        const event = new SubmitEvent('submit', {
            bubbles: true,
            cancelable: true,
            submitter: form,
        });
        if (form.tagName === 'FORM') {
            if (getExperimentalMode() && form.noValidate === false) {
                devHint(`If you have not focusable or hidden form fields in your form, you should enable noValidate for your form.`, {
                    force: true,
                });
            }
            setTimeout(() => {
                if (typeof form.requestSubmit === 'function') {
                    form.requestSubmit();
                }
                else {
                    setEventTarget(event, form);
                    form.dispatchEvent(event);
                }
            });
        }
        else if (form.tagName === KolFormTag.toUpperCase()) {
            setEventTarget(event, KoliBriDevHelper.querySelector('form', form));
            const kolForm = form;
            setTimeout(() => {
                var _a, _b;
                if (typeof ((_a = kolForm._on) === null || _a === void 0 ? void 0 : _a.onSubmit) === 'function') {
                    (_b = kolForm._on) === null || _b === void 0 ? void 0 : _b.onSubmit(event);
                }
            });
        }
    }
};

export { propagateResetEventToForm as a, propagateSubmitEventToForm as p };
//# sourceMappingURL=controller-D0y9uwX0.js.map

//# sourceMappingURL=controller-D0y9uwX0.js.map