"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useFormValidationError = useFormValidationError;
exports.useValidation = useValidation;
var _utils = require("@grozav/utils");
var _vue = require("vue");
var _IForm = require("@inkline/inkline/components/IForm");
var _IFormGroup = require("@inkline/inkline/components/IFormGroup");
var _validation = require("@inkline/inkline/validation");
var _useInkline = require("@inkline/inkline/composables/useInkline");
function useValidation(options) {
  const inkline = (0, _useInkline.useInkline)();
  const form = (0, _vue.inject)(_IForm.FormKey, null);
  const formGroup = (0, _vue.inject)(_IFormGroup.FormGroupKey, null);
  const schema = form ? (0, _vue.computed)(() => form.schema && options.validate?.value && (0, _utils.getValueByPath)(form.schema.value, options.name.value)) : (0, _vue.ref)(options.schema?.value || null);
  if (!form && options.schema?.value) {
    (0, _vue.watch)(() => options.schema?.value, value => {
      schema.value = value;
    });
  }
  function shouldValidate(schema2, eventName) {
    if (!options.validate?.value) {
      return;
    }
    const events = schema2.validateOn ? [].concat(schema2.validateOn) : inkline?.options?.validateOn;
    return events.includes(eventName);
  }
  function setValue(name, value) {
    if (!options.validate?.value) {
      return;
    }
    let clonedSchema = (0, _utils.clone)(schema.value);
    const targetSchema = (0, _utils.getValueByPath)(clonedSchema, name);
    if (!targetSchema) {
      throw new Error('Schema to be validated not found. Did you forget to match the schema key to the input "name" prop?');
    }
    clonedSchema = (0, _utils.setValueByPath)(clonedSchema, `${name}.value`, value);
    clonedSchema = (0, _utils.setValuesAlongPath)(clonedSchema, name, {
      pristine: false,
      dirty: true
    });
    if (shouldValidate(targetSchema, "input")) {
      clonedSchema = (0, _validation.validate)(clonedSchema);
    }
    schema.value = clonedSchema;
    options.onUpdate?.(clonedSchema);
  }
  function setTouched(name, event) {
    if (!options.validate?.value) {
      return;
    }
    let clonedSchema = (0, _utils.clone)(schema.value);
    const targetSchema = (0, _utils.getValueByPath)(clonedSchema, name);
    if (!targetSchema) {
      throw new Error('Schema to be validated not found. Did you forget to match the schema key to the input "name" prop?');
    }
    clonedSchema = (0, _utils.setValuesAlongPath)(clonedSchema, name, {
      untouched: false,
      touched: true
    });
    if (shouldValidate(targetSchema, event.type)) {
      clonedSchema = (0, _validation.validate)(clonedSchema);
    }
    schema.value = clonedSchema;
    options.onUpdate?.(clonedSchema);
  }
  function onSubmit(event) {
    if (!options.validate?.value) {
      return;
    }
    let clonedSchema = (0, _utils.clone)(schema.value);
    clonedSchema = (0, _utils.setValuesAlongPath)((0, _validation.validate)(clonedSchema), "", {
      untouched: false,
      touched: true
    });
    if (clonedSchema.valid) {
      options.onSubmit?.(event);
    }
    schema.value = clonedSchema;
    options.onUpdate?.(clonedSchema);
  }
  function onInput(nameRef, value) {
    const name = (0, _vue.unref)(nameRef);
    if (!options.validate?.value) {
      return;
    }
    if (formGroup) {
      formGroup.onInput(name, value);
    } else if (form) {
      form.onInput(name, value);
    } else if (options.schema?.value) {
      setValue(name, value);
    }
  }
  function onBlur(nameRef, event) {
    const name = (0, _vue.unref)(nameRef);
    if (!options.validate?.value) {
      return;
    }
    if (formGroup) {
      formGroup.onBlur(name, event);
    } else if (form) {
      form.onBlur(name, event);
    } else if (options.schema?.value) {
      setTouched(name, event);
    }
  }
  return {
    schema,
    onSubmit,
    onInput,
    onBlur
  };
}
function useFormValidationError(options) {
  const hasError = (0, _vue.computed)(() => {
    if (typeof options.error.value === "boolean") {
      return options.error.value;
    } else if (options.schema.value && options.error.value) {
      let visible = true;
      [].concat(options.error.value).forEach(status => {
        visible = visible && options.schema.value[status];
      });
      return visible;
    }
    return false;
  });
  return {
    hasError
  };
}