import { h } from './index-d0047af2.js';
import { v as validator, d as debounce } from './validator-d308772c.js';

const getInputCreator = (component, onerror) => {
    let error = '';
    const fc = (_, children) => {
        const props = getProps();
        return [
            props.label != undefined && (h("bce-label", { disabled: props.disabled, hasFocus: props.hasFocus, tooltip: props.tooltip, "data-hover": props.hover },
                props.label,
                h("slot", { name: "label" }))),
            children,
            (error || props.info) && h("small", null, error || props.info)
        ];
    };
    const getProps = () => ({
        el: component.el,
        disabled: component.disabled,
        hasFocus: component.hasFocus,
        hover: component.hover,
        label: component.label,
        tooltip: component.tooltip,
        validation: component.validation,
        info: component.info
    });
    const handleInput = () => {
        if (error)
            validate();
        else
            _debounceValidate();
    };
    const reset = () => updateError([]);
    const validate = async (silent = false) => {
        const props = getProps();
        if (!props.validation)
            return updateError([]);
        const label = props.label || '';
        const name = props.name || '';
        const meta = { label, name };
        const errors = await validator.validate(props.validation, props.el, meta);
        return updateError(errors, silent);
    };
    const updateError = (errors, silent = false) => {
        error = errors.length ? errors[0].message : '';
        if (!silent && onerror)
            onerror(error);
        return errors;
    };
    const _debounceValidate = debounce(validate, 1000);
    return Object.assign(fc, { getProps, handleInput, reset, validate });
};

export { getInputCreator as g };
