{"version":3,"file":"reduceObjectFormat.cjs","names":[],"sources":["../../../src/utils/reduceObjectFormat.ts"],"sourcesContent":["export type Primitive = string | number | boolean | null | undefined;\n\nexport type Recursive =\n  | Primitive\n  | { [key: string]: Recursive }\n  | Array<Recursive>;\n\n/**\n * Reduce an object to only the shape provided by a format object.\n * Values are always taken from the source object; the format is used only for structure.\n *\n * Examples:\n * reduceObjectFormat({ a: 1, b: 2 }, { a: 0 }) => { a: 1 }\n * reduceObjectFormat({ a: { x: 1, y: 2 } }, { a: { x: 0 } }) => { a: { x: 1 } }\n */\nexport const reduceObjectFormat = (\n  source: Recursive,\n  format: Recursive\n): Recursive => {\n  // If the format is an array, reduce each element by its counterpart in source\n  if (Array.isArray(format)) {\n    const sourceArray = Array.isArray(source) ? source : [];\n    return format.map((formatItem, index) =>\n      reduceObjectFormat(sourceArray[index], formatItem)\n    );\n  }\n\n  // If the format is an object (and not null), pick matching keys and recurse\n  if (typeof format === 'object' && format !== null) {\n    const result: Record<string, Recursive> = {};\n    const sourceObject =\n      typeof source === 'object' && source !== null && !Array.isArray(source)\n        ? (source as Record<string, Recursive>)\n        : ({} as Record<string, Recursive>);\n\n    for (const key of Object.keys(format)) {\n      const nextSource = sourceObject[key];\n      const nextFormat = (format as Record<string, Recursive>)[key];\n      result[key] = reduceObjectFormat(nextSource, nextFormat);\n    }\n    return result;\n  }\n\n  // For primitives in the format, simply return the source value (can be undefined)\n  return source as Primitive;\n};\n\n/**\n * Returns the subset of source whose keys are NOT present in the format object.\n * Inverse of reduceObjectFormat.\n *\n * Null values in source are always included as explicit fallback markers\n * (they signal \"use default locale\" at render time).\n *\n * Examples:\n * excludeObjectFormat({ a: 1, b: 2 }, { b: 0 }) => { a: 1 }\n * excludeObjectFormat({ a: { x: 1, y: 2 } }, { a: { x: 0 } }) => { a: { y: 2 } }\n * excludeObjectFormat({ a: null, b: 2 }, { b: 0 }) => { a: null }\n */\nexport const excludeObjectFormat = (\n  source: Recursive,\n  format: Recursive\n): Recursive | undefined => {\n  // null in source = explicit fallback marker → always include\n  if (source === null) return null;\n\n  // No format means nothing is translated yet → include all source\n  if (format === undefined || format === null) return source;\n\n  // Both plain objects → recurse key-by-key\n  if (\n    typeof source === 'object' &&\n    !Array.isArray(source) &&\n    typeof format === 'object' &&\n    !Array.isArray(format)\n  ) {\n    const result: Record<string, Recursive> = {};\n    let hasContent = false;\n\n    for (const key of Object.keys(source as Record<string, Recursive>)) {\n      const excluded = excludeObjectFormat(\n        (source as Record<string, Recursive>)[key],\n        (format as Record<string, Recursive>)[key]\n      );\n      if (excluded !== undefined) {\n        result[key] = excluded;\n        hasContent = true;\n      }\n    }\n\n    return hasContent ? result : undefined;\n  }\n\n  // Arrays → recurse by index\n  if (Array.isArray(source)) {\n    const formatArr = Array.isArray(format) ? format : [];\n    const result: Recursive[] = [];\n\n    for (let i = 0; i < source.length; i++) {\n      const excluded = excludeObjectFormat(source[i], formatArr[i]);\n      if (excluded !== undefined) {\n        result.push(excluded);\n      }\n    }\n\n    return result.length > 0 ? result : undefined;\n  }\n\n  // Primitive in source and format has a value → already translated → exclude\n  return undefined;\n};\n"],"mappings":";;;;;;;;;;;AAeA,MAAa,sBACX,QACA,WACc;CAEd,IAAI,MAAM,QAAQ,MAAM,GAAG;EACzB,MAAM,cAAc,MAAM,QAAQ,MAAM,IAAI,SAAS,CAAC;EACtD,OAAO,OAAO,KAAK,YAAY,UAC7B,mBAAmB,YAAY,QAAQ,UAAU,CACnD;CACF;CAGA,IAAI,OAAO,WAAW,YAAY,WAAW,MAAM;EACjD,MAAM,SAAoC,CAAC;EAC3C,MAAM,eACJ,OAAO,WAAW,YAAY,WAAW,QAAQ,CAAC,MAAM,QAAQ,MAAM,IACjE,SACA,CAAC;EAER,KAAK,MAAM,OAAO,OAAO,KAAK,MAAM,GAAG;GACrC,MAAM,aAAa,aAAa;GAChC,MAAM,aAAc,OAAqC;GACzD,OAAO,OAAO,mBAAmB,YAAY,UAAU;EACzD;EACA,OAAO;CACT;CAGA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