{"version":3,"file":"objects.mjs","sources":["../../../admin/src/utils/objects.ts"],"sourcesContent":["import clone from 'lodash/clone';\nimport toPath from 'lodash/toPath';\n\n/**\n * Deeply get a value from an object via its path.\n */\nexport function getIn(obj: any, key: string | string[], def?: any, pathStartIndex: number = 0) {\n  const path = toPath(key);\n  while (obj && pathStartIndex < path.length) {\n    obj = obj[path[pathStartIndex++]];\n  }\n\n  // check if path is not in the end\n  if (pathStartIndex !== path.length && !obj) {\n    return def;\n  }\n\n  return obj === undefined ? def : obj;\n}\n\n/** @internal is the given object an Object? */\nexport const isObject = (obj: any): obj is object =>\n  obj !== null && typeof obj === 'object' && !Array.isArray(obj);\n\n/** @internal is the given object an integer? */\nexport const isInteger = (obj: any): boolean => String(Math.floor(Number(obj))) === obj;\n\n/**\n * Deeply set a value from in object via its path. If the value at `path`\n * has changed, return a shallow copy of obj with `value` set at `path`.\n * If `value` has not changed, return the original `obj`.\n *\n * Existing objects / arrays along `path` are also shallow copied. Sibling\n * objects along path retain the same internal js reference. Since new\n * objects / arrays are only created along `path`, we can test if anything\n * changed in a nested structure by comparing the object's reference in\n * the old and new object, similar to how russian doll cache invalidation\n * works.\n *\n * In earlier versions of this function, which used cloneDeep, there were\n * issues whereby settings a nested value would mutate the parent\n * instead of creating a new object. `clone` avoids that bug making a\n * shallow copy of the objects along the update path\n * so no object is mutated in place.\n *\n * Before changing this function, please read through the following\n * discussions.\n *\n * @see https://github.com/developit/linkstate\n * @see https://github.com/jaredpalmer/formik/pull/123\n */\nexport function setIn(obj: any, path: string, value: any): any {\n  const res: any = clone(obj); // this keeps inheritance when obj is a class\n  let resVal: any = res;\n  let i = 0;\n  const pathArray = toPath(path);\n\n  for (; i < pathArray.length - 1; i++) {\n    const currentPath: string = pathArray[i];\n    const currentObj: any = getIn(obj, pathArray.slice(0, i + 1));\n\n    if (currentObj && (isObject(currentObj) || Array.isArray(currentObj))) {\n      resVal = resVal[currentPath] = clone(currentObj);\n    } else {\n      const nextPath: string = pathArray[i + 1];\n      resVal = resVal[currentPath] = isInteger(nextPath) && Number(nextPath) >= 0 ? [] : {};\n    }\n  }\n\n  // Return original object if new value is the same as current\n  if ((i === 0 ? obj : resVal)[pathArray[i]] === value) {\n    return obj;\n  }\n\n  if (value === undefined) {\n    delete resVal[pathArray[i]];\n  } else {\n    resVal[pathArray[i]] = value;\n  }\n\n  // If the path array has a single element, the loop did not run.\n  // Deleting on `resVal` had no effect in this scenario, so we delete on the result instead.\n  if (i === 0 && value === undefined) {\n    delete res[pathArray[i]];\n  }\n\n  return res;\n}\n"],"names":["getIn","obj","key","def","pathStartIndex","path","toPath","length","undefined","isObject","Array","isArray","isInteger","String","Math","floor","Number","setIn","value","res","clone","resVal","i","pathArray","currentPath","currentObj","slice","nextPath"],"mappings":";;;AAGA;;IAGO,SAASA,KAAAA,CAAMC,GAAQ,EAAEC,GAAsB,EAAEC,GAAS,EAAEC,cAAAA,GAAyB,CAAC,EAAA;AAC3F,IAAA,MAAMC,OAAOC,MAAAA,CAAOJ,GAAAA,CAAAA;AACpB,IAAA,MAAOD,GAAAA,IAAOG,cAAAA,GAAiBC,IAAAA,CAAKE,MAAM,CAAE;AAC1CN,QAAAA,GAAAA,GAAMA,GAAG,CAACI,IAAI,CAACD,iBAAiB,CAAC;AACnC,IAAA;;AAGA,IAAA,IAAIA,cAAAA,KAAmBC,IAAAA,CAAKE,MAAM,IAAI,CAACN,GAAAA,EAAK;QAC1C,OAAOE,GAAAA;AACT,IAAA;IAEA,OAAOF,GAAAA,KAAQO,YAAYL,GAAAA,GAAMF,GAAAA;AACnC;AAEA,gDACO,MAAMQ,QAAAA,GAAW,CAACR,GAAAA,GACvBA,GAAAA,KAAQ,IAAA,IAAQ,OAAOA,QAAQ,QAAA,IAAY,CAACS,KAAAA,CAAMC,OAAO,CAACV,GAAAA;AAE5D,iDACO,MAAMW,SAAAA,GAAY,CAACX,GAAAA,GAAsBY,MAAAA,CAAOC,IAAAA,CAAKC,KAAK,CAACC,MAAAA,CAAOf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