// ../../packages/core/dist/index.solid.js
import * as v from "valibot";
import { batch, createSignal as createSignal$1, createUniqueId as createId, untrack } from "solid-js";
var framework = "solid";
// @__NO_SIDE_EFFECTS__
function createSignal(initialValue) {
  const [getSignal, setSignal] = createSignal$1(initialValue);
  return {
    get value() {
      return getSignal();
    },
    set value(nextValue) {
      setSignal(() => nextValue);
    }
  };
}
function initializeFieldStore(internalFormStore, internalFieldStore, schema, initialInput, path, nullish = false) {
  if (framework === "qwik" && schema.type === "lazy" || schema.type === "object_with_rest" || schema.type === "record" || schema.type === "tuple_with_rest" || schema.type === "promise") throw new Error(`"${schema.type}" schema is not supported`);
  else if (schema.type === "lazy") initializeFieldStore(internalFormStore, internalFieldStore, schema.getter(void 0), initialInput, path, nullish);
  else if (schema.type === "exact_optional" || schema.type === "nullable" || schema.type === "nullish" || schema.type === "optional" || schema.type === "undefinedable") initializeFieldStore(internalFormStore, internalFieldStore, schema.wrapped, initialInput === void 0 ? v.getDefault(schema) : initialInput, path, true);
  else if (schema.type === "non_nullable" || schema.type === "non_nullish" || schema.type === "non_optional") initializeFieldStore(internalFormStore, internalFieldStore, schema.wrapped, initialInput, path, nullish);
  else if (schema.type === "intersect" || schema.type === "union" || schema.type === "variant") for (const schemaOption of schema.options) initializeFieldStore(internalFormStore, internalFieldStore, schemaOption, initialInput, path, nullish);
  else {
    internalFieldStore.schema = schema;
    internalFieldStore.name = JSON.stringify(path);
    internalFieldStore.path = path;
    internalFieldStore.isNullish = nullish;
    const initialElements = [];
    internalFieldStore.initialElements = initialElements;
    internalFieldStore.elements = initialElements;
    internalFieldStore.errors = /* @__PURE__ */ createSignal(null);
    internalFieldStore.isTouched = /* @__PURE__ */ createSignal(false);
    internalFieldStore.isEdited = /* @__PURE__ */ createSignal(false);
    internalFieldStore.isDirty = /* @__PURE__ */ createSignal(false);
    if (schema.type === "array" || schema.type === "loose_tuple" || schema.type === "strict_tuple" || schema.type === "tuple") {
      if (internalFieldStore.kind && internalFieldStore.kind !== "array") throw new Error(`Store initialized as "${internalFieldStore.kind}" cannot be reinitialized as "array"`);
      internalFieldStore.kind = "array";
      if (internalFieldStore.kind === "array") {
        internalFieldStore.children ??= [];
        if (schema.type === "array") {
          if (initialInput) for (let index = 0; index < initialInput.length; index++) {
            internalFieldStore.children[index] = {};
            initializeFieldStore(internalFormStore, internalFieldStore.children[index], schema.item, initialInput[index], [...path, index]);
          }
        } else for (let index = 0; index < schema.items.length; index++) {
          internalFieldStore.children[index] = {};
          initializeFieldStore(internalFormStore, internalFieldStore.children[index], schema.items[index], initialInput?.[index], [...path, index]);
        }
        const arrayInput = nullish && initialInput == null ? initialInput : true;
        internalFieldStore.initialInput = /* @__PURE__ */ createSignal(arrayInput);
        internalFieldStore.startInput = /* @__PURE__ */ createSignal(arrayInput);
        internalFieldStore.input = /* @__PURE__ */ createSignal(arrayInput);
        const initialItems = internalFieldStore.children.map(createId);
        internalFieldStore.initialItems = /* @__PURE__ */ createSignal(initialItems);
        internalFieldStore.startItems = /* @__PURE__ */ createSignal(initialItems);
        internalFieldStore.items = /* @__PURE__ */ createSignal(initialItems);
      }
    } else if (schema.type === "loose_object" || schema.type === "object" || schema.type === "strict_object") {
      if (internalFieldStore.kind && internalFieldStore.kind !== "object") throw new Error(`Store initialized as "${internalFieldStore.kind}" cannot be reinitialized as "object"`);
      internalFieldStore.kind = "object";
      if (internalFieldStore.kind === "object") {
        internalFieldStore.children ??= {};
        for (const key in schema.entries) {
          internalFieldStore.children[key] ??= {};
          initializeFieldStore(internalFormStore, internalFieldStore.children[key], schema.entries[key], initialInput?.[key], [...path, key]);
        }
        const objectInput = nullish && initialInput == null ? initialInput : true;
        internalFieldStore.initialInput = /* @__PURE__ */ createSignal(objectInput);
        internalFieldStore.startInput = /* @__PURE__ */ createSignal(objectInput);
        internalFieldStore.input = /* @__PURE__ */ createSignal(objectInput);
      }
    } else {
      if (internalFieldStore.kind && internalFieldStore.kind !== "value") throw new Error(`Store initialized as "${internalFieldStore.kind}" cannot be reinitialized as "value"`);
      internalFieldStore.kind = "value";
      if (internalFieldStore.kind === "value") {
        const valueInput = initialInput === void 0 && !nullish ? internalFormStore.emptyInput[schema.type] : initialInput;
        internalFieldStore.initialInput = /* @__PURE__ */ createSignal(valueInput);
        internalFieldStore.startInput = /* @__PURE__ */ createSignal(valueInput);
        internalFieldStore.input = /* @__PURE__ */ createSignal(valueInput);
      }
    }
  }
}
function copyItemState(internalFormStore, fromInternalFieldStore, toInternalFieldStore) {
  batch(() => {
    untrack(() => {
      toInternalFieldStore.elements = fromInternalFieldStore.elements;
      toInternalFieldStore.errors.value = fromInternalFieldStore.errors.value;
      toInternalFieldStore.startInput.value = fromInternalFieldStore.startInput.value;
      toInternalFieldStore.input.value = fromInternalFieldStore.input.value;
      toInternalFieldStore.isTouched.value = fromInternalFieldStore.isTouched.value;
      toInternalFieldStore.isEdited.value = fromInternalFieldStore.isEdited.value;
      toInternalFieldStore.isDirty.value = fromInternalFieldStore.isDirty.value;
      if (fromInternalFieldStore.kind === "array" && toInternalFieldStore.kind === "array") {
        const fromItems = fromInternalFieldStore.items.value;
        toInternalFieldStore.startItems.value = fromInternalFieldStore.startItems.value;
        toInternalFieldStore.items.value = fromItems;
        for (let index = 0; index < fromItems.length; index++) {
          if (!toInternalFieldStore.children[index]) {
            toInternalFieldStore.children[index] = {};
            initializeFieldStore(internalFormStore, toInternalFieldStore.children[index], toInternalFieldStore.schema.item, void 0, [...toInternalFieldStore.path, index]);
          }
          copyItemState(internalFormStore, fromInternalFieldStore.children[index], toInternalFieldStore.children[index]);
        }
      } else if (fromInternalFieldStore.kind === "object" && toInternalFieldStore.kind === "object") for (const key in fromInternalFieldStore.children) copyItemState(internalFormStore, fromInternalFieldStore.children[key], toInternalFieldStore.children[key]);
    });
  });
}
function resetItemState(internalFormStore, internalFieldStore, input, keepStart = false) {
  batch(() => {
    const elements = [];
    if (internalFieldStore.elements === internalFieldStore.initialElements) internalFieldStore.initialElements = elements;
    internalFieldStore.elements = elements;
    internalFieldStore.errors.value = null;
    internalFieldStore.isTouched.value = false;
    internalFieldStore.isEdited.value = false;
    internalFieldStore.isDirty.value = false;
    if (internalFieldStore.kind === "array" || internalFieldStore.kind === "object") {
      const objectInput = internalFieldStore.isNullish && input == null ? input : true;
      if (!keepStart) internalFieldStore.startInput.value = objectInput;
      internalFieldStore.input.value = objectInput;
      if (internalFieldStore.kind === "array") {
        const isTuple = internalFieldStore.schema.type !== "array";
        if (input || isTuple) {
          const length = isTuple ? internalFieldStore.children.length : input.length;
          const newItems = Array.from({ length }, createId);
          if (!keepStart) internalFieldStore.startItems.value = newItems;
          internalFieldStore.items.value = newItems;
          for (let index = 0; index < length; index++) {
            const itemInput = input?.[index];
            if (internalFieldStore.children[index]) resetItemState(internalFormStore, internalFieldStore.children[index], itemInput, keepStart);
            else {
              internalFieldStore.children[index] = {};
              initializeFieldStore(internalFormStore, internalFieldStore.children[index], internalFieldStore.schema.item, itemInput, [...internalFieldStore.path, index]);
            }
          }
        } else {
          if (!keepStart) internalFieldStore.startItems.value = [];
          internalFieldStore.items.value = [];
        }
      } else for (const key in internalFieldStore.children) resetItemState(internalFormStore, internalFieldStore.children[key], input?.[key], keepStart);
    } else {
      const valueInput = input === void 0 && !internalFieldStore.isNullish ? internalFormStore.emptyInput[internalFieldStore.schema.type] : input;
      if (!keepStart) internalFieldStore.startInput.value = valueInput;
      internalFieldStore.input.value = valueInput;
    }
  });
}
function swapItemState(internalFormStore, firstInternalFieldStore, secondInternalFieldStore) {
  batch(() => {
    untrack(() => {
      const tempElements = firstInternalFieldStore.elements;
      firstInternalFieldStore.elements = secondInternalFieldStore.elements;
      secondInternalFieldStore.elements = tempElements;
      const tempErrors = firstInternalFieldStore.errors.value;
      firstInternalFieldStore.errors.value = secondInternalFieldStore.errors.value;
      secondInternalFieldStore.errors.value = tempErrors;
      const tempStartInput = firstInternalFieldStore.startInput.value;
      firstInternalFieldStore.startInput.value = secondInternalFieldStore.startInput.value;
      secondInternalFieldStore.startInput.value = tempStartInput;
      const tempInput = firstInternalFieldStore.input.value;
      firstInternalFieldStore.input.value = secondInternalFieldStore.input.value;
      secondInternalFieldStore.input.value = tempInput;
      const tempIsTouched = firstInternalFieldStore.isTouched.value;
      firstInternalFieldStore.isTouched.value = secondInternalFieldStore.isTouched.value;
      secondInternalFieldStore.isTouched.value = tempIsTouched;
      const tempIsEdited = firstInternalFieldStore.isEdited.value;
      firstInternalFieldStore.isEdited.value = secondInternalFieldStore.isEdited.value;
      secondInternalFieldStore.isEdited.value = tempIsEdited;
      const tempIsDirty = firstInternalFieldStore.isDirty.value;
      firstInternalFieldStore.isDirty.value = secondInternalFieldStore.isDirty.value;
      secondInternalFieldStore.isDirty.value = tempIsDirty;
      if (firstInternalFieldStore.kind === "array" && secondInternalFieldStore.kind === "array") {
        const firstItems = firstInternalFieldStore.items.value;
        const secondItems = secondInternalFieldStore.items.value;
        const tempStartItems = firstInternalFieldStore.startItems.value;
        firstInternalFieldStore.startItems.value = secondInternalFieldStore.startItems.value;
        secondInternalFieldStore.startItems.value = tempStartItems;
        firstInternalFieldStore.items.value = secondItems;
        secondInternalFieldStore.items.value = firstItems;
        const maxLength = Math.max(firstItems.length, secondItems.length);
        for (let index = 0; index < maxLength; index++) {
          if (!firstInternalFieldStore.children[index]) {
            firstInternalFieldStore.children[index] = {};
            initializeFieldStore(internalFormStore, firstInternalFieldStore.children[index], firstInternalFieldStore.schema.item, void 0, [...firstInternalFieldStore.path, index]);
          }
          if (!secondInternalFieldStore.children[index]) {
            secondInternalFieldStore.children[index] = {};
            initializeFieldStore(internalFormStore, secondInternalFieldStore.children[index], secondInternalFieldStore.schema.item, void 0, [...secondInternalFieldStore.path, index]);
          }
          swapItemState(internalFormStore, firstInternalFieldStore.children[index], secondInternalFieldStore.children[index]);
        }
      } else if (firstInternalFieldStore.kind === "object" && secondInternalFieldStore.kind === "object") for (const key in firstInternalFieldStore.children) swapItemState(internalFormStore, firstInternalFieldStore.children[key], secondInternalFieldStore.children[key]);
    });
  });
}
function focusFieldElement(internalFieldStore) {
  for (const element of internalFieldStore.elements) {
    element.focus();
    if (element.getRootNode().activeElement === element) return true;
  }
  return false;
}
function walkFieldStore(internalFieldStore, callback) {
  if (callback(internalFieldStore)) return true;
  if (internalFieldStore.kind === "array") {
    for (let index = 0; index < internalFieldStore.items.value.length; index++) if (walkFieldStore(internalFieldStore.children[index], callback)) return true;
  } else if (internalFieldStore.kind === "object") {
    for (const key in internalFieldStore.children) if (walkFieldStore(internalFieldStore.children[key], callback)) return true;
  }
  return false;
}
// @__NO_SIDE_EFFECTS__
function getFieldBool(internalFieldStore, type) {
  return walkFieldStore(internalFieldStore, (internalFieldStore$1) => Boolean(internalFieldStore$1[type].value));
}
// @__NO_SIDE_EFFECTS__
function getDirtyFieldInput(internalFieldStore, dirtyOnly = true) {
  if (dirtyOnly && !/* @__PURE__ */ getFieldBool(internalFieldStore, "isDirty")) return;
  if (internalFieldStore.kind === "array") {
    if (internalFieldStore.input.value) {
      const value = [];
      for (let index = 0; index < internalFieldStore.items.value.length; index++) value[index] = /* @__PURE__ */ getDirtyFieldInput(internalFieldStore.children[index], false);
      return value;
    }
    return internalFieldStore.input.value;
  }
  if (internalFieldStore.kind === "object") {
    if (internalFieldStore.input.value) {
      const value = {};
      for (const key in internalFieldStore.children) {
        const child = internalFieldStore.children[key];
        if (!dirtyOnly || /* @__PURE__ */ getFieldBool(child, "isDirty")) value[key] = /* @__PURE__ */ getDirtyFieldInput(child, dirtyOnly);
      }
      return value;
    }
    return internalFieldStore.input.value;
  }
  return internalFieldStore.input.value;
}
// @__NO_SIDE_EFFECTS__
function getFieldInput(internalFieldStore) {
  if (internalFieldStore.kind === "array") {
    if (internalFieldStore.input.value) {
      const value = [];
      for (let index = 0; index < internalFieldStore.items.value.length; index++) value[index] = /* @__PURE__ */ getFieldInput(internalFieldStore.children[index]);
      return value;
    }
    return internalFieldStore.input.value;
  }
  if (internalFieldStore.kind === "object") {
    if (internalFieldStore.input.value) {
      const value = {};
      for (const key in internalFieldStore.children) value[key] = /* @__PURE__ */ getFieldInput(internalFieldStore.children[key]);
      return value;
    }
    return internalFieldStore.input.value;
  }
  return internalFieldStore.input.value;
}
// @__NO_SIDE_EFFECTS__
function getElementInput(element, internalFieldStore) {
  if (element.options && element.multiple) return [...element.options].filter((option) => option.selected && !option.disabled).map((option) => option.value);
  if (element.type === "checkbox") {
    const options = document.getElementsByName(element.name);
    if (options.length > 1) return [...options].filter((option) => option.checked).map((option) => option.value);
    return element.checked;
  }
  if (element.type === "radio") {
    if (element.checked) return element.value;
    return untrack(() => /* @__PURE__ */ getFieldInput(internalFieldStore));
  }
  if (element.type === "file") {
    if (element.multiple) return [...element.files];
    return element.files[0];
  }
  return element.value;
}
// @__NO_SIDE_EFFECTS__
function getFieldStore(internalFormStore, path) {
  let internalFieldStore = internalFormStore;
  for (const key of path) internalFieldStore = internalFieldStore.children[key];
  return internalFieldStore;
}
function setFieldBool(internalFieldStore, type, bool) {
  batch(() => {
    untrack(() => {
      walkFieldStore(internalFieldStore, (internalFieldStore$1) => {
        internalFieldStore$1[type].value = bool;
      });
    });
  });
}
function setNestedInput(internalFormStore, internalFieldStore, input) {
  internalFieldStore.isTouched.value = true;
  internalFieldStore.isEdited.value = true;
  if (internalFieldStore.kind === "array") {
    const arrayInput = input ?? [];
    const items = internalFieldStore.items.value;
    const length = internalFieldStore.schema.type === "array" ? arrayInput.length : internalFieldStore.children.length;
    if (length < items.length) internalFieldStore.items.value = items.slice(0, length);
    else if (length > items.length) {
      for (let index = items.length; index < length; index++) if (internalFieldStore.children[index]) resetItemState(internalFormStore, internalFieldStore.children[index], arrayInput[index], true);
      else {
        internalFieldStore.children[index] = {};
        initializeFieldStore(internalFormStore, internalFieldStore.children[index], internalFieldStore.schema.item, arrayInput[index], [...internalFieldStore.path, index]);
      }
      internalFieldStore.items.value = [...items, ...Array.from({ length: length - items.length }, createId)];
    }
    for (let index = 0; index < length; index++) setNestedInput(internalFormStore, internalFieldStore.children[index], arrayInput[index]);
    internalFieldStore.input.value = input == null ? input : true;
    internalFieldStore.isDirty.value = internalFieldStore.startInput.value !== internalFieldStore.input.value || internalFieldStore.startItems.value.length !== internalFieldStore.items.value.length;
  } else if (internalFieldStore.kind === "object") {
    for (const key in internalFieldStore.children) setNestedInput(internalFormStore, internalFieldStore.children[key], input?.[key]);
    internalFieldStore.input.value = input == null ? input : true;
    internalFieldStore.isDirty.value = internalFieldStore.startInput.value !== internalFieldStore.input.value;
  } else {
    internalFieldStore.input.value = input;
    const startInput = internalFieldStore.startInput.value;
    internalFieldStore.isDirty.value = startInput !== input && (startInput != null || input !== "" && !Number.isNaN(input));
  }
}
function setFieldInput(internalFormStore, path, input) {
  batch(() => {
    untrack(() => {
      let internalFieldStore = internalFormStore;
      for (let index = 0; index < path.length; index++) {
        internalFieldStore = internalFieldStore.children[path[index]];
        if (index < path.length - 1) internalFieldStore.input.value = true;
      }
      setNestedInput(internalFormStore, internalFieldStore, input);
    });
  });
}
function setInitialFieldInput(internalFormStore, internalFieldStore, initialInput) {
  batch(() => {
    if (internalFieldStore.kind === "array") {
      internalFieldStore.initialInput.value = initialInput == null ? initialInput : true;
      const initialArrayInput = initialInput ?? [];
      const length = internalFieldStore.schema.type === "array" ? initialArrayInput.length : internalFieldStore.children.length;
      if (length > internalFieldStore.children.length) for (let index = internalFieldStore.children.length; index < length; index++) {
        internalFieldStore.children[index] = {};
        initializeFieldStore(internalFormStore, internalFieldStore.children[index], internalFieldStore.schema.item, initialArrayInput[index], [...internalFieldStore.path, index]);
      }
      internalFieldStore.initialItems.value = Array.from({ length }, createId);
      for (let index = 0; index < internalFieldStore.children.length; index++) setInitialFieldInput(internalFormStore, internalFieldStore.children[index], initialArrayInput[index]);
    } else if (internalFieldStore.kind === "object") {
      internalFieldStore.initialInput.value = initialInput == null ? initialInput : true;
      for (const key in internalFieldStore.children) setInitialFieldInput(internalFormStore, internalFieldStore.children[key], initialInput?.[key]);
    } else internalFieldStore.initialInput.value = initialInput === void 0 && !internalFieldStore.isNullish ? internalFormStore.emptyInput[internalFieldStore.schema.type] : initialInput;
  });
}
var DEFAULT_EMPTY_INPUT = { string: "" };
// @__NO_SIDE_EFFECTS__
function createFormStore(config, parse) {
  const store = {};
  store.emptyInput = {
    ...DEFAULT_EMPTY_INPUT,
    ...config.emptyInput
  };
  store.validators = 0;
  store.validate = config.validate ?? "submit";
  store.revalidate = config.revalidate ?? "input";
  store.parse = parse;
  store.isSubmitting = /* @__PURE__ */ createSignal(false);
  store.isSubmitted = /* @__PURE__ */ createSignal(false);
  store.isValidating = /* @__PURE__ */ createSignal(false);
  initializeFieldStore(store, store, config.schema, config.initialInput, []);
  return store;
}
async function validateFormInput(internalFormStore, config) {
  internalFormStore.validators++;
  internalFormStore.isValidating.value = true;
  try {
    const result = await internalFormStore.parse(untrack(() => /* @__PURE__ */ getFieldInput(internalFormStore)));
    let rootErrors;
    let nestedErrors;
    if (result.issues) {
      nestedErrors = {};
      for (const issue of result.issues) if (issue.path) {
        const path = [];
        for (const pathItem of issue.path) {
          const key = pathItem.key;
          const keyType = typeof key;
          const itemType = pathItem.type;
          if (keyType !== "string" && keyType !== "number" || itemType === "map" || itemType === "set") break;
          path.push(key);
        }
        const name = JSON.stringify(path);
        const fieldErrors = nestedErrors[name];
        if (fieldErrors) fieldErrors.push(issue.message);
        else nestedErrors[name] = [issue.message];
      } else if (rootErrors) rootErrors.push(issue.message);
      else rootErrors = [issue.message];
    }
    let shouldFocus = config?.shouldFocus ?? false;
    batch(() => {
      untrack(() => {
        walkFieldStore(internalFormStore, (internalFieldStore) => {
          if (internalFieldStore.path.length === 0) internalFieldStore.errors.value = rootErrors ?? null;
          else {
            const fieldErrors = nestedErrors?.[internalFieldStore.name] ?? null;
            internalFieldStore.errors.value = fieldErrors;
            if (shouldFocus && fieldErrors && focusFieldElement(internalFieldStore)) shouldFocus = false;
          }
        });
      });
      internalFormStore.validators--;
      internalFormStore.isValidating.value = internalFormStore.validators > 0;
    });
    return result;
  } catch (error) {
    batch(() => {
      internalFormStore.validators--;
      internalFormStore.isValidating.value = internalFormStore.validators > 0;
    });
    throw error;
  }
}
function validateIfRequired(internalFormStore, internalFieldStore, validationMode) {
  if (validationMode === (internalFormStore.validate === "initial" || (internalFormStore.validate === "submit" ? untrack(() => internalFormStore.isSubmitted.value) : untrack(() => /* @__PURE__ */ getFieldBool(internalFieldStore, "errors"))) ? internalFormStore.revalidate : internalFormStore.validate)) validateFormInput(internalFormStore);
}
var INTERNAL = "~internal";

