{"version":3,"file":"masthead-zip-form-21415adc.js","sources":["../../../../src/scripts/react-components/contractor-locator-listing/masthead-zip-form.tsx"],"sourcesContent":["import React, { useState, useEffect, useRef } from 'react';\nimport ZipForm from '../zip-form';\nimport {\n  GeolocationType,\n  COOKIE_NAMES,\n  secureStorage\n} from '../../helpers/global-storage';\nimport { MASTHEAD_ZIP_FORM_EVENTS } from './helpers';\n\nimport { getStoredGeolocationData } from '../../helpers/geolocation';\n\ntype MastheadZipFormProps = {\n  zipInvalidMessage: string;\n  zipRegex: string;\n  zipPlaceholders: string;\n  submitLabel: string;\n  defaultCountryValue: string;\n  redirectUrl: string;\n  commercialUrl: string;\n};\n\nfunction MastheadZipForm({\n  zipInvalidMessage,\n  zipRegex,\n  zipPlaceholders,\n  submitLabel,\n  defaultCountryValue,\n  redirectUrl,\n  commercialUrl\n}: MastheadZipFormProps) {\n  const regex = JSON.parse(zipRegex);\n  const placeholders = JSON.parse(zipPlaceholders);\n\n  const [ready, setReady] = useState(false);\n  const [locationData, setLocationData] = useState<GeolocationType | null>(\n    null\n  );\n\n  const containerRef = useRef<HTMLDivElement>(null);\n\n  const fillLocationData = async () => {\n    const location = getStoredGeolocationData();\n\n    if (location) {\n      setLocationData(location);\n    }\n  };\n\n  const getNextStep = () => {\n    let nextStep = null;\n    let currentStep = null;\n\n    if (containerRef.current) {\n      const parent = containerRef.current.closest('.tabbed-container__panel');\n\n      if (parent) {\n        currentStep = parent.querySelector(\n          '.tabbed-container__panel-content--gated'\n        ) as HTMLElement;\n\n        nextStep = parent.querySelector(\n          '.tabbed-container__panel-content--ungated'\n        ) as HTMLElement;\n      }\n    }\n\n    return { currentStep, nextStep };\n  };\n\n  useEffect(() => {\n    const onReady = () => setReady(true);\n    window.addEventListener(MASTHEAD_ZIP_FORM_EVENTS.ready, onReady);\n    window.addEventListener('commercialChange', e =>\n      handleRedirectCommercial(e as CustomEvent)\n    );\n\n    return () => {\n      window.removeEventListener(MASTHEAD_ZIP_FORM_EVENTS.ready, onReady);\n      window.removeEventListener('commercialChange', e =>\n        handleRedirectCommercial(e as CustomEvent)\n      );\n    };\n  }, []);\n\n  useEffect(() => {\n    fillLocationData();\n    const { currentStep, nextStep } = getNextStep();\n\n    if (currentStep && nextStep) {\n      if (ready) {\n        currentStep.classList.add('tabbed-container__panel-content--hidden');\n        nextStep.classList.remove('tabbed-container__panel-content--hidden');\n      } else {\n        currentStep.classList.remove('tabbed-container__panel-content--hidden');\n        nextStep.classList.add('tabbed-container__panel-content--hidden');\n      }\n    }\n  }, [ready]);\n\n  const handleRedirectCommercial = (e: CustomEvent) => {\n    if (e.detail && !window.location.href.includes(commercialUrl)) {\n      window.location.href = commercialUrl;\n    } else if (!e.detail && !window.location.href.includes(redirectUrl)) {\n      window.location.href = redirectUrl;\n    }\n  };\n\n  const handleSubmit = (zip: string, country: string) => {\n    if (zip && country) {\n      const location = {\n        address: '',\n        zip,\n        country,\n        latitude: '',\n        longitude: ''\n      };\n\n      secureStorage.set(COOKIE_NAMES.geolocation, JSON.stringify(location));\n\n      window.location.href = `${window.location.origin}${redirectUrl}?postalCode=${zip}&countryCode=${country}&user=true`;\n      setReady(true);\n      window.dispatchEvent(new CustomEvent('zipFormSubmit'));\n    } else {\n      setReady(false);\n    }\n  };\n\n  return (\n    <div className=\"masthead-zip-form__wrapper\" ref={containerRef}>\n      {!ready && (\n        <ZipForm\n          defaultCountryValue={\n            locationData?.country\n              ? locationData.country.toLowerCase()\n              : defaultCountryValue.toLowerCase()\n          }\n          defaultZipValue={locationData?.zip ? locationData.zip : ''}\n          invalidZipMessage={zipInvalidMessage}\n          zipRegex={regex}\n          zipPlaceholders={placeholders}\n          onSubmit={handleSubmit}\n          submitLabel={submitLabel}\n          withSubmitButton={true}\n        />\n      )}\n    </div>\n  );\n}\n\nexport default MastheadZipForm;\n"],"names":["MastheadZipForm","zipInvalidMessage","zipRegex","zipPlaceholders","submitLabel","defaultCountryValue","redirectUrl","commercialUrl","regex","placeholders","ready","setReady","useState","locationData","setLocationData","containerRef","useRef","fillLocationData","location","getStoredGeolocationData","getNextStep","nextStep","currentStep","parent","useEffect","onReady","MASTHEAD_ZIP_FORM_EVENTS","e","handleRedirectCommercial","handleSubmit","zip","country","secureStorage","COOKIE_NAMES","React","ZipForm"],"mappings":"kSAqBA,SAASA,EAAgB,CACvB,kBAAAC,EACA,SAAAC,EACA,gBAAAC,EACA,YAAAC,EACA,oBAAAC,EACA,YAAAC,EACA,cAAAC,CACF,EAAyB,CACjB,MAAAC,EAAQ,KAAK,MAAMN,CAAQ,EAC3BO,EAAe,KAAK,MAAMN,CAAe,EAEzC,CAACO,EAAOC,CAAQ,EAAIC,WAAS,EAAK,EAClC,CAACC,EAAcC,CAAe,EAAIF,EAAA,SACtC,IAAA,EAGIG,EAAeC,SAAuB,IAAI,EAE1CC,EAAmB,SAAY,CACnC,MAAMC,EAAWC,IAEbD,GACFJ,EAAgBI,CAAQ,CAC1B,EAGIE,EAAc,IAAM,CACxB,IAAIC,EAAW,KACXC,EAAc,KAElB,GAAIP,EAAa,QAAS,CACxB,MAAMQ,EAASR,EAAa,QAAQ,QAAQ,0BAA0B,EAElEQ,IACFD,EAAcC,EAAO,cACnB,yCAAA,EAGFF,EAAWE,EAAO,cAChB,2CAAA,GAKC,MAAA,CAAE,YAAAD,EAAa,SAAAD,EAAS,EAGjCG,EAAAA,UAAU,IAAM,CACR,MAAAC,EAAU,IAAMd,EAAS,EAAI,EAC5B,cAAA,iBAAiBe,EAAyB,MAAOD,CAAO,EACxD,OAAA,iBAAiB,mBAAoBE,GAC1CC,EAAyBD,CAAgB,CAAA,EAGpC,IAAM,CACJ,OAAA,oBAAoBD,EAAyB,MAAOD,CAAO,EAC3D,OAAA,oBAAoB,mBAAoBE,GAC7CC,EAAyBD,CAAgB,CAAA,CAC3C,CAEJ,EAAG,CAAE,CAAA,EAELH,EAAAA,UAAU,IAAM,CACGP,IACjB,KAAM,CAAE,YAAAK,EAAa,SAAAD,CAAS,EAAID,EAAY,EAE1CE,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