{"version":3,"file":"useLink-XcKPsgg3.js","sources":["../../../../../../../../app/frontend/shared/ui/Link/useLink.ts"],"sourcesContent":["import { router, usePage } from '@inertiajs/react'\nimport { useCallback, useMemo, useState } from 'react'\nimport { useTranslation } from 'react-i18next'\n\nimport { REGIONS_SEARCH } from '~/shared/constants/regions'\n\nconst regionsValues: string[] = REGIONS_SEARCH.map((e) => e.value)\n\nconst useLocalizedLink = ({ url, partialMatch }: { url: string; partialMatch?: boolean }) => {\n const page = usePage()\n const [origin] = useState(page.props.origin)\n\n const { i18n } = useTranslation()\n const { url: siteUrl } = usePage()\n\n const contractUrl = useCallback(\n (url: string) => {\n if (origin) {\n let parsedUrl: URL\n const locale = i18n.language\n\n try {\n parsedUrl = new URL(url)\n } catch {\n parsedUrl = new URL(url, `${origin}/`)\n }\n const pathParts = parsedUrl.pathname.split('/').filter(Boolean)\n\n if (pathParts.length > 0 && ['en', ...regionsValues].includes(pathParts[0])) {\n pathParts[0] = locale\n } else {\n pathParts.unshift(locale)\n }\n\n parsedUrl.pathname = '/' + pathParts.join('/')\n\n return parsedUrl\n }\n },\n [i18n.language, origin]\n )\n\n const isActive = useMemo(() => {\n const parsedUrl = contractUrl(url)\n if (partialMatch) {\n return siteUrl.includes(parsedUrl?.pathname + parsedUrl?.search + parsedUrl?.hash)\n }\n return (\n siteUrl === parsedUrl?.pathname + parsedUrl?.search + parsedUrl?.hash ||\n siteUrl === parsedUrl?.href\n )\n }, [contractUrl, partialMatch, siteUrl, url])\n\n const localizedLink = useMemo(() => {\n const parsedUrl = contractUrl(url)\n if (parsedUrl) {\n return parsedUrl.pathname.toString() + parsedUrl.search + parsedUrl.hash\n }\n }, [contractUrl, url])\n\n const handleRedirect = useCallback(() => {\n if (localizedLink) {\n router.replace(localizedLink)\n }\n }, [localizedLink])\n\n return {\n localizedLink,\n origin,\n handleRedirect,\n isActive,\n }\n}\n\nexport default useLocalizedLink\n"],"names":["regionsValues","REGIONS_SEARCH","e","useLocalizedLink","url","partialMatch","page","usePage","origin","useState","i18n","useTranslation","siteUrl","contractUrl","useCallback","parsedUrl","locale","pathParts","isActive","useMemo","localizedLink","handleRedirect","router"],"mappings":"oIAMA,MAAMA,EAA0BC,EAAe,IAAKC,GAAMA,EAAE,KAAK,EAE3DC,EAAmB,CAAC,CAAE,IAAAC,EAAK,aAAAC,KAA4D,CAC3F,MAAMC,EAAOC,IACP,CAACC,CAAM,EAAIC,EAAAA,SAASH,EAAK,MAAM,MAAM,EAErC,CAAE,KAAAI,GAASC,IACX,CAAE,IAAKC,CAAQ,EAAIL,EAAQ,EAE3BM,EAAcC,EAAA,YACjBV,GAAgB,CACf,GAAII,EAAQ,CACN,IAAAO,EACJ,MAAMC,EAASN,EAAK,SAEhB,GAAA,CACUK,EAAA,IAAI,IAAIX,CAAG,CAAA,MACjB,CACNW,EAAY,IAAI,IAAIX,EAAK,GAAGI,CAAM,GAAG,CACvC,CACA,MAAMS,EAAYF,EAAU,SAAS,MAAM,GAAG,EAAE,OAAO,OAAO,EAE9D,OAAIE,EAAU,OAAS,GAAK,CAAC,KAAM,GAAGjB,CAAa,EAAE,SAASiB,EAAU,CAAC,CAAC,EACxEA,EAAU,CAAC,EAAID,EAEfC,EAAU,QAAQD,CAAM,EAG1BD,EAAU,SAAW,IAAME,EAAU,KAAK,GAAG,EAEtCF,CACT,CACF,EACA,CAACL,EAAK,SAAUF,CAAM,CAAA,EAGlBU,EAAWC,EAAAA,QAAQ,IAAM,CACvB,MAAAJ,EAAYF,EAAYT,CAAG,EACjC,OAAIC,EACKO,EAAQ,UAASG,GAAA,YAAAA,EAAW,WAAWA,GAAA,YAAAA,EAAW,SAASA,GAAA,YAAAA,EAAW,KAAI,EAGjFH,KAAYG,GAAA,YAAAA,EAAW,WAAWA,GAAA,YAAAA,EAAW,SAASA,GAAA,YAAAA,EAAW,OACjEH,KAAYG,GAAA,YAAAA,EAAW,OAExB,CAACF,EAAaR,EAAcO,EAASR,CAAG,CAAC,EAEtCgB,EAAgBD,EAAA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