// ../../packages/methods/dist/index.solid.js
function focus(form, config) {
  focusFieldElement(getFieldStore(form[INTERNAL], config.path));
}
// @__NO_SIDE_EFFECTS__
function getDeepErrorEntries(form, config) {
  const entries = [];
  walkFieldStore(config?.path ? getFieldStore(form[INTERNAL], config.path) : form[INTERNAL], (internalFieldStore) => {
    const errors = internalFieldStore.errors.value;
    if (errors) entries.push({
      path: internalFieldStore.path,
      errors
    });
  });
  return entries;
}
// @__NO_SIDE_EFFECTS__
function getDeepErrors(form, config) {
  let deepErrors = null;
  walkFieldStore(config?.path ? getFieldStore(form[INTERNAL], config.path) : form[INTERNAL], (internalFieldStore) => {
    const errors = internalFieldStore.errors.value;
    if (errors) if (deepErrors) deepErrors.push(...errors);
    else deepErrors = [...errors];
  });
  return deepErrors;
}
// @__NO_SIDE_EFFECTS__
function getDirtyInput(form, config) {
  return getDirtyFieldInput(config?.path ? getFieldStore(form[INTERNAL], config.path) : form[INTERNAL]);
}
// @__NO_SIDE_EFFECTS__
function getDirtyPaths(form, config) {
  const paths = [];
  config?.path ? getFieldStore(form[INTERNAL], config.path) : form[INTERNAL];
  return paths;
}
// @__NO_SIDE_EFFECTS__
function getErrors(form, config) {
  return (config?.path ? getFieldStore(form[INTERNAL], config.path) : form[INTERNAL]).errors.value;
}
// @__NO_SIDE_EFFECTS__
function getInput(form, config) {
  return getFieldInput(config?.path ? getFieldStore(form[INTERNAL], config.path) : form[INTERNAL]);
}
// @__NO_SIDE_EFFECTS__
function handleSubmit(form, handler) {
  return async (event) => {
    event?.preventDefault();
    const internalFormStore = form[INTERNAL];
    internalFormStore.isSubmitted.value = true;
    internalFormStore.isSubmitting.value = true;
    try {
      const result = await validateFormInput(internalFormStore, { shouldFocus: true });
      if (result.success) await handler(result.output, event);
    } catch (error) {
      internalFormStore.errors.value = [error && typeof error === "object" && "message" in error && typeof error.message === "string" ? error.message : "An unknown error has occurred."];
    } finally {
      internalFormStore.isSubmitting.value = false;
    }
  };
}
function insert(form, config) {
  const internalFormStore = form[INTERNAL];
  let internalFieldStore = internalFormStore;
  for (let index = 0; index < config.path.length; index++) {
    internalFieldStore = internalFieldStore.children[config.path[index]];
    if (index < config.path.length - 1) internalFieldStore.input.value = true;
  }
  const internalArrayStore = internalFieldStore;
  const items = untrack(() => internalArrayStore.items.value);
  const insertIndex = config.at === void 0 ? items.length : config.at;
  if (insertIndex >= 0 && insertIndex <= items.length) batch(() => {
    const newItems = [...items];
    newItems.splice(insertIndex, 0, createId());
    internalArrayStore.items.value = newItems;
    for (let index = items.length; index > insertIndex; index--) {
      if (!internalArrayStore.children[index]) {
        internalArrayStore.children[index] = {};
        initializeFieldStore(internalFormStore, internalArrayStore.children[index], internalArrayStore.schema.item, void 0, [...internalArrayStore.path, index]);
      }
      copyItemState(internalFormStore, internalArrayStore.children[index - 1], internalArrayStore.children[index]);
    }
    if (!internalArrayStore.children[insertIndex]) {
      internalArrayStore.children[insertIndex] = {};
      initializeFieldStore(internalFormStore, internalArrayStore.children[insertIndex], internalArrayStore.schema.item, config.initialInput, [...internalArrayStore.path, insertIndex]);
    } else resetItemState(internalFormStore, internalArrayStore.children[insertIndex], config.initialInput);
    internalArrayStore.input.value = true;
    internalArrayStore.isTouched.value = true;
    internalArrayStore.isEdited.value = true;
    internalArrayStore.isDirty.value = true;
    validateIfRequired(internalFormStore, internalArrayStore, "input");
  });
}
// @__NO_SIDE_EFFECTS__
function isDirty(form, config) {
  return getFieldBool(config?.path ? getFieldStore(form[INTERNAL], config.path) : form[INTERNAL], "isDirty");
}
// @__NO_SIDE_EFFECTS__
function isEdited(form, config) {
  return getFieldBool(config?.path ? getFieldStore(form[INTERNAL], config.path) : form[INTERNAL], "isEdited");
}
// @__NO_SIDE_EFFECTS__
function isTouched(form, config) {
  return getFieldBool(config?.path ? getFieldStore(form[INTERNAL], config.path) : form[INTERNAL], "isTouched");
}
// @__NO_SIDE_EFFECTS__
function isValid(form, config) {
  return !getFieldBool(config?.path ? getFieldStore(form[INTERNAL], config.path) : form[INTERNAL], "errors");
}
function move(form, config) {
  const internalFormStore = form[INTERNAL];
  const internalArrayStore = getFieldStore(internalFormStore, config.path);
  const items = untrack(() => internalArrayStore.items.value);
  if (config.from >= 0 && config.from <= items.length - 1 && config.to >= 0 && config.to <= items.length - 1 && config.from !== config.to) batch(() => {
    const newItems = [...items];
    newItems.splice(config.to, 0, newItems.splice(config.from, 1)[0]);
    internalArrayStore.items.value = newItems;
    const tempInternalFieldStore = {};
    initializeFieldStore(internalFormStore, tempInternalFieldStore, internalArrayStore.schema.item, void 0, []);
    copyItemState(internalFormStore, internalArrayStore.children[config.from], tempInternalFieldStore);
    if (config.from < config.to) for (let index = config.from; index < config.to; index++) copyItemState(internalFormStore, internalArrayStore.children[index + 1], internalArrayStore.children[index]);
    else for (let index = config.from; index > config.to; index--) copyItemState(internalFormStore, internalArrayStore.children[index - 1], internalArrayStore.children[index]);
    copyItemState(internalFormStore, tempInternalFieldStore, internalArrayStore.children[config.to]);
    internalArrayStore.isTouched.value = true;
    internalArrayStore.isEdited.value = true;
    internalArrayStore.isDirty.value = internalArrayStore.startItems.value.join() !== newItems.join();
    validateIfRequired(internalFormStore, internalArrayStore, "input");
  });
}
// @__NO_SIDE_EFFECTS__
function pickDirty(form, config) {
  if (!getFieldBool(form[INTERNAL], "isDirty")) return;
  const result = /* @__PURE__ */ pickFieldValue(form[INTERNAL], config.from);
  return Object.keys(result).length ? result : void 0;
}
// @__NO_SIDE_EFFECTS__
function pickFieldValue(internalFieldStore, value) {
  if (internalFieldStore.kind === "object" && internalFieldStore.input.value && value && typeof value === "object" && !Array.isArray(value)) {
    const result = {};
    for (const key in internalFieldStore.children) {
      const child = internalFieldStore.children[key];
      if (getFieldBool(child, "isDirty") && key in value) result[key] = /* @__PURE__ */ pickFieldValue(child, value[key]);
    }
    return result;
  }
  return value;
}
function remove(form, config) {
  const internalFormStore = form[INTERNAL];
  const internalArrayStore = getFieldStore(internalFormStore, config.path);
  const items = untrack(() => internalArrayStore.items.value);
  if (config.at >= 0 && config.at <= items.length - 1) batch(() => {
    const newItems = [...items];
    newItems.splice(config.at, 1);
    internalArrayStore.items.value = newItems;
    for (let index = config.at; index < items.length - 1; index++) copyItemState(internalFormStore, internalArrayStore.children[index + 1], internalArrayStore.children[index]);
    internalArrayStore.isTouched.value = true;
    internalArrayStore.isEdited.value = true;
    internalArrayStore.isDirty.value = internalArrayStore.startItems.value.join() !== newItems.join();
    validateIfRequired(internalFormStore, internalArrayStore, "input");
  });
}
function replace(form, config) {
  const internalFormStore = form[INTERNAL];
  const internalArrayStore = getFieldStore(internalFormStore, config.path);
  const items = untrack(() => internalArrayStore.items.value);
  if (config.at >= 0 && config.at <= items.length - 1) batch(() => {
    const newItems = [...items];
    newItems[config.at] = createId();
    internalArrayStore.items.value = newItems;
    resetItemState(internalFormStore, internalArrayStore.children[config.at], config.initialInput);
    internalArrayStore.isTouched.value = true;
    internalArrayStore.isEdited.value = true;
    internalArrayStore.isDirty.value = true;
    validateIfRequired(internalFormStore, internalArrayStore, "input");
  });
}
function reset(form, config) {
  batch(() => {
    untrack(() => {
      const internalFormStore = form[INTERNAL];
      const internalFieldStore = config?.path ? getFieldStore(internalFormStore, config.path) : internalFormStore;
      if (config && "initialInput" in config) setInitialFieldInput(internalFormStore, internalFieldStore, config.initialInput);
      walkFieldStore(internalFieldStore, (internalFieldStore$1) => {
        internalFieldStore$1.elements = internalFieldStore$1.initialElements;
        if (!config?.keepErrors) internalFieldStore$1.errors.value = null;
        if (!config?.keepTouched) internalFieldStore$1.isTouched.value = false;
        if (!config?.keepEdited) internalFieldStore$1.isEdited.value = false;
        internalFieldStore$1.startInput.value = internalFieldStore$1.initialInput.value;
        if (!config?.keepInput) internalFieldStore$1.input.value = internalFieldStore$1.initialInput.value;
        if (internalFieldStore$1.kind === "array") {
          internalFieldStore$1.startItems.value = internalFieldStore$1.initialItems.value;
          if (!config?.keepInput || internalFieldStore$1.startItems.value.length === internalFieldStore$1.items.value.length) internalFieldStore$1.items.value = internalFieldStore$1.initialItems.value;
          internalFieldStore$1.isDirty.value = internalFieldStore$1.startInput.value !== internalFieldStore$1.input.value || internalFieldStore$1.startItems.value !== internalFieldStore$1.items.value;
        } else if (internalFieldStore$1.kind === "object") internalFieldStore$1.isDirty.value = internalFieldStore$1.startInput.value !== internalFieldStore$1.input.value;
        else {
          const startInput = internalFieldStore$1.startInput.value;
          const input = internalFieldStore$1.input.value;
          internalFieldStore$1.isDirty.value = startInput !== input && (startInput != null || input !== "" && !Number.isNaN(input));
          for (const element of internalFieldStore$1.elements) if (element.type === "file") element.value = "";
        }
      });
      if (!config?.path) {
        if (!config?.keepSubmitted) internalFormStore.isSubmitted.value = false;
        if (internalFormStore.validate === "initial") validateFormInput(internalFormStore);
      }
    });
  });
}
function setErrors(form, config) {
  (config.path ? getFieldStore(form[INTERNAL], config.path) : form[INTERNAL]).errors.value = config.errors;
}
function setInput(form, config) {
  batch(() => {
    const internalFormStore = form[INTERNAL];
    setFieldInput(internalFormStore, config.path ?? [], config.input);
    validateIfRequired(internalFormStore, config.path ? getFieldStore(internalFormStore, config.path) : internalFormStore, "input");
  });
}
function submit(form) {
  form[INTERNAL].element?.requestSubmit();
}
function swap(form, config) {
  const internalFormStore = form[INTERNAL];
  const internalArrayStore = getFieldStore(internalFormStore, config.path);
  const items = untrack(() => internalArrayStore.items.value);
  if (config.at >= 0 && config.at <= items.length - 1 && config.and >= 0 && config.and <= items.length - 1 && config.at !== config.and) batch(() => {
    const newItems = [...items];
    const tempItemId = newItems[config.at];
    newItems[config.at] = newItems[config.and];
    newItems[config.and] = tempItemId;
    internalArrayStore.items.value = newItems;
    swapItemState(internalFormStore, internalArrayStore.children[config.at], internalArrayStore.children[config.and]);
    internalArrayStore.isTouched.value = true;
    internalArrayStore.isEdited.value = true;
    internalArrayStore.isDirty.value = internalArrayStore.startItems.value.join() !== newItems.join();
    validateIfRequired(internalFormStore, internalArrayStore, "input");
  });
}
function validate(form, config) {
  return validateFormInput(form[INTERNAL], config);
}

