{"version":3,"file":"useSelectedOptionInUrlParam.mjs","names":[],"sources":["../../src/useSelectedOptionInUrlParam.tsx"],"sourcesContent":["import {useEffect} from 'react';\nimport {mapSelectedProductOptionToObject} from './getProductOptions.js';\nimport {SelectedOption} from './storefront-api-types.js';\n\n/** @publicDocs */\nexport function useSelectedOptionInUrlParam(\n  selectedOptions: Pick<SelectedOption, 'name' | 'value'>[],\n): null {\n  useEffect(() => {\n    const optionsSearchParams = new URLSearchParams(\n      mapSelectedProductOptionToObject(selectedOptions || []),\n    );\n    const currentSearchParams = new URLSearchParams(window.location.search);\n\n    // ts ignoring the URLSearchParams not iterable error for now\n    // https://stackoverflow.com/questions/72522489/urlsearchparams-not-accepting-string#answer-72522838\n    // TODO: update ts lib\n    const combinedSearchParams = new URLSearchParams({\n      // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n      // @ts-ignore\n      ...Object.fromEntries(currentSearchParams),\n      // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n      // @ts-ignore\n      ...Object.fromEntries(optionsSearchParams),\n    });\n\n    if (combinedSearchParams.size > 0) {\n      window.history.replaceState(\n        {},\n        '',\n        `${window.location.pathname}?${combinedSearchParams.toString()}`,\n      );\n    }\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [JSON.stringify(selectedOptions)]);\n\n  return null;\n}\n"],"mappings":";;;;AAKA,SAAgB,4BACd,iBACM;AACN,iBAAgB;EACd,MAAM,sBAAsB,IAAI,gBAC9B,iCAAiC,mBAAmB,EAAE,CAAC,CACxD;EACD,MAAM,sBAAsB,IAAI,gBAAgB,OAAO,SAAS,OAAO;EAKvE,MAAM,uBAAuB,IAAI,gBAAgB;GAG/C,GAAG,OAAO,YAAY,oBAAoB;GAG1C,GAAG,OAAO,YAAY,oBAAoB;GAC3C,CAAC;AAEF,MAAI,qBAAqB,OAAO,EAC9B,QAAO,QAAQ,aACb,EAAE,EACF,IACA,GAAG,OAAO,SAAS,SAAS,GAAG,qBAAqB,UAAU,GAC/D;IAGF,CAAC,KAAK,UAAU,gBAAgB,CAAC,CAAC;AAErC,QAAO"}