"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
function valueByKeyChain(state, keyChain) {
  if (!(keyChain == null ? void 0 : keyChain.length)) {
    return state;
  }
  return keyChain.reduce((value, key) => {
    return value == null ? void 0 : value[key];
  }, state ?? {});
}
function mergeDeepFromKeyChain(target, keyChain, value) {
  if (keyChain.length === 0) {
    if (Array.isArray(value)) {
      return [...value];
    }
    if (typeof value === "object") {
      return { ...value };
    }
  }
  const output = Array.isArray(target) || Number.isInteger(keyChain[0]) ? [...target || []] : { ...target || {} };
  let bookmarkRef = output;
  for (let i = 0; i < keyChain.length; i += 1) {
    const key = keyChain[i];
    if (i === keyChain.length - 1) {
      if (Array.isArray(value)) {
        bookmarkRef[key] = [...value];
      } else if (typeof value === "object") {
        bookmarkRef[key] = { ...value };
      } else {
        bookmarkRef[key] = value;
      }
    } else if (Array.isArray(bookmarkRef[key])) {
      bookmarkRef[key] = [...bookmarkRef[key]];
      bookmarkRef = bookmarkRef[key];
    } else if (typeof bookmarkRef[key] === "object") {
      bookmarkRef[key] = { ...bookmarkRef[key] };
      bookmarkRef = bookmarkRef[key];
    } else if (Number.isInteger(key)) {
      bookmarkRef[key] = [...bookmarkRef[key]];
      bookmarkRef = bookmarkRef[key];
    } else {
      bookmarkRef[key] = { ...bookmarkRef[key] };
      bookmarkRef = bookmarkRef[key];
    }
  }
  return output;
}
function childKeyChainStringFromParent(childKeyChainString, parentKeyChain) {
  const regexSafeParent = (parentKeyChain == null ? void 0 : parentKeyChain.join(".").replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&")) ?? "";
  const childWithoutParent = childKeyChainString.replace(new RegExp(`^${regexSafeParent}`), "");
  return childWithoutParent.replace(/^.*?\./, "");
}
function isArrayValue(value, attemptedAction) {
  if (value && !Array.isArray(value)) {
    throw new Error(`"${attemptedAction}" cannot be used on a set form property that does not contain an array value, the current value of this property is: ${JSON.stringify(value)}`);
  }
  return true;
}
function keyStringFromKeyChain(keyChain, mode) {
  switch (mode) {
    case "dots":
      return (keyChain == null ? void 0 : keyChain.filter((key) => !!key).join(".")) ?? "";
    case "brackets":
      return (keyChain == null ? void 0 : keyChain.reduce((attrString, key) => {
        if (typeof key === "string") {
          return `${attrString}${attrString ? `.` : ""}${key}`;
        }
        if (typeof key === "number") {
          return `${attrString}[${key}]`;
        }
        return attrString;
      }, "")) ?? "";
    default:
      throw new Error(`Unsupported mode: ${mode} sent to validation key factory`);
  }
}
function isMyKeyChainItem(itemKey, ...propertyKeyChains) {
  if (!itemKey) {
    return true;
  }
  return propertyKeyChains.some((propertyKeyChain) => {
    return propertyKeyChain === itemKey || itemKey.indexOf(`${propertyKeyChain}.`) === 0 || itemKey.indexOf(`${propertyKeyChain}[`) === 0;
  });
}
exports.childKeyChainStringFromParent = childKeyChainStringFromParent;
exports.isArrayValue = isArrayValue;
exports.isMyKeyChainItem = isMyKeyChainItem;
exports.keyStringFromKeyChain = keyStringFromKeyChain;
exports.mergeDeepFromKeyChain = mergeDeepFromKeyChain;
exports.valueByKeyChain = valueByKeyChain;
//# sourceMappingURL=keyChain.cjs.map
