UNPKG

@salla.sa/twilight-components

Version:
4 lines 81 kB
/*! * Crafted with ❤ by Salla */ import{r as t,c as s,h as i,H as e,a as l}from"./p-CFtXUFT2.js";import{a,A as r}from"./p-Vqpj4CWE.js";import{L as n}from"./p-BO5AoBAP.js";import{S as h}from"./p-BscTeWDc.js";const o="BulletDeliveryAPI",d=t=>{const s=null==t?Number.NaN:Number(t);return!Number.isNaN(s)&&0!==s};async function c(t,s,i){try{return await t()}catch(t){return console.error(`${o}: ${i}`,t),s}}const u=new Map;function p(t){const s=u.get(t);return!s||Date.now()-s.timestamp>3e5?(u.delete(t),null):s.data}function b(t,s){u.set(t,{data:s,timestamp:Date.now()})}const y=t=>"SA"===String(t).toUpperCase(),v={async getCountries(t=!1,s){const i=s?.trim()?null:"countries_"+(t?1:0);if(i){const t=p(i);if(t)return t}const e=await c((async()=>{const i={for_branch:t?1:0};return t&&(i.for_allocation=1),s?.trim()&&(i.query=s.trim()),((await salla.api.request("shipping/countries",{params:i}))?.data??[]).map((t=>({id:t.id,code:t.code,name:t.name,has_regions:y(t.code)})))}),[],"Error getting countries");return i&&e.length>0&&b(i,e),e},async getRegions(t,s){if(!d(t))return console.warn(`${o}: getRegions called without valid country_id`),[];const i=s?.trim()?null:`regions_${t}`;if(i){const t=p(i);if(t)return t}const e=await c((async()=>{const i={};return s?.trim()&&(i.query=s.trim()),((await salla.api.request(`shipping/countries/${t}/regions`,{params:i}))?.data??[]).map((s=>({id:s.id,name:s.name,code:s.code,country_id:Number(t)})))}),[],"Error fetching regions");return i&&e.length>0&&b(i,e),e},async getCities(t,s,i){if(!d(t))return console.warn(`${o}: getCities called without valid country_id`),[];const e=i?.trim()?null:`cities_${t}_${s||"all"}`;if(e){const t=p(e);if(t)return t}const l=await c((async()=>{const e={for_branch:0,country_id:t};return s&&(e.region_id=s),i?.trim()&&(e.query=i.trim()),((await salla.api.request("shipping/cities",{params:e}))?.data??[]).map((s=>({id:s.id,name:s.name,country_id:t,...null!=s.region_id&&{region_id:s.region_id}})))}),[],"Error fetching cities");return e&&l.length>0&&b(e,l),l},getDistricts:async(t,s)=>d(t)?c((async()=>{const i={for_branch:0,city_id:t};s?.trim()&&(i.query=s.trim());const e=(await salla.api.request("shipping/districts",{params:i}))?.data;return(Array.isArray(e)?e:Array.isArray(e?.districts)?e.districts:[]).map((s=>({id:s.id,name:s.name,name_en:s.name_en,city_id:t})))}),[],"Error fetching districts"):(console.warn(`${o}: getDistricts called without valid city_id`),[]),getSavedAddresses:async()=>c((async()=>{const t=(await salla.api.request("address"))?.data;return Array.isArray(t)?t:[]}),[],"Error fetching user addresses"),async getBranches({query:t,lat:s,lng:i,country_id:e,per_page:l=20}={}){const a=t?.trim()?.length>=2||s||i||!e?null:`branches_${e}`;if(a){const t=p(a);if(t)return t}const r=await c((async()=>{const a={per_page:l};t?.trim().length>=2&&(a.query=t),s&&(a.lat=s),i&&(a.lng=i),e&&(a.country_id=e);const r=await salla.api.request("branches",{params:a});return Array.isArray(r?.data)?r.data:[]}),[],"Error fetching branches");return a&&r.length>0&&b(a,r),r},saveAddressLocation:async t=>c((async()=>{const s=await salla.api.request("address/location",{...t,for_allocation:!0},"post"),i=s?.data??s;return{success:!0,address:"object"==typeof i&&null!=i&&"id"in i?i:void 0}}),{success:!1},"Error saving address/location"),setDeliveryScope:async t=>c((async()=>(await salla.api.withoutNotifier((()=>salla.scope.change({id:t}))),salla.storage.set("scope",{...salla.storage.get("scope")||{},id:t}),!0)),!1,"Error setting delivery scope"),async allocateScope(t){try{const s=await salla.api.withoutNotifier((()=>salla.api.request("scopes/allocation",t,"post")));return{success:!0,data:s?.data??s}}catch(t){console.error(`${o}: Error allocating scope`,t);const i=t;return{success:!1,error:null!=i?.response?.data?(s=i.response.data,s?.error?.message??s?.message??"Failed to allocate scope"):i?.message??"Unknown error"}}var s}};function m(){u.clear()}const g=t=>t*(Math.PI/180);function f(t,s,i,e){const l=g(i-t),a=g(e-s),r=Math.sin(l/2)**2+Math.cos(g(t))*Math.cos(g(i))*Math.sin(a/2)**2;return 12742*Math.atan2(Math.sqrt(r),Math.sqrt(1-r))}function _(t,s){const[i,e]=String(t).trim().split(":").map(Number);return Number.isNaN(i)||Number.isNaN(e)?t:new Date(2e3,0,1,i,e).toLocaleTimeString(s,{hour:"numeric",minute:"2-digit",hour12:!0})}const S=(t,s,i)=>{if("branch"===t.type){const s=t.branch_details?.city??t.branch_details?.name;return s?`${i} ${s}`.trim():""}if("address"===t.type&&t.address_details){const i=[t.address_details.district?.name,t.address_details.city?.name,t.address_details.country?.name].filter(Boolean).join("، ")||t.address_details.short_address||"";return i?`${s} ${i}`.trim():""}return""},w=t=>null!=t?Number(t):void 0,C=t=>w(t?.address_details?.country?.id),k=t=>t?.country_code??t?.address_details?.country?.code??"",I=t=>w(t?.address_details?.region?.id),x=t=>w(t?.address_details?.city?.id),A=t=>w(t?.address_details?.district?.id),M=t=>t?.branch_details?.id,B=t=>t?.branch_details?.latitude,N=t=>t?.branch_details?.longitude,D=t=>{if(!t||"address"!==t.type||t.address_id)return!1;const s=C(t),i=x(t),e=A(t),l=k(t),a=null!=e&&0!==Number(e),r=Boolean(t.address_details?.district?.name?.trim()),n=!y(l)||a||r;return!!(s&&i&&n)},$=(t,s,i)=>!t||!y(t)||null!=s&&0!==Number(s)&&null!=i&&("number"==typeof i?0!==i:String(i).trim().length>0),L={[GeolocationPositionError.PERMISSION_DENIED]:"Location permission denied",[GeolocationPositionError.POSITION_UNAVAILABLE]:"Location unavailable",[GeolocationPositionError.TIMEOUT]:"Location timeout"};var T='<svg width="15" height="14" viewBox="0 0 15 14" fill="none" xmlns="http://www.w3.org/2000/svg">\n <path\n d="M1.14453 5.83032L1.14453 9.16486C1.14453 11.0511 1.14453 11.9943 1.73032 12.5803C2.3161 13.1663 3.25891 13.1663 5.14453 13.1663L9.14453 13.1663C11.0301 13.1663 11.9729 13.1663 12.5587 12.5803C13.1445 11.9943 13.1445 11.0511 13.1445 9.16486V5.83032"\n stroke="currentcolor" stroke-linecap="round" />\n <path\n d="M9.14453 10.1619C8.68846 10.5667 7.9624 10.8285 7.14453 10.8285C6.32666 10.8285 5.60059 10.5667 5.14453 10.1619"\n stroke="currentcolor" stroke-linecap="round" />\n <path\n d="M5.90256 4.44533C5.71455 5.12426 5.03086 6.29542 3.73185 6.46516C2.58488 6.61502 1.71497 6.11437 1.49277 5.90504C1.24779 5.7353 0.689441 5.19216 0.552703 4.85269C0.415966 4.51322 0.575492 3.77771 0.689441 3.47785L1.14495 2.15892C1.25615 1.82765 1.51646 1.04411 1.78334 0.779094C2.05022 0.514073 2.59054 0.502543 2.81294 0.502543L7.48329 0.502543C8.68543 0.519525 11.3139 0.491824 11.8335 0.502545C12.3532 0.513266 12.6654 0.948828 12.7565 1.13553C13.5318 3.01341 13.8333 4.08889 13.8333 4.54717C13.7321 5.03605 13.3133 5.95787 11.8335 6.36332C10.2956 6.7847 9.42363 5.96509 9.15007 5.65043M5.27015 5.65044C5.48665 5.91635 6.16578 6.45158 7.15029 6.46516C8.1348 6.47873 8.98485 5.79188 9.28681 5.44676C9.37227 5.34492 9.55687 5.04279 9.7483 4.44533"\n stroke="currentcolor" stroke-linecap="round" stroke-linejoin="round" />\n</svg>\n ';const E="salla-bullet-delivery-override-ip",P=class{constructor(i){t(this,i),this.bulletDeliveryConfirmed=s(this,"bulletDeliveryConfirmed"),this.bulletDeliveryClosed=s(this,"bulletDeliveryClosed"),this.addressCreated=s(this,"addressCreated"),this.headerContextUpdate=s(this,"headerContextUpdate"),this.confirming=!1,this.pendingCartSubmitResolver=null,this.pendingCartSubmitPromise=null,this.cartSubmitConfirmationPending=!1,this.branchSearchDebounceTimer=null,this.countrySearchTimer=null,this.regionSearchTimer=null,this.citySearchTimer=null,this.districtSearchTimer=null,this.citySearchCounter=0,this.componentReady=!1,this.pendingOpen=!1,this.tabChanging=!1,this.intentStorageKey="bullet_delivery_intent",this.sessionShownKey="bullet_delivery_shown",this.incompleteIntentPromptKey="bullet_delivery_incomplete_intent_prompted",this.cartItemAddedEvent="cart::item.added",this.cartItemAddedHandler=null,this.cartSubmittingHandler=null,this.useCartEventApi=!1,this.authLoggedInHandler=null,this.authLoggedOutHandler=null,this.loginClosedHandler=null,this.hasExplicitPreselectedIdsForOpen=!1,this.bulletDeliveryOpenHandler=t=>{this.preselectedAddressId=t?.preselected_address_id,this.preselectedBranchId=t?.preselected_branch_id,this.hasExplicitPreselectedIdsForOpen=null!=t?.preselected_address_id||null!=t?.preselected_branch_id,this.open()},this.closeHandler=null,this.bulletDeliveryMobileSelectHandler=null,this.savedAddressesLoaded=!1,this.activeTab="address",this.isLoggedIn=!1,this.viewMode="main",this.countries=[],this.regions=[],this.cities=[],this.districts=[],this.loadingRegions=!1,this.selectedCountry=null,this.selectedRegion=null,this.selectedCity=null,this.selectedDistrict=null,this.districtName="",this.savedAddresses=[],this.selectedSavedAddress=null,this.selectedSessionAddress=!1,this.branches=[],this.filteredBranches=[],this.selectedBranch=null,this.branchSearchQuery="",this.loadingCountries=!1,this.loadingCities=!1,this.loadingDistricts=!1,this.loadingBranches=!1,this.loadingNearestBranch=!1,this.locationError="",this.savingAddress=!1,this.loadingSavedAddresses=!1,this.showCartWillBeClearedBanner=!1,this.countrySearchQuery="",this.regionSearchQuery="",this.citySearchQuery="",this.districtSearchQuery="",this.searchingCountries=!1,this.searchingRegions=!1,this.searchingCities=!1,this.searchingDistricts=!1,this.displayedCountries=[],this.displayedRegions=[],this.displayedCities=[],this.displayedDistricts=[],this.allocationOutOfCoverageMessage=null,this.newAddressForm={...P.INITIAL_ADDRESS_FORM},this.handleCountrySearch=t=>{this.countrySearchQuery=t},this.handleRegionSearch=t=>{this.regionSearchQuery=t},this.handleCitySearch=t=>{if(this.citySearchQuery=t,this.citySearchTimer&&clearTimeout(this.citySearchTimer),!t.trim())return this.displayedCities=this.cities,void(this.searchingCities=!1);if(t.trim().length<P.DROPDOWN_SEARCH_MIN_CHARS)return;const s=++this.citySearchCounter;this.citySearchTimer=setTimeout((async()=>{this.searchingCities=!0;try{const i=y(this.selectedCountry?.code??"")?this.selectedRegion?.id:void 0,e=await v.getCities(this.selectedCountry?.id,i,t);s===this.citySearchCounter&&(this.displayedCities=e)}catch{s===this.citySearchCounter&&(this.displayedCities=this.cities)}finally{s===this.citySearchCounter&&(this.searchingCities=!1)}}),P.DROPDOWN_SEARCH_DEBOUNCE_MS)},this.handleDistrictSearch=t=>{this.districtSearchQuery=t},this.handleCountryDropdownClosed=()=>{this.countrySearchQuery="",this.displayedCountries=this.countries,this.searchingCountries=!1},this.handleRegionDropdownClosed=()=>{this.regionSearchQuery="",this.displayedRegions=this.regions,this.searchingRegions=!1},this.handleCityDropdownClosed=()=>{this.citySearchQuery="",this.citySearchTimer&&(clearTimeout(this.citySearchTimer),this.citySearchTimer=null),++this.citySearchCounter,this.displayedCities=this.cities,this.searchingCities=!1},this.handleDistrictDropdownClosed=()=>{this.districtSearchQuery="",this.displayedDistricts=this.districts,this.searchingDistricts=!1},this.handleSubmitAddAddress=async t=>{if(t.preventDefault(),!this.savingAddress&&this.selectedCountry&&this.selectedCity&&$(this.selectedCountry?.code,this.selectedRegion?.id,this.selectedDistrict?.id??(this.districtName?.trim()||null))){this.savingAddress=!0;try{const t=this.buildAddressLocationPayload(),{success:s,address:i}=await v.saveAddressLocation(t);if(s){await this.loadSavedAddresses();const t=i??this.savedAddresses[0]??null;t?.is_in_coverage&&(this.selectedSessionAddress=!1,this.selectedSavedAddress=t),this.addressCreated.emit({address:t}),this.viewMode="main",this.districtName=""}else Salla.notify?.error(Salla.lang.get("common.errors.error_occurred"))}catch(t){console.error("SallaBulletDelivery: Error saving address",t),Salla.notify?.error(Salla.lang.get("common.errors.error_occurred"))}finally{this.savingAddress=!1}}},this.handleCountrySelected=t=>{const s=this.countries.find((s=>String(s.id)===String(t.id)))||t;this.countrySearchQuery="",this.displayedCountries=this.countries,this.applyCountryChange(s||null)},this.handleRegionSelected=t=>{const s=this.regions.find((s=>String(s.id)===String(t.id)))||t;this.selectedRegion=s||null,this.regionSearchQuery="",this.displayedRegions=this.regions,this.updateNewAddressForm({region_id:s?.id,city_id:void 0,district_id:void 0,city:void 0,district:void 0}),s&&this.selectedCountry&&this.loadCities(this.selectedCountry.id,s.id)},this.handleCitySelected=t=>{const s=y(this.selectedCountry?.code??""),i=this.cities.find((s=>String(s.id)===String(t.id)))||t;this.selectedCity=i||null,this.selectedDistrict=null,this.districtName="",this.citySearchQuery="",this.displayedCities=this.cities,i&&(this.updateNewAddressForm({city:i,district:void 0}),s&&this.loadDistricts(i.id))},this.handleDistrictSelected=t=>{const s=this.districts.find((s=>String(s.id)===String(t.id)))||t;this.selectedDistrict=s||null,this.districtName="",this.districtSearchQuery="",this.displayedDistricts=this.districts,s&&this.updateNewAddressForm({district:s})}}get supportsPickup(){return Boolean(Salla.config.get("store.shipping.support_pickup"))||Boolean(Salla.config.get("store.support_pickup"))}get openingType(){return Salla.config.get("store.settings.bullet_delivery.settings.type")}get isRequired(){return!!Salla.config.get("store.settings.bullet_delivery.settings.is_required")}get isMobileApp(){return Salla.config.isMobileApp()}async open(){if(this.isMobileApp)Salla.event.dispatch("salla::bullet-delivery.open-sheet");else if(this.componentReady&&this.modal){this.resetState(),this.modal.open(),Salla.event.dispatch("salla::bullet-delivery.modal.opened");try{this.isLoggedIn=!this.isGuestUser();const t=this.cleanStaleGuestAddressIdIfNeeded(this.getStoredIntent());this.applyPreselectedIdsFromIntent(t);const s=!this.isLoggedIn&&"address"===t?.type&&null!=C(t)&&null!=x(t);if(this.activeTab=this.supportsPickup&&t?.type&&("address"===t.type||"branch"===t.type)?t.type:"address",this.isLoggedIn&&"address"===this.activeTab&&(this.loadingSavedAddresses=!0),await Promise.all([this.loadCountries(s),this.isLoggedIn&&"address"===this.activeTab?this.loadSavedAddresses().then((()=>{this.savedAddressesLoaded=!0})).finally((()=>{this.loadingSavedAddresses=!1})):Promise.resolve()]),this.isLoggedIn&&this.preselectedAddressId){const t=this.savedAddresses.find((t=>t.id===this.preselectedAddressId));t&&(this.selectedSessionAddress=!1,this.selectedSavedAddress=t)}if("branch"===this.activeTab){const s=B(t),i=N(t);if(null!=s&&null!=i?await this.loadBranchesWithLocation(s,i):await this.loadBranches(),this.preselectedBranchId){const t=this.branches.find((t=>t.id===this.preselectedBranchId));t&&(this.selectedBranch=t)}}await this.prefillFromSessionStorage()}catch(t){console.error("SallaBulletDelivery: Error loading data",t)}finally{this.overrideScopeSwitchUI()}}else this.pendingOpen=!0}async close(){if(!this.shouldForceNonClosable()||this.confirming)return this.cartSubmitConfirmationPending&&!this.confirming&&this.resolvePendingCartSubmit(),this.bulletDeliveryClosed.emit(),this.modal?.close()}resetState(){this.viewMode="main",this.selectedCountry=null,this.selectedRegion=null,this.selectedCity=null,this.selectedDistrict=null,this.districtName="",this.selectedSavedAddress=null,this.selectedSessionAddress=!1,this.selectedBranch=null,this.branchSearchQuery="",this.regions=[],this.cities=[],this.districts=[],this.branches=[],this.filteredBranches=[],this.newAddressForm={...P.INITIAL_ADDRESS_FORM},this.showCartWillBeClearedBanner=!1,this.allocationOutOfCoverageMessage=null,this.locationError="",this.savedAddressesLoaded=!1,this.loadingSavedAddresses=!1,this.loadingCountries=!1,this.resetSearchState()}getIntentStorage(){const t=Boolean(Salla.config.get("store.settings.bullet_delivery.settings.remember_last_session"));return Salla.storage[t?"store":"session"]}getShownStorage(){return Salla.storage.session}getStoredIntent(){return this.getIntentStorage().get(this.intentStorageKey)}isGuestUser(){return"guest"===String(Salla.config.get("user.type")??"guest")}cleanStaleGuestAddressIdIfNeeded(t){if(!this.isGuestUser()||"address"!==t?.type||!t.address_id)return t;const s={...t,address_id:void 0};return this.setStoredIntent(s),s}applyPreselectedIdsFromIntent(t){this.hasExplicitPreselectedIdsForOpen?this.hasExplicitPreselectedIdsForOpen=!1:(this.preselectedAddressId=void 0,this.preselectedBranchId=void 0,this.isGuestUser()?"branch"===t?.type&&t.branch_id&&(this.preselectedBranchId=t.branch_id):"address"===t?.type&&t.address_id?this.preselectedAddressId=t.address_id:"branch"===t?.type&&t.branch_id&&(this.preselectedBranchId=t.branch_id))}hasIncompleteUserAddressIntent(){if(this.isGuestUser())return!1;const t=this.getStoredIntent();return!(!t||"address"!==t.type||t.address_id||!t.address_details)&&D(t)}hasCompleteIntentForLoggedInUser(){if(this.isGuestUser())return!1;const t=this.getStoredIntent();return!!t&&("address"===t.type?!!t.address_id:"branch"===t.type&&!!t.branch_id)}shouldForceNonClosable(){return!this.isGuestUser()&&!!Salla.url.is_page("cart")&&!this.hasCompleteIntentForLoggedInUser()}shouldPromptIncompleteIntentThisSession(){const t=this.getShownStorage();return this.hasIncompleteUserAddressIntent()&&!0!==t.get(this.incompleteIntentPromptKey)}markIncompleteIntentPrompted(){this.getShownStorage().set(this.incompleteIntentPromptKey,!0)}setStoredIntent(t){this.getIntentStorage().set(this.intentStorageKey,t),this.overrideScopeSwitchUI()}clearBulletDeliveryStoredData(){Salla.storage.session.remove(this.intentStorageKey),Salla.storage.store.remove(this.intentStorageKey),Salla.storage.session.remove(this.sessionShownKey),Salla.storage.session.remove(this.incompleteIntentPromptKey),this.overrideScopeSwitchUI()}async prefillAddressFromStoredIntent(){const t=this.getStoredIntent();if(!t||"address"!==t.type||null==x(t))return;const s=C(t);if(!s)return;const i=this.countries.find((t=>String(t.id)===String(s)));if(!i)return;this.selectedCountry=i,this.updateNewAddressForm({country_id:Number(i.id)});const e=y(i.code),l=x(t),a=I(t),r=A(t),n=t.address_details?.district?.name,[h,o,d]=await Promise.all([e?v.getRegions(i.id):Promise.resolve([]),v.getCities(i.id,e&&null!=a?a:void 0),null!=l?v.getDistricts(Number(l)):Promise.resolve([])]);if(e&&(this.regions=h,this.displayedRegions=this.regions),this.cities=o,this.displayedCities=this.cities,this.districts=d,this.displayedDistricts=this.districts,this.loadingCities=!1,this.loadingRegions=!1,this.loadingDistricts=!1,e&&null!=a){const t=this.regions.find((t=>String(t.id)===String(a)));t&&(this.selectedRegion=t,this.updateNewAddressForm({region_id:t.id}))}if(null!=l&&this.cities.length>0){const t=this.cities.find((t=>String(t.id)===String(l)));t&&(this.selectedCity=t,this.updateNewAddressForm({city_id:t.id,city:t}))}if(this.districts.length>0){const t=this.findPrefillDistrict(r,n);t&&(this.updateNewAddressForm({district_id:t.id,district:t}),this.selectedDistrict=t)}!this.selectedDistrict&&n&&(this.districtName=n)}updateNewAddressForm(t){this.newAddressForm={...this.newAddressForm,...t}}hasBeenShownThisSession(){return!0===this.getShownStorage().get(this.sessionShownKey)}markShownThisSession(){this.getShownStorage().set(this.sessionShownKey,!0)}getIPLocationConfig(){const t="store.shipping.delivery_location",s={countryId:Salla.config.get(`${t}.country_id`),regionId:Salla.config.get(`${t}.region_id`),cityId:Salla.config.get(`${t}.city_id`),districtId:Salla.config.get(`${t}.district_id`)};return localStorage.getItem(E)?JSON.parse(localStorage.getItem(E)):s}async loadCountries(t=!1){this.loadingCountries=!0;const s="branch"===this.activeTab;try{this.countries=await v.getCountries(s),this.displayedCountries=this.countries;const{countryId:l,regionId:a,cityId:r,districtId:n}=this.getIPLocationConfig();i="getIPLocationConfig",e=this.getIPLocationConfig(),localStorage.getItem("salla-bullet-delivery-debug")&&(void 0!==e?console.log(i,e):console.log(i));const h=l&&this.countries.length>0?this.countries.find((t=>String(t.id)===String(l))):null;h&&(this.selectedCountry=h,this.updateNewAddressForm({country_id:Number(this.selectedCountry.id)}),!t)&&(null!=a||null!=r||null!=n?await this.prefillFromIPLocation(h,{regionId:a,cityId:r,districtId:n}):y(this.selectedCountry.code)?await this.loadRegions(this.selectedCountry.id):await this.loadCities(this.selectedCountry.id))}finally{this.loadingCountries=!1}var i,e}async prefillFromIPLocation(t,s){const i=y(t.code),{regionId:e,cityId:l,districtId:a}=s,[r,n,h]=await Promise.all([i?v.getRegions(t.id):Promise.resolve([]),v.getCities(t.id,i&&null!=e?e:void 0),null!=l?v.getDistricts(Number(l)):Promise.resolve([])]);if(i&&(this.regions=r,this.displayedRegions=this.regions),this.cities=n,this.displayedCities=this.cities,this.districts=h,this.displayedDistricts=this.districts,this.loadingRegions=!1,this.loadingCities=!1,this.loadingDistricts=!1,i&&null!=e){const t=this.regions.find((t=>String(t.id)===String(e)));t&&(this.selectedRegion=t,this.updateNewAddressForm({region_id:t.id}))}if(null!=l&&this.cities.length>0){const t=this.cities.find((t=>String(t.id)===String(l)));t&&(this.selectedCity=t,this.updateNewAddressForm({city_id:t.id,city:t}))}if(null!=a&&this.districts.length>0){const t=this.findPrefillDistrict(a);t&&(this.selectedDistrict=t,this.updateNewAddressForm({district_id:t.id,district:t}))}}async loadCities(t,s){this.loadingCities=!0,this.cities=[],this.displayedCities=[],this.districts=[],this.displayedDistricts=[],this.selectedCity=null,this.selectedDistrict=null,this.districtName="";try{this.cities=await v.getCities(t,s),this.displayedCities=this.cities}finally{this.loadingCities=!1}}async loadDistricts(t){this.loadingDistricts=!0,this.districts=[],this.displayedDistricts=[],this.selectedDistrict=null,this.districtName="";try{this.districts=await v.getDistricts(t),this.displayedDistricts=this.districts}finally{this.loadingDistricts=!1}}findPrefillDistrict(t,s){if(0===this.districts.length)return;if(null!=t){const s=this.districts.find((s=>String(s.id)===String(t)));if(s)return s}const i=s?.trim().toLowerCase();return i?this.districts.find((t=>{const s=t.name?.trim().toLowerCase(),e=t.name_en?.trim().toLowerCase();return s===i||e===i})):void 0}async loadBranches(){this.loadingBranches=!0;try{this.branches=await v.getBranches({country_id:this.selectedCountry?.id}),this.filterBranches()}finally{this.loadingBranches=!1}}filterBranches(){this.filteredBranches=((t,s)=>{if(!s)return t;const i=s.toLowerCase();return t.filter((t=>t.name.toLowerCase().includes(i)||t.city?.name?.toLowerCase().includes(i)))})(this.branches,this.branchSearchQuery)}async prefillFromSessionStorage(){try{const t=this.getStoredIntent();if(!t)return;if("address"!==this.activeTab||"address"!==t.type||this.isLoggedIn||await this.prefillAddressFromStoredIntent(),"branch"===this.activeTab&&"branch"===t.type){const s=C(t);if(null!=s){const i=this.countries.find((t=>String(t.id)===String(s)));if(i){this.selectedCountry=i,this.updateNewAddressForm({country_id:Number(i.id)});const s=M(t);if(null!=s&&this.branches.length>0){const t=this.branches.find((t=>t.id===s));t&&(this.selectedBranch=t)}}}}}catch(t){console.error("[BulletDelivery] Error prefilling from session storage:",t)}}async loadSavedAddresses(){this.savedAddresses=await v.getSavedAddresses();const t=this.getStoredIntent();if(D(t))this.selectedSessionAddress=!0,this.selectedSavedAddress=null;else{const t=this.savedAddresses.find((t=>t.is_default&&!1!==t.is_in_coverage));if(t)this.selectedSavedAddress=t;else{const t=this.savedAddresses.find((t=>!1!==t.is_in_coverage));t&&(this.selectedSavedAddress=t)}}}applyCountryChange(t){this.selectedCountry=t,this.selectedRegion=null,this.regions=[],this.displayedRegions=[],this.cities=[],this.displayedCities=[],this.districts=[],this.displayedDistricts=[],this.selectedCity=null,this.selectedDistrict=null,this.districtName="",this.allocationOutOfCoverageMessage=null,this.regionSearchQuery="",this.citySearchQuery="",this.districtSearchQuery="",this.searchingRegions=!1,this.searchingCities=!1,this.searchingDistricts=!1,this.clearSearchTimers(),this.selectedCountry&&(this.updateNewAddressForm({country_id:Number(this.selectedCountry.id),region_id:void 0,city_id:void 0,district_id:void 0,city:void 0,district:void 0}),"branch"===this.activeTab?this.loadBranches():y(this.selectedCountry.code)?this.loadRegions(this.selectedCountry.id):this.loadCities(this.selectedCountry.id))}async loadRegions(t){this.loadingRegions=!0,this.regions=[],this.displayedRegions=[],this.selectedRegion=null,this.cities=[],this.displayedCities=[],this.districts=[],this.displayedDistricts=[],this.selectedCity=null,this.selectedDistrict=null,this.districtName="";try{this.regions=await v.getRegions(t),this.displayedRegions=this.regions}finally{this.loadingRegions=!1}}emitHeaderContextUpdate(t,s,i){let e="";"address"===t.type?s?e=s.district?.name&&s.city?.name?`${s.district.name}، ${s.city.name}`:s.city?.name??"":this.selectedCity?e=this.selectedDistrict?`${this.selectedDistrict.name}، ${this.selectedCity.name}`:this.selectedCity.name:t.address_details&&(e=[t.address_details.district?.name,t.address_details.city.name,t.address_details.country.name].filter(Boolean).join("، ")||t.address_details.short_address||""):i?e=i.name:t.branch_details&&(e=t.branch_details.city??t.branch_details.name);const l="address"===t.type?Salla.lang.get("mobile_app.strings.delivery"):Salla.lang.get("mobile_app.strings.pickup");this.headerContextUpdate.emit({type:t.type,display_text:e?`${l}: ${e}`:"",country_code:t.country_code,city:this.selectedCity?.name??t.address_details?.city?.name,district:this.selectedDistrict?.name??t.address_details?.district?.name,branch_name:i?.name??t.branch_details?.name})}handleBranchSearch(t){const s=t.target.value;if(this.branchSearchQuery=s,this.branchSearchDebounceTimer&&clearTimeout(this.branchSearchDebounceTimer),!s?.trim())return this.branchSearchQuery="",void this.loadBranches();this.branchSearchDebounceTimer=setTimeout((async()=>{if(!(s.trim().length<2)){this.loadingBranches=!0;try{this.branches=await v.getBranches({country_id:this.selectedCountry?.id,query:s}),this.filterBranches()}finally{this.loadingBranches=!1}}}),1e3)}clearSearchTimers(){[this.countrySearchTimer,this.regionSearchTimer,this.citySearchTimer,this.districtSearchTimer].forEach((t=>{t&&clearTimeout(t)})),this.countrySearchTimer=this.regionSearchTimer=this.citySearchTimer=this.districtSearchTimer=null}resetSearchState(){this.countrySearchQuery="",this.regionSearchQuery="",this.citySearchQuery="",this.districtSearchQuery="",this.searchingCountries=!1,this.searchingRegions=!1,this.searchingCities=!1,this.searchingDistricts=!1,this.displayedCountries=[],this.displayedRegions=[],this.displayedCities=[],this.displayedDistricts=[],this.clearSearchTimers()}async loadBranchesWithLocation(t,s){this.loadingBranches=!0;try{if(this.branches=await v.getBranches({country_id:this.selectedCountry?.id,lat:t,lng:s}),this.filterBranches(),!this.selectedCountry&&this.branches.length>0){const t=this.branches[0];if(t.country){const s=this.countries.find((s=>s.id===t.country.id));s&&(this.selectedCountry=s,this.updateNewAddressForm({country_id:Number(s.id)}))}}}finally{this.loadingBranches=!1}}async handleFindNearestBranch(){if(navigator.geolocation){this.loadingNearestBranch=!0,this.locationError="";try{const t=await new Promise(((t,s)=>{navigator.geolocation.getCurrentPosition(t,s,{enableHighAccuracy:!0,timeout:1e4,maximumAge:0})})),{latitude:s,longitude:i}=t.coords;await this.loadBranchesWithLocation(s,i);const e=function(t,s,i){let e=null,l=Number.POSITIVE_INFINITY;for(const a of t){const t=null!=a.location?.lat?Number(a.location.lat):Number.NaN,r=null!=a.location?.lng?Number(a.location.lng):Number.NaN;if(Number.isNaN(t)||Number.isNaN(r)||!1===a.is_open)continue;const n=f(s,i,t,r);n<l&&(l=n,e=a)}return e}(this.branches,s,i);e&&(this.selectedBranch=e,this.branchSearchQuery="",this.filterBranches())}catch(t){const s=t;this.locationError=L["number"==typeof s?.code?s.code:-1]??"An error occurred while detecting location"}finally{this.loadingNearestBranch=!1}}else this.locationError=Salla.lang.get("pages.checkout.current_location_not_supported")}handleBranchSelect(t){this.selectedBranch=t}handleSavedAddressSelect(t){!1!==t.is_in_coverage?(this.selectedSavedAddress=t,this.selectedSessionAddress=!1):Salla.notify?.error(Salla.lang.get("pages.checkout.shipping_not_available"))}handleSessionAddressSelect(){this.selectedSessionAddress=!0,this.selectedSavedAddress=null}async handleAddNewAddress(){this.viewMode="add-address";const t=this.getStoredIntent(),s=C(t);if(s&&this.countries.length>0){const t=this.countries.find((t=>t.id===s));t&&(this.selectedCountry=t,this.updateNewAddressForm({country_id:Number(t.id)}),y(t.code)?await this.loadRegions(t.id):await this.loadCities(t.id))}}handleBackToAddressList(){this.viewMode="main"}buildAddressLocationPayload(){const t=this.selectedDistrict?.name??this.districtName?.trim()??"";return function(t){const{countryId:s,countryCode:i,regionId:e,cityId:l,districtId:a,description:r=""}=t,n=r||"";return{country_id:Number(s)||void 0,region_id:null!=e?Number(e):void 0,city_id:null!=l?Number(l):void 0,district_id:null!=a?Number(a):void 0,description:n,...i&&y(i)&&{local:n}}}({countryId:this.selectedCountry?.id??0,countryCode:this.selectedCountry?.code,regionId:this.selectedRegion?.id,cityId:this.selectedCity?.id,districtId:this.selectedDistrict?.id,description:t})}async handleTabChange(t){if(!this.tabChanging){this.tabChanging=!0;try{this.activeTab=t,this.showCartWillBeClearedBanner=!1,this.allocationOutOfCoverageMessage=null,this.resetSearchState();const s="branch"===t;this.countries=await v.getCountries(s),this.displayedCountries=this.countries;const i=this.selectedCountry?.id;if(null!=i&&(this.selectedCountry=this.countries.find((t=>t.id===i))||null),!this.selectedCountry){const{countryId:t}=this.getIPLocationConfig();t&&this.countries.length>0&&(this.selectedCountry=this.countries.find((s=>String(s.id)===String(t)))||null),this.selectedCountry||1!==this.countries.length||(this.selectedCountry=this.countries[0])}if(this.selectedCountry)if("branch"===t){const t=this.getStoredIntent(),s=B(t),i=N(t);null!=s&&null!=i?await this.loadBranchesWithLocation(s,i):await this.loadBranches();const e=M(t);if(null!=e&&this.branches.length>0){const t=this.branches.find((t=>t.id===e));t&&(this.selectedBranch=t)}}else if("address"===t){if(this.isLoggedIn&&!this.savedAddressesLoaded){this.loadingSavedAddresses=!0;try{await this.loadSavedAddresses(),this.savedAddressesLoaded=!0}finally{this.loadingSavedAddresses=!1}}if(!this.isLoggedIn){const t=this.getStoredIntent();t&&x(t)&&await this.prefillAddressFromStoredIntent()}}}finally{this.tabChanging=!1}}}isConfirmDisabled(){return"branch"===this.activeTab?!this.selectedBranch:!("address"!==this.activeTab||this.isLoggedIn&&this.selectedSessionAddress||(this.isLoggedIn&&this.selectedSavedAddress?!1!==this.selectedSavedAddress.is_in_coverage:this.selectedCountry&&this.selectedCity&&$(this.selectedCountry?.code,this.selectedRegion?.id,this.selectedDistrict?.id??(this.districtName?.trim()||null))))}handleLogin(){Salla.auth.setCanRedirect(!1),Salla.event.dispatch("salla::bullet-delivery.modal.close.requested"),Salla.event.dispatch("login::open",Salla.url.is_page("cart")?{withoutReload:!0,source:"cart-submit"}:{withoutReload:!0,source:"bullet-delivery"})}resolvePendingCartSubmit(){if(!this.pendingCartSubmitResolver)return;const t=this.pendingCartSubmitResolver;this.pendingCartSubmitResolver=null,this.pendingCartSubmitPromise=null,this.cartSubmitConfirmationPending=!1,t()}buildIntentAddress(){if("address"!==this.activeTab||!this.selectedCountry||!this.selectedCity)return;const t={id:this.selectedCountry.id,name:this.selectedCountry.name,code:this.selectedCountry.code},s={id:this.selectedCity.id,name:this.selectedCity.name},i=this.selectedRegion?{id:this.selectedRegion.id,name:this.selectedRegion.name,code:this.selectedRegion.code}:void 0,e=this.selectedDistrict?{id:this.selectedDistrict.id,name:this.selectedDistrict.name}:this.districtName?{id:0,name:this.districtName}:void 0,l=this.selectedSavedAddress?.short_address;return{country:t,region:i,city:s,district:e,short_address:l}}buildIntentAddressFromSaved(t){return{country:{id:t.country?.id??t.country_id??0,name:t.country?.name??"",code:t.country?.code},region:t.region?{id:t.region.id,name:t.region.name,code:t.region.code}:void 0,city:{id:t.city?.id??t.city_id??0,name:t.city?.name??""},district:t.district?{id:t.district.id,name:t.district.name}:void 0,short_address:t.short_address}}buildIntentBranch(){if("branch"!==this.activeTab||!this.selectedBranch)return;const t=this.selectedBranch,s=null!=t.location?.lat?Number(t.location.lat):void 0,i=null!=t.location?.lng?Number(t.location.lng):void 0;return{id:t.id,name:t.name,city:t.city?.name,latitude:s,longitude:i}}buildAllocationConfirmedAddressData(t){return"address"!==this.activeTab?null:{saved_address:t??this.selectedSavedAddress??void 0,country:this.selectedCountry??void 0,region:this.selectedRegion??void 0,city:this.selectedCity??void 0,district:this.selectedDistrict??void 0,district_name:this.districtName?.trim()||void 0,short_address:t?.short_address??this.selectedSavedAddress?.short_address,form_data:{...this.newAddressForm}}}buildAllocationConfirmedAddressFromIntent(t){if("address"!==t.type)return null;const s=t.address_details,i=Number(t.country_id)||0;return{country:s?.country?{id:s.country.id,name:s.country.name,code:s.country.code??t.country_code??""}:void 0,region:s?.region?{id:Number(s.region.id),name:s.region.name,country_id:i,code:s.region.code??void 0}:void 0,city:s?.city?{id:Number(s.city.id),name:s.city.name,country_id:i}:void 0,district:s?.district?{id:Number(s.district.id),name:s.district.name,city_id:Number(s.city?.id)||0}:void 0,short_address:s?.short_address}}buildBranchFromIntent(t){if("branch"!==t.type||!t.branch_details)return null;const s=t.branch_details,i={id:s.id,name:s.name};return s.city&&(i.city={id:0,name:s.city}),null!=s.latitude&&null!=s.longitude&&(i.location={lat:String(s.latitude),lng:String(s.longitude)}),i}buildAllocationRequestFromIntent(t){if("branch"===t.type)return{type:"branch",branch_id:null!=t.branch_id?String(t.branch_id):""};if(null!=t.address_id)return{type:"address",address_id:String(t.address_id)};const s=t.address_details,i={type:"address",country_id:null!=t.country_id?String(t.country_id):"",city_id:null!=s?.city?.id?String(s.city.id):""};return null!=s?.region?.id&&(i.region_id=String(s.region.id)),null!=s?.district?.id&&(i.district_id=String(s.district.id)),i}applyMobileBulletDeliverySelection(t){if(!t||"address"!==t.type&&"branch"!==t.type)return void console.warn("SallaBulletDelivery: ignoring invalid mobile select payload",t);const s=t.allocation_headers??{},i=s["s-scope-allocation-type"]??t.type,e=s["s-scope-allocation-id"];i&&Salla.api.setHeader("s-scope-allocation-type",i),e&&Salla.api.setHeader("s-scope-allocation-id",String(e));const l=Salla.config.get("store.scope",Salla.storage.get("scope"))||{};Salla.storage.set("scope",{...l,allocation_type:i,allocation_id:e?String(e):void 0}),this.markShownThisSession();const a={...t,allocation_headers:{"s-scope-allocation-type":i,"s-scope-allocation-id":e?String(e):void 0}};this.setStoredIntent(a),m();const r=this.buildBranchFromIntent(a),n={...a,address:this.buildAllocationConfirmedAddressFromIntent(a),branch:r,allocation_request:this.buildAllocationRequestFromIntent(a),allocation_response:null};Salla.event.dispatch("salla::bullet-delivery.allocation.confirmed",n),this.bulletDeliveryConfirmed.emit({...a,branch:r??void 0}),this.emitHeaderContextUpdate(a,void 0,r??void 0),setTimeout((()=>window.location.reload()),100)}extractMobileSelectPayload(t){return t&&"object"==typeof t?"payload"in t&&t.payload?t.payload:t:null}async handleConfirm(){if(this.isConfirmDisabled()||this.confirming)return;this.confirming=!0,this.confirmBtn?.load();let t=!1;try{const s={type:this.activeTab,country_id:Number(this.selectedCountry?.id)||0,country_code:this.selectedCountry?.code||""};let i,e,l;if("address"===this.activeTab)if(this.isLoggedIn&&this.selectedSessionAddress){const t=await this.saveSessionAddressToProfile();if(!t)return Salla.notify?.error(Salla.lang.get("common.errors.error_occurred")),void this.confirmBtn?.stop();s.address_id=t.id,i=t,this.selectedSessionAddress=!1,this.selectedSavedAddress=t,s.address_details=this.buildIntentAddressFromSaved(t)}else this.isLoggedIn&&this.selectedSavedAddress?(s.address_id=this.selectedSavedAddress.id,i=this.selectedSavedAddress,s.address_details=this.buildIntentAddressFromSaved(this.selectedSavedAddress)):s.address_details=this.buildIntentAddress();else s.branch_details=this.buildIntentBranch(),s.branch_id=this.selectedBranch?.id,e=this.selectedBranch||void 0;"address"===this.activeTab?this.isLoggedIn&&this.selectedSavedAddress?l={type:"address",address_id:String(this.selectedSavedAddress.id)}:(l={type:"address",country_id:String(this.selectedCountry?.id||0),city_id:String(this.selectedCity?.id||"")},y(this.selectedCountry?.code??"")&&(this.selectedRegion?.id&&(l.region_id=String(this.selectedRegion.id)),l.district_id=String(this.selectedDistrict?.id||""))):l={type:"branch",branch_id:String(this.selectedBranch?.id||"")};const a=await v.allocateScope(l);if(!a.success){console.error("BulletDelivery: Scope allocation failed",a.error);const t=a.error||("address"===this.activeTab?Salla.lang.get("pages.checkout.address_out_of_coverage"):Salla.lang.get("pages.checkout.failed_to_set_pickup"));return this.allocationOutOfCoverageMessage=t,this.confirmBtn?.stop(),void(this.confirming=!1)}let r;if(a.data){const t=a.data;r=t.id;const i=t?.allocation?.type,e=t?.allocation?.branch_id;i&&Salla.api.setHeader("s-scope-allocation-type",i),e&&Salla.api.setHeader("s-scope-allocation-id",String(e));const l=Salla.config.get("store.scope",Salla.storage.get("scope"))||{};Salla.storage.set("scope",{...l,id:r,allocation_type:i,allocation_id:e?String(e):void 0}),s.allocation_headers={"s-scope-allocation-type":i,"s-scope-allocation-id":e?String(e):void 0}}this.markShownThisSession(),this.setStoredIntent(s),r&&(await v.setDeliveryScope(r),m());const n={...s,address:this.buildAllocationConfirmedAddressData(i),branch:e??null,allocation_request:l,allocation_response:a.data??null};Salla.event.dispatch("salla::bullet-delivery.allocation.confirmed",n),this.bulletDeliveryConfirmed.emit({...s,address:i,branch:e}),this.emitHeaderContextUpdate(s,i,e),t=!0,setTimeout((()=>{if(this.confirmBtn?.stop(),this.close(),this.confirming=!1,this.cartSubmitConfirmationPending)this.resolvePendingCartSubmit();else if(r){const t=Salla.storage.get("scope")||{};let s=Salla.helpers.addParamToUrl("scope",r);t.allocation_id&&(s=Salla.helpers.addParamToUrl("allocation_id",t.allocation_id,s)),t.allocation_type&&(s=Salla.helpers.addParamToUrl("allocation_type",t.allocation_type,s)),window.location.replace(s)}else window.location.reload()}),500)}finally{t||(this.confirming=!1)}}getCartViewConfig(t){if(!t||!Salla.url.is_page("cart"))return null;const s="branch"===t.type,i="address"===t.type;return s&&!this.supportsPickup||!s&&!i?null:{title:s?Salla.lang.get("pages.checkout.pickup_option"):Salla.lang.get("pages.checkout.delivery_option"),subtitle:S(t,Salla.lang.get("common.elements.to"),""),buttonText:Salla.lang.get("common.elements.edit"),isAddress:i,intent:t}}createCartViewElement(){const t=this.getStoredIntent(),s=this.getCartViewConfig(t);if(!s)return null;const{title:i,subtitle:e,buttonText:l,intent:a}=s,r="branch"===a.type?{preselected_branch_id:M(a)}:{preselected_address_id:a.address_id},h=document.createElement("span");h.className="s-bullet-delivery-cart-view-icon s-bullet-delivery-tab-icon",h.setAttribute("aria-hidden","true"),h.innerHTML=n;const o=document.createElement("span");o.className="s-bullet-delivery-cart-view-title",o.textContent=i;const d=document.createElement("span");d.className="s-bullet-delivery-cart-view-subtitle",d.textContent=e;const c=document.createElement("div");c.className="s-bullet-delivery-cart-view-text",c.append(o,d);const u=document.createElement("div");u.className="s-bullet-delivery-cart-view-info",u.append(h,c);const p=document.createElement("salla-button");p.className="s-bullet-delivery-cart-view-button",p.setAttribute("fill","outline"),p.setAttribute("color","primary"),p.textContent=l,p.addEventListener("click",(()=>Salla.event.emit("salla::bullet-delivery.modal.open.requested",r)));const b=document.createElement("div");return b.className="s-bullet-delivery-cart-view",b.append(u,p),b}renderTabs(){return this.supportsPickup?i("div",{class:"s-bullet-delivery-tabs"},i("button",{type:"button",class:{"s-bullet-delivery-tab":!0,"s-bullet-delivery-tab--active":"address"===this.activeTab},onClick:()=>this.handleTabChange("address")},i("span",{class:"s-bullet-delivery-tab-icon",innerHTML:n,"aria-hidden":"true"}),i("span",null,Salla.lang.get("mobile_app.strings.delivery"))),i("button",{type:"button",class:{"s-bullet-delivery-tab":!0,"s-bullet-delivery-tab--active":"branch"===this.activeTab},onClick:()=>this.handleTabChange("branch")},i("span",{class:"s-bullet-delivery-tab-icon",innerHTML:T,"aria-hidden":"true"}),i("span",null,Salla.lang.get("mobile_app.strings.pickup")))):null}renderAlert(t){return i("div",{class:"s-bullet-delivery-allocation-out-of-coverage",role:"alert"},i("span",{class:"s-bullet-delivery-allocation-out-of-coverage-icon","aria-hidden":"true"},i("svg",{width:"20",height:"20",viewBox:"0 0 20 20",fill:"none",xmlns:"http://www.w3.org/2000/svg",role:"img","aria-hidden":"true"},i("title",null,"Information"),i("circle",{cx:"10",cy:"10",r:"9",stroke:"currentColor","stroke-width":"1.5",fill:"currentColor"}),i("text",{x:"10",y:"14","text-anchor":"middle",fill:"white","font-size":"12","font-weight":"bold"},"i"))),i("span",{class:"s-bullet-delivery-allocation-out-of-coverage-text"},t))}renderAllocationOutOfCoverageAlert(){const t=this.allocationOutOfCoverageMessage||Salla.lang.get("pages.checkout.shipping_not_available");return this.renderAlert(t)}renderCountrySelect(){return i("div",{class:"s-bullet-delivery-field"},i("salla-searchable-dropdown",{label:Salla.lang.get("blocks.buy_as_gift.receiver_country"),placeholder:Salla.lang.get("pages.checkout.select_country"),items:this.displayedCountries,selectedItem:this.selectedCountry,loading:this.loadingCountries,searching:this.searchingCountries,disabled:this.countries.length<=1,required:!0,inputId:"bullet-delivery-country",searchQuery:this.countrySearchQuery,clientSearch:!0,onItemSelected:t=>this.handleCountrySelected(t.detail),onSearchInput:t=>this.handleCountrySearch(t.detail),onDropdownClosed:()=>this.handleCountryDropdownClosed()}))}renderRegionSelect(){return y(this.selectedCountry?.code??"")&&this.selectedCountry?i("div",{class:"s-bullet-delivery-field"},i("salla-searchable-dropdown",{label:Salla.lang.get("pages.checkout.region_field"),placeholder:Salla.lang.get("pages.checkout.select_region"),items:this.displayedRegions,selectedItem:this.selectedRegion,loading:this.loadingRegions,searching:this.searchingRegions,disabled:this.loadingRegions||0===this.regions.length,required:!0,inputId:"bullet-delivery-region",searchQuery:this.regionSearchQuery,clientSearch:!0,onItemSelected:t=>this.handleRegionSelected(t.detail),onSearchInput:t=>this.handleRegionSearch(t.detail),onDropdownClosed:()=>this.handleRegionDropdownClosed()})):null}renderCityDistrictSelects(){const t=y(this.selectedCountry?.code??""),s=t,e=s&&this.selectedCity&&!this.loadingDistricts&&0===this.districts.length,l=!this.selectedCountry||t&&!this.selectedRegion||this.loadingCities||0===this.cities.length,a=!this.selectedCountry||t&&!this.selectedRegion||!this.selectedCity||this.loadingDistricts;return i("div",{class:"s-bullet-delivery-field-row"},i("div",{class:"s-bullet-delivery-field"},i("salla-searchable-dropdown",{label:Salla.lang.get("blocks.buy_as_gift.receiver_city"),placeholder:Salla.lang.get("pages.checkout.select_city"),items:this.displayedCities,selectedItem:this.selectedCity,loading:this.loadingCities,searching:this.searchingCities,disabled:l,required:!0,inputId:"bullet-delivery-city",searchQuery:this.citySearchQuery,onItemSelected:t=>this.handleCitySelected(t.detail),onSearchInput:t=>this.handleCitySearch(t.detail),onDropdownClosed:()=>this.handleCityDropdownClosed(),dropUp:!0})),s&&i("div",{class:"s-bullet-delivery-field"},e?i("div",null,i("label",{class:"s-bullet-delivery-label",htmlFor:"bullet-delivery-district"},Salla.lang.get("pages.checkout.district_field"),i("span",{class:"text-red-500"}," *")),i("input",{id:"bullet-delivery-district",type:"text",class:"form-input",placeholder:Salla.lang.get("pages.checkout.select_district"),value:this.districtName,disabled:a,onInput:t=>{this.districtName=t.target.value,this.updateNewAddressForm({district:void 0})}})):i("salla-searchable-dropdown",{label:Salla.lang.get("pages.checkout.district_field"),placeholder:Salla.lang.get("pages.checkout.select_district"),items:this.displayedDistricts,selectedItem:this.selectedDistrict,loading:this.loadingDistricts,searching:this.searchingDistricts,disabled:a||0===this.districts.length,required:!0,inputId:"bullet-delivery-district",searchQuery:this.districtSearchQuery,clientSearch:!0,onItemSelected:t=>this.handleDistrictSelected(t.detail),onSearchInput:t=>this.handleDistrictSearch(t.detail),onDropdownClosed:()=>this.handleDistrictDropdownClosed(),dropUp:!0})))}renderSavedAddressesEmptyState(){return i("div",{class:"s-bullet-delivery-saved-addresses-empty"},i("div",{class:"s-bullet-delivery-saved-addresses-empty-icon","aria-hidden":"true"},i("svg",{width:"28",height:"28",viewBox:"0 0 28 29",fill:"none",xmlns:"http://www.w3.org/2000/svg"},i("title",null,"No Saved Adresses"),i("path",{d:"M20.16 0C21.4613 0 22.6773 0.341333 23.808 1.024C24.9387 1.70667 25.8347 2.61333 26.496 3.744C27.1787 4.91733 27.52 6.18667 27.52 7.552C27.52 9.45067 26.8267 11.232 25.44 12.896C24.4587 14.0907 23.0827 15.2427 21.312 16.352H21.28C20.9387 16.5653 20.5653 16.672 20.16 16.672C19.7547 16.672 19.3813 16.5653 19.04 16.352H19.008C17.2373 15.2213 15.8613 14.0693 14.88 12.896C13.4933 11.232 12.8 9.45067 12.8 7.552C12.8 6.18667 13.1413 4.91733 13.824 3.744C14.4853 2.61333 15.3813 1.70667 16.512 1.024C17.6427 0.341333 18.8587 0 20.16 0ZM11.488 1.984C9.67467 2.368 8.04267 3.14667 6.592 4.32C5.14133 5.49333 4.01067 6.92267 3.2 8.608C2.34667 10.3573 1.92 12.224 1.92 14.208C1.92 14.72 1.952 15.2427 2.016 15.776L2.08 15.744C2.57067 15.488 2.94933 15.3067 3.216 15.2C3.48267 15.0933 3.73867 15.0933 3.984 15.2C4.22933 15.3067 4.4 15.488 4.496 15.744C4.592 16 4.58667 16.256 4.48 16.512C4.37333 16.768 4.19733 16.9493 3.952 17.056C3.70667 17.1627 3.36 17.3227 2.912 17.536L2.656 17.664C2.57067 17.7067 2.48533 17.7387 2.4 17.76C2.89067 19.4667 3.70133 20.9973 4.832 22.352C5.96267 23.7067 7.296 24.7573 8.832 25.504C10.432 26.272 12.1173 26.656 13.888 26.656C15.4667 26.656 16.9813 26.3467 18.432 25.728C19.8187 25.1307 21.0667 24.288 22.176 23.2C21.92 23.0507 21.7653 22.8373 21.712 22.56C21.6587 22.2827 21.7013 22.0267 21.84 21.792C21.9787 21.5573 22.176 21.408 22.432 21.344C22.8373 21.2587 23.2 21.12 23.52 20.928C23.6907 20.8427 23.8613 20.8 24.032 20.8C24.8 19.5413 25.3333 18.176 25.632 16.704C25.6747 16.4267 25.808 16.2133 26.032 16.064C26.256 15.9147 26.496 15.8667 26.752 15.92C27.008 15.9733 27.2107 16.1173 27.36 16.352C27.5093 16.5867 27.552 16.832 27.488 17.088C27.0613 19.2853 26.1973 21.264 24.896 23.024C23.5947 24.784 22.0053 26.1547 20.128 27.136C18.1653 28.16 16.0853 28.672 13.888 28.672C11.3707 28.672 9.03467 28.0107 6.88 26.688C4.78933 25.408 3.12533 23.68 1.888 21.504C0.629333 19.264 0 16.832 0 14.208C0 11.904 0.490667 9.73867 1.472 7.712C2.41067 5.74933 3.728 4.09067 5.424 2.736C7.12 1.38133 9.01333 0.48 11.104 0.032C11.36 -0.032 11.6 0.0106667 11.824 0.16C12.048 0.309333 12.1867 0.522667 12.24 0.8C12.2933 1.07733 12.2453 1.328 12.096 1.552C11.9467 1.776 11.744 1.92 11.488 1.984ZM14.72 7.552C14.72 8.96 15.2533 10.304 16.32 11.584C17.1733 12.5867 18.3893 13.6 19.968 14.624C20.032 14.6453 20.096 14.656 20.16 14.656C20.224 14.656 20.288 14.6453 20.352 14.624C21.9307 13.6213 23.1467 12.608 24 11.584C25.0667 10.304 25.6 8.96 25.6 7.552C25.6 6.57067 25.3547 5.65333 24.864 4.8C24.3733 3.94667 23.7067 3.26933 22.864 2.768C22.0213 2.26667 21.12 2.016 20.16 2.016C19.2 2.016 18.2987 2.26667 17.456 2.768C16.6133 3.26933 15.9467 3.94667 15.456 4.8C14.9653 5.65333 14.72 6.57067 14.72 7.552ZM17.28 7.68C17.28 6.848 17.5627 6.13867 18.128 5.552C18.6933 4.96533 19.3707 4.672 20.16 4.672C20.9493 4.672 21.6267 4.96533 22.192 5.552C22.7573 6.13867 23.04 6.848 23.04 7.68C23.04 8.512 22.7573 9.216 22.192 9.792C21.6267 10.368 20.9493 10.656 20.16 10.656C19.3707 10.656 18.6933 10.368 18.128 9.792C17.5627 9.216 17.28 8.512 17.28 7.68ZM6.752 14.688C7.62667 14.7733 8.53333 15.0293 9.472 15.456C9.70667 15.5627 9.87733 15.744 9.984 16C10.0907 16.256 10.0907 16.512 9.984 16.768C9.87733 17.024 9.70133 17.2 9.456 17.296C9.21067 17.392 8.97067 17.3867 8.736 17.28C7.94667 16.9387 7.22133 16.736 6.56 16.672C6.28267 16.6293 6.064 16.5013 5.904 16.288C5.744 16.0747 5.68 15.8293 5.712 15.552C5.744 15.2747 5.86133 15.0507 6.064 14.88C6.26667 14.7093 6.496 14.6453 6.752 14.688ZM17.6 20.544C18.304 20.864 18.976 21.0987 19.616 21.248C19.872 21.312 20.0693 21.4613 20.208 21.696C20.3467 21.9307 20.3893 22.1813 20.336 22.448C20.2827 22.7147 20.144 22.9227 19.92 23.072C19.696 23.2213 19.4453 23.264 19.168 23.2C18.4213 23.0293 17.632 22.752 16.8 22.368C16.5653 22.2613 16.4 22.08 16.304 21.824C16.208 21.568 16.2133 21.312 16.32 21.056C16.4267 20.8 16.6027 20.624 16.848 20.528C17.0933 20.432 17.344 20.4373 17.6 20.544ZM12.832 17.44C13.0027 17.568 13.248 17.7493 13.568 17.984L14.496 18.688C14.7093 18.8587 14.8373 19.0773 14.88 19.344C14.9227 19.6107 14.8693 19.856 14.72 20.08C14.5707 20.304 14.368 20.4427 14.112 20.496C13.856