"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const zod = require("zod");
const keyChain = require("./keyChain.cjs");
function validationErrorsByKeyChain(rootErrors = [], keyChain$1 = []) {
  if (!keyChain$1.length) {
    return [];
  }
  const keyChainAttrStringDots = keyChain.keyStringFromKeyChain(keyChain$1, "dots");
  const keyChainAttrStringSquareArray = keyChain.keyStringFromKeyChain(keyChain$1, "brackets");
  return rootErrors.filter((error) => keyChain.isMyKeyChainItem(error.key, keyChainAttrStringDots, keyChainAttrStringSquareArray));
}
function clientValidationReducer(state, action) {
  switch (action.type) {
    case "add-validation":
      return [...state, ...action.errors];
    case "clear-validation":
      return state.filter((s) => {
        var _a, _b;
        return !((!action.key || action.key === s.key) && (!((_a = action.identifiers) == null ? void 0 : _a.length) || ((_b = action.identifiers) == null ? void 0 : _b.some((i) => i === s.identifier))));
      });
    default:
      return state;
  }
}
function zodFromValidationSchema(schema) {
  const isArrayOfZod = (arrayItem) => {
    return !!(arrayItem == null ? void 0 : arrayItem.itemSchema);
  };
  const isObjectOfZod = (objectItem) => {
    return !!(objectItem == null ? void 0 : objectItem.schema);
  };
  const unpackObject = (incomingObject) => {
    return Object.keys(incomingObject).reduce((sch, key) => ({
      ...sch,
      // eslint-disable-next-line @typescript-eslint/no-use-before-define -- these two require eachother
      [key]: unpackValueToZod(incomingObject[key])
    }), {});
  };
  const unpackValueToZod = (incomingToZod) => {
    if (isArrayOfZod(incomingToZod)) {
      const arrayInner = zod.z.array(unpackValueToZod(incomingToZod.itemSchema));
      return incomingToZod.opts ? incomingToZod.opts(arrayInner) : arrayInner;
    }
    if (isObjectOfZod(incomingToZod)) {
      const obInner = unpackValueToZod(incomingToZod.schema);
      return incomingToZod.opts ? incomingToZod.opts(obInner) : obInner;
    }
    if (incomingToZod._def) {
      return incomingToZod;
    }
    if (typeof incomingToZod === "object") {
      return zod.z.object(unpackObject(incomingToZod));
    }
    return unpackValueToZod(incomingToZod);
  };
  if (isArrayOfZod(schema) || isObjectOfZod(schema)) {
    return unpackValueToZod(schema);
  }
  return zod.z.object(unpackObject(schema));
}
const getMyZodErrors = (errors, keyChainString) => {
  return errors.filter((e) => keyChainString ? keyChain.isMyKeyChainItem(keyChain.keyStringFromKeyChain(e.path, "dots"), keyChainString) : true).map((e) => ({
    key: keyChain.keyStringFromKeyChain(e.path, "dots"),
    message: e.message
  }));
};
const rootValidationSchemaIsFunction = (schema) => {
  return typeof schema === "function";
};
exports.clientValidationReducer = clientValidationReducer;
exports.getMyZodErrors = getMyZodErrors;
exports.rootValidationSchemaIsFunction = rootValidationSchemaIsFunction;
exports.validationErrorsByKeyChain = validationErrorsByKeyChain;
exports.zodFromValidationSchema = zodFromValidationSchema;
//# sourceMappingURL=validation.cjs.map
