{"version":3,"sources":["webpack:///./app/components/m/search-slot/index.js","webpack:///./app/core/js/libs/GlobalCountrySearch.js"],"names":["form","document","querySelector","GlobalCountrySearch","create","apply","constructor","console","log","addEventListener","e","preventDefault","action","getAttribute","window","location","href","indexOf","GeoDetection","inject","then","_ref","country","newAction","replace","fetch","method","resp","status","langCountry","_findLangCountry","error","lang","enAction","_replaceLast","setAttribute","submit","catch","err","url","result","regex","m","exec","index","lastIndex","forEach","match","groupIndex","push","length","replaceAll","parts","split","string","search","lastIndexOf","slice"],"mappings":";;;;;;;;;;;AAAA;AAAA;AAA4E;;AAE5E;AACA;AACA,MAAMA,IAAI,GAAGC,QAAQ,CAACC,aAAa,CAAC,mBAAmB,CAAC;AACxDC,yEAAmB,CAACC,MAAM,CAACJ,IAAI,CAAC,CAACK,KAAK,EAAE,C;;;;;;;;;;;;ACLxC;AAAA;AAAA;AAA0C;AAE3B,MAAMF,mBAAmB,CAAC;EACvC;AACF;AACA;AACA;EACEG,WAAWA,CAACN,IAAI,EAAE;IAChB,IAAI,CAACA,IAAI,GAAGA,IAAI;EAClB;EAEA,OAAOI,MAAMA,CAACJ,IAAI,EAAE;IAClB,OAAO,IAAIG,mBAAmB,CAACH,IAAI,CAAC;EACtC;EAEAK,KAAKA,CAAA,EAAG;IACN,IAAI,IAAI,CAACL,IAAI,KAAK,IAAI,EAAE;MACtBO,OAAO,CAACC,GAAG,CAAC,qCAAqC,CAAC;MAClD;IACF;IAEA,IAAI,CAACR,IAAI,CAACS,gBAAgB,CAAC,QAAQ,EAAGC,CAAC,IAAK;MAC1CA,CAAC,CAACC,cAAc,EAAE;MAClB;MACA;MACA,IAAIC,MAAM,GAAG,IAAI,CAACZ,IAAI,CAACa,YAAY,CAAC,QAAQ,CAAC;MAC7C,IAAIC,MAAM,CAACC,QAAQ,CAACC,IAAI,CAACC,OAAO,CAAC,WAAW,CAAC,GAAG,CAAC,EAC/CL,MAAM,GAAG,yDAAyD;MAEpEM,qDAAY,CAACd,MAAM,E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MAAM,GAAG,EAAE;IACjB,MAAMC,KAAK,GAAG,uBAAuB;IACrC,IAAIC,CAAC;IAEL,OAAO,CAACA,CAAC,GAAGD,KAAK,CAACE,IAAI,CAACJ,GAAG,CAAC,MAAM,IAAI,EAAE;MACrC;MACA,IAAIG,CAAC,CAACE,KAAK,KAAKH,KAAK,CAACI,SAAS,EAAE;QAC/BJ,KAAK,CAACI,SAAS,EAAE;MACnB;MAEAH,CAAC,CAACI,OAAO,CAAC,CAACC,KAAK,EAAEC,UAAU,KAAK;QAC/BR,MAAM,CAACS,IAAI,CAACF,KAAK,CAAC;MACpB,CAAC,CAAC;IACJ;IAEA,IAAIP,MAAM,CAACU,MAAM,KAAK,CAAC,EAAE,OAAO,IAAI;;IAEpC;IACA,MAAMrB,WAAW,GAAGW,MAAM,CAAC,CAAC,CAAC,CAACW,UAAU,CAAC,GAAG,EAAE,EAAE,CAAC;IACjD,MAAMC,KAAK,GAAGvB,WAAW,CAACwB,KAAK,CAAC,GAAG,CAAC;IAEpC,OAAO;MACLrB,IAAI,EAAEoB,KAAK,CAAC,CAAC,CAAC;MACd9B,OAAO,EAAE8B,KAAK,CAAC,CAAC;IAClB,CAAC;EACH;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACElB,YAAYA,CAACoB,MAAM,EAAEC,MAAM,EAAE/B,OAAO,EAAE;IACpC,MAAMoB,KAAK,GAAGU,MAAM,CAACE,WAAW,CAACD,MAAM,CAAC;IACxC,OAAOD,MAAM,CAACG,KAAK,CAAC,CAAC,EAAEb,KAAK,CAAC,GAAGU,MAAM,CAACG,KAAK,CAACb,KAAK,CAAC,CAACpB,OAAO,CAAC+B,MAAM,EAAE/B,OAAO,CAAC;EAC9E;AACF,C","file":"search-slot.chunk.js","sourcesContent":["import GlobalCountrySearch from '../../../core/js/libs/GlobalCountrySearch';\r\n\r\n// apply global country search, to update form action if a\r\n// user are searching via the global country search-front\r\nconst form = document.querySelector('.search-slot form');\r\nGlobalCountrySearch.create(form).apply();\r\n","import GeoDetection from './GeoDetection';\r\n\r\nexport default class GlobalCountrySearch {\r\n  /**\r\n   *\r\n   * @param form\r\n   */\r\n  constructor(form) {\r\n    this.form = form;\r\n  }\r\n\r\n  static create(form) {\r\n    return new GlobalCountrySearch(form);\r\n  }\r\n\r\n  apply() {\r\n    if (this.form === null) {\r\n      console.log('No form instance given. Aborting...');\r\n      return;\r\n    }\r\n\r\n    this.form.addEventListener('submit', (e) => {\r\n      e.preventDefault();\r\n      // get current form-action value. If on localhost simulate\r\n      // live url\r\n      let action = this.form.getAttribute('action');\r\n      if (window.location.href.indexOf('localhost') > 0)\r\n        action = 'https://www.wirtgen-group.com/searchfrontend/es-global/';\r\n\r\n      GeoDetection.create()\r\n        .inject()\r\n        .then(({ country }) => {\r\n          //\r\n          const newAction = action.replace('global', country);\r\n\r\n          // perform a head request to see if the global\r\n          // country exists. if not use 'en' language\r\n          // as fallback\r\n          fetch(newAction, { method: 'HEAD' })\r\n            .then((resp) => {\r\n              if (resp.status !== 200) {\r\n                // get lang from current url so were able\r\n                // to replace it with the fallback version\r\n                const langCountry = this._findLangCountry(newAction);\r\n                if (langCountry === null) {\r\n                  console.error('Could not determine lang/country for url: ', newAction);\r\n                  return;\r\n                }\r\n                // replace lang value\r\n                const { lang } = langCountry;\r\n                const enAction = this._replaceLast(newAction, lang, 'en');\r\n\r\n                // update form-action and submit form\r\n                this.form.setAttribute('action', enAction);\r\n                this.form.submit();\r\n                return;\r\n              }\r\n\r\n              // update form-action and submit form\r\n              this.form.setAttribute('action', newAction);\r\n              this.form.submit();\r\n            })\r\n            .catch((err) => console.error(err));\r\n        })\r\n        .catch((err) => {\r\n          console.error(err);\r\n        });\r\n    });\r\n  }\r\n\r\n  /**\r\n   * Try to find a lang/country pair within the given url.\r\n   * @param url       The url to search in\r\n   * @returns {*}     The lang/country values included within the url\r\n   * @private\r\n   */\r\n  _findLangCountry(url) {\r\n    const result = [];\r\n    const regex = /\\/[a-z]{2}-[a-z]{2}/gm;\r\n    let m;\r\n\r\n    while ((m = regex.exec(url)) !== null) {\r\n      // prevent infinite loops with zero-width matches\r\n      if (m.index === regex.lastIndex) {\r\n        regex.lastIndex++;\r\n      }\r\n\r\n      m.forEach((match, groupIndex) => {\r\n        result.push(match);\r\n      });\r\n    }\r\n\r\n    if (result.length === 0) return null;\r\n\r\n    // only return first match\r\n    const langCountry = result[0].replaceAll('/', '');\r\n    const parts = langCountry.split('-');\r\n\r\n    return {\r\n      lang: parts[0],\r\n      country: parts[1],\r\n    };\r\n  }\r\n\r\n  /**\r\n   * replaceLast will replace the last occurrence\r\n   * of search with replace within string\r\n   * @param string\r\n   * @param search\r\n   * @param replace\r\n   * @returns {*}\r\n   * @private\r\n   */\r\n  _replaceLast(string, search, replace) {\r\n    const index = string.lastIndexOf(search);\r\n    return string.slice(0, index) + string.slice(index).replace(search, replace);\r\n  }\r\n}\r\n"],"sourceRoot":""}