{"version":3,"file":"merge-props.cjs","names":[],"sources":["../../src/utils/merge-props.ts"],"sourcesContent":["/* eslint-disable quotes */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable @typescript-eslint/no-unsafe-function-type */\ntype UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (\n  k: infer I\n) => void\n  ? I\n  : never;\n\nfunction pushProp(\n  target: { [key: string]: any },\n  key: string,\n  value: unknown[]\n): void {\n  if (key === \"className\") {\n    target.className = [target.className, value].join(\" \").trim();\n  } else if (key === \"style\") {\n    target.style = { ...target.style, ...value };\n  } else if (typeof value === \"function\") {\n    const oldFn = target[key] as Function | undefined;\n    target[key] = oldFn\n      ? (...args: unknown[]) => {\n          oldFn(...args);\n          (value as Function)(...args);\n        }\n      : value;\n  } else if (\n    // skip merging undefined values\n    value === undefined ||\n    // skip if both value are the same primitive value\n    (typeof value !== \"object\" && value === target[key])\n  ) {\n    return;\n  } else if (!(key in target)) {\n    target[key] = value;\n  } else {\n    throw new Error(\n      `Didn’t know how to merge prop '${key}'. ` +\n        `Only 'className', 'style', and event handlers are supported`\n    );\n  }\n}\n\ntype MergedProps<T extends object[]> = {\n  [K in keyof UnionToIntersection<T[number]>]: K extends \"className\"\n    ? string\n    : K extends \"style\"\n      ? UnionToIntersection<T[number]>[K]\n      : Exclude<Extract<T[number], { [Q in K]: unknown }>[K], undefined>;\n};\n\n/** -------------------------------------------------------------------\n * * ***Merges multiple props objects into a single props object with\n * predictable and React-friendly merging behavior.***\n * --------------------------------------------------------------------\n *\n * This utility is designed to safely merge component props where certain\n * keys require special handling instead of simple overwrites.\n *\n * - **Behavior:**\n *    - Concatenates duplicate `className` values with a single space\n *    - Shallow-merges duplicate `style` objects (later props take precedence)\n *    - Chains duplicate function values (commonly event handlers),\n *      executing them from left to right\n *    - Skips merging `undefined` values\n *    - Preserves identical primitive values without reassigning\n *    - Throws an error for unsupported duplicate keys\n *\n * Later props in the argument list always have higher precedence\n * when conflicts occur.\n *\n * This makes the function especially suitable for **React / JSX props\n * composition**, such as combining props from hooks, components,\n * and user overrides.\n *\n * @typeParam T - A tuple of props object types to be merged.\n * @param props - One or more props objects. Later objects override\n * earlier ones when applicable.\n *\n * @returns A single merged props object with correctly inferred types,\n * including special handling for `className` and `style`.\n *\n * @throws {Error} If duplicate keys other than `className`, `style`,\n * or function values are encountered.\n *\n * @example\n * ```ts\n * mergeProps(\n *   { className: \"btn\", onClick: () => console.log(\"a\") },\n *   { className: \"btn-primary\", onClick: () => console.log(\"b\") }\n * );\n * // => {\n * //   className: \"btn btn-primary\",\n * //   onClick: () => { a(); b(); }\n * // }\n * ```\n *\n * @example\n * ```ts\n * mergeProps(\n *   { style: { color: \"red\", fontSize: 12 } },\n *   { style: { fontSize: 14 } }\n * );\n * // => { style: { color: \"red\", fontSize: 14 } }\n * ```\n */\nexport function mergeProps<T extends object[]>(...props: T): MergedProps<T> {\n  if (props.length === 1) {\n    return props[0] as MergedProps<T>;\n  }\n\n  return props.reduce((merged, ps: { [key: string]: any }) => {\n    for (const key in ps) {\n      pushProp(merged, key, ps[key]);\n    }\n    return merged;\n  }, {}) as MergedProps<T>;\n}\n"],"mappings":";;;;;;;;;;;;;AASA,SAAS,SACP,QACA,KACA,OACM;CACN,IAAI,QAAQ,aACV,OAAO,YAAY,CAAC,OAAO,WAAW,KAAK,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK;MACvD,IAAI,QAAQ,SACjB,OAAO,QAAQ;EAAE,GAAG,OAAO;EAAO,GAAG;CAAM;MACtC,IAAI,OAAO,UAAU,YAAY;EACtC,MAAM,QAAQ,OAAO;EACrB,OAAO,OAAO,SACT,GAAG,SAAoB;GACtB,MAAM,GAAG,IAAI;GACb,AAAC,MAAmB,GAAG,IAAI;EAC7B,IACA;CACN,OAAO,IAEL,UAAU,UAET,OAAO,UAAU,YAAY,UAAU,OAAO,MAE/C;MACK,IAAI,EAAE,OAAO,SAClB,OAAO,OAAO;MAEd,MAAM,IAAI,MACR,kCAAkC,IAAI,+DAExC;AAEJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiEA,SAAgB,WAA+B,GAAG,OAA0B;CAC1E,IAAI,MAAM,WAAW,GACnB,OAAO,MAAM;CAGf,OAAO,MAAM,QAAQ,QAAQ,OAA+B;EAC1D,KAAK,MAAM,OAAO,IAChB,SAAS,QAAQ,KAAK,GAAG,IAAI;EAE/B,OAAO;CACT,GAAG,CAAC,CAAC;AACP"}
