'use strict';

const index = require('./index-d719496c.js');
const validator = require('./validator-02b15c49.js');

const getInputCreator = (component, onerror) => {
    let error = '';
    const fc = (_, children) => {
        const props = getProps();
        return [
            props.label != undefined && (index.h("bce-label", { disabled: props.disabled, hasFocus: props.hasFocus, tooltip: props.tooltip, "data-hover": props.hover },
                props.label,
                index.h("slot", { name: "label" }))),
            children,
            (error || props.info) && index.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.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 = validator.debounce(validate, 1000);
    return Object.assign(fc, { getProps, handleInput, reset, validate });
};

exports.getInputCreator = getInputCreator;