// src/primitives/createForm/createForm.ts
import { createMemo } from "solid-js";
import * as v2 from "valibot";
// @__NO_SIDE_EFFECTS__
function createForm(config) {
  const internalFormStore = createFormStore(
    config,
    (input) => v2.safeParseAsync(config.schema, input)
  );
  const getIsTouched = createMemo(
    () => getFieldBool(internalFormStore, "isTouched")
  );
  const getIsEdited = createMemo(
    () => getFieldBool(internalFormStore, "isEdited")
  );
  const getIsDirty = createMemo(
    () => getFieldBool(internalFormStore, "isDirty")
  );
  const getIsValid = createMemo(
    () => !getFieldBool(internalFormStore, "errors")
  );
  const form = {
    [INTERNAL]: internalFormStore,
    get isSubmitting() {
      return internalFormStore.isSubmitting.value;
    },
    get isSubmitted() {
      return internalFormStore.isSubmitted.value;
    },
    get isValidating() {
      return internalFormStore.isValidating.value;
    },
    get isTouched() {
      return getIsTouched();
    },
    get isEdited() {
      return getIsEdited();
    },
    get isDirty() {
      return getIsDirty();
    },
    get isValid() {
      return getIsValid();
    },
    get errors() {
      return internalFormStore.errors.value;
    }
  };
  if (config.validate === "initial") {
    validateFormInput(form[INTERNAL]);
  }
  return form;
}

// src/primitives/useField/useField.ts
import { createMemo as createMemo2, onCleanup } from "solid-js";

// src/utils/unwrap/unwrap.ts
function unwrap(value) {
  if (typeof value === "function") {
    return value();
  }
  return value;
}

// src/primitives/useField/useField.ts
// @__NO_SIDE_EFFECTS__
function useField(form, config) {
  const getPath = createMemo2(() => unwrap(config).path);
  const getInternalFormStore = createMemo2(() => unwrap(form)[INTERNAL]);
  const getInternalFieldStore = createMemo2(
    () => getFieldStore(getInternalFormStore(), getPath())
  );
  const getInput2 = createMemo2(() => getFieldInput(getInternalFieldStore()));
  const getIsTouched = createMemo2(
    () => getFieldBool(getInternalFieldStore(), "isTouched")
  );
  const getIsEdited = createMemo2(
    () => getFieldBool(getInternalFieldStore(), "isEdited")
  );
  const getIsDirty = createMemo2(
    () => getFieldBool(getInternalFieldStore(), "isDirty")
  );
  const getIsValid = createMemo2(
    () => !getFieldBool(getInternalFieldStore(), "errors")
  );
  return {
    get path() {
      return getPath();
    },
    get input() {
      return getInput2();
    },
    get errors() {
      return getInternalFieldStore().errors.value;
    },
    get isTouched() {
      return getIsTouched();
    },
    get isEdited() {
      return getIsEdited();
    },
    get isDirty() {
      return getIsDirty();
    },
    get isValid() {
      return getIsValid();
    },
    onInput(value) {
      setFieldInput(getInternalFormStore(), getPath(), value);
      validateIfRequired(
        getInternalFormStore(),
        getInternalFieldStore(),
        "input"
      );
    },
    props: {
      get name() {
        return getInternalFieldStore().name;
      },
      // eslint-disable-next-line solid/reactivity
      autofocus: !!getInternalFieldStore().errors.value,
      ref: (element) => {
        const internalFieldStore = getInternalFieldStore();
        internalFieldStore.elements.push(element);
        onCleanup(() => {
          const elements = internalFieldStore.elements.filter(
            (el) => el !== element
          );
          if (internalFieldStore.elements === internalFieldStore.initialElements) {
            internalFieldStore.initialElements = elements;
          }
          internalFieldStore.elements = elements;
        });
      },
      onFocus() {
        setFieldBool(getInternalFieldStore(), "isTouched", true);
        validateIfRequired(
          getInternalFormStore(),
          getInternalFieldStore(),
          "touch"
        );
      },
      onInput(event) {
        const internalFieldStore = getInternalFieldStore();
        setFieldInput(
          getInternalFormStore(),
          getPath(),
          getElementInput(event.currentTarget, internalFieldStore)
        );
        validateIfRequired(getInternalFormStore(), internalFieldStore, "input");
      },
      onChange() {
        validateIfRequired(
          getInternalFormStore(),
          getInternalFieldStore(),
          "change"
        );
      },
      onBlur() {
        validateIfRequired(
          getInternalFormStore(),
          getInternalFieldStore(),
          "blur"
        );
      }
    }
  };
}

// src/primitives/useFieldArray/useFieldArray.ts
import { createMemo as createMemo3 } from "solid-js";
// @__NO_SIDE_EFFECTS__
function useFieldArray(form, config) {
  const getInternalFieldStore = createMemo3(
    () => getFieldStore(
      unwrap(form)[INTERNAL],
      unwrap(config).path
    )
  );
  const getIsTouched = createMemo3(
    () => getFieldBool(getInternalFieldStore(), "isTouched")
  );
  const getIsEdited = createMemo3(
    () => getFieldBool(getInternalFieldStore(), "isEdited")
  );
  const getIsDirty = createMemo3(
    () => getFieldBool(getInternalFieldStore(), "isDirty")
  );
  const getIsValid = createMemo3(
    () => !getFieldBool(getInternalFieldStore(), "errors")
  );
  return {
    get path() {
      return unwrap(config).path;
    },
    get items() {
      return getInternalFieldStore().items.value;
    },
    get errors() {
      return getInternalFieldStore().errors.value;
    },
    get isTouched() {
      return getIsTouched();
    },
    get isEdited() {
      return getIsEdited();
    },
    get isDirty() {
      return getIsDirty();
    },
    get isValid() {
      return getIsValid();
    }
  };
}

// src/components/Field/Field.tsx
// @__NO_SIDE_EFFECTS__
function Field(props) {
  const field = useField(
    () => props.of,
    () => ({ path: props.path })
  );
  return <>{props.children(field)}</>;
}

// src/components/FieldArray/FieldArray.tsx
// @__NO_SIDE_EFFECTS__
function FieldArray(props) {
  const field = useFieldArray(
    () => props.of,
    () => ({ path: props.path })
  );
  return <>{props.children(field)}</>;
}

// src/components/Form/Form.tsx
import { splitProps } from "solid-js";
// @__NO_SIDE_EFFECTS__
function Form(props) {
  const [, other] = splitProps(props, ["of", "onSubmit"]);
  return <form
    {...other}
    novalidate
    ref={(element) => {
      props.of[INTERNAL].element = element;
    }}
    onSubmit={(event) => handleSubmit(props.of, props.onSubmit)(event)}
  />;
}
export {
  Field,
  FieldArray,
  Form,
  createForm,
  focus,
  getDeepErrorEntries,
  getDeepErrors,
  getDirtyInput,
  getDirtyPaths,
  getErrors,
  getInput,
  handleSubmit,
  insert,
  isDirty,
  isEdited,
  isTouched,
  isValid,
  move,
  pickDirty,
  remove,
  replace,
  reset,
  setErrors,
  setInput,
  submit,
  swap,
  useField,
  useFieldArray,
  validate
};
