'use strict';

const index$1 = require('./index-578e90b1.js');
const validator = require('./validator-f6fe05c3.js');

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