import{Host,h}from"@stencil/core";import{state as checkoutState,onChange}from"../../../../../store/checkout/index";import{getGeoCoordinates}from"../../../../../functions/geolocation";import{getGeoPermissionDefaults}from"../../../../../functions/geo-permission";const DECISION_KEY="sc_geo_capture_decision";export class ScCheckoutGeoPermission{constructor(){this.open=!1}cartHasItems(){var e,t,o;return!!(null===(o=null===(t=null===(e=checkoutState.checkout)||void 0===e?void 0:e.line_items)||void 0===t?void 0:t.pagination)||void 0===o?void 0:o.count)}wasDismissed(){try{return"declined"===window.localStorage.getItem(DECISION_KEY)}catch{return!1}}rememberDismissed(){try{window.localStorage.setItem(DECISION_KEY,"declined")}catch{}}async getPermissionState(){var e,t;try{const o=await(null===(e=navigator.permissions)||void 0===e?void 0:e.query({name:"geolocation"}));return null!==(t=null==o?void 0:o.state)&&void 0!==t?t:null}catch{return null}}async componentDidLoad(){checkoutState.captureGeoAddressEnabled&&(null===window||void 0===window?void 0:window.isSecureContext)&&(null===navigator||void 0===navigator?void 0:navigator.geolocation)&&(this.cartHasItems()?this.maybePromptForLocation():this.removeCartListener=onChange("checkout",(()=>{var e;this.cartHasItems()&&(null===(e=this.removeCartListener)||void 0===e||e.call(this),this.removeCartListener=void 0,this.maybePromptForLocation())})))}disconnectedCallback(){var e;null===(e=this.removeCartListener)||void 0===e||e.call(this)}async maybePromptForLocation(){const e=await this.getPermissionState();"granted"!==e?"denied"!==e&&(this.wasDismissed()||(this.open=!0)):this.capture()}async capture(){checkoutState.geoCoordinates||(checkoutState.geoCoordinates=await getGeoCoordinates())}onAllow(){this.open=!1,this.capture()}onDecline(){this.rememberDismissed(),this.open=!1}render(){const{geoCapture:e}=checkoutState,t=getGeoPermissionDefaults(),o=(null==e?void 0:e.title)||t.title,i=(null==e?void 0:e.content)||t.content,s=(null==e?void 0:e.allowLabel)||t.allowLabel,n=(null==e?void 0:e.declineLabel)||t.declineLabel;return h(Host,{key:"0ad4ae587e752158efb5c99d8c8fcb14930997bb"},h("sc-dialog",{key:"2fc8c24f596da900c5c9ab489a4e57b2d884ee7e",open:this.open,noHeader:!0,onScRequestClose:()=>this.onDecline(),class:"geo-permission"},h("sc-dashboard-module",{key:"45f9bcc6b76f56ad5bae67eac7e8c5cb063a7f30",style:{"--sc-dashboard-module-spacing":"1em"}},h("sc-flex",{key:"7d7fb2fc9a4e3d8beb9347b2a714f7d209b7bbd5",slot:"heading","align-items":"center","justify-content":"flex-start"},h("sc-icon",{key:"588595f82cdcda2b20ba59f0b47378d3f309eddd",name:"map-pin",style:{color:"var(--sc-color-primary-500)"}}),o),h("div",{key:"b2ab9bc87c22b19f2540f3e5574f9e336a5d97f7",slot:"description",class:"geo-permission__content",innerHTML:i})),h("sc-button",{key:"3994b2422d57fba69b9f147b9a65e9740c7daf36",slot:"footer",type:"text",onClick:()=>this.onDecline()},n),h("sc-button",{key:"e7b2b56522060bd72732e65e61e201d03c5e3748",slot:"footer",type:"primary",onClick:()=>this.onAllow()},s,h("sc-icon",{key:"7a93908aacf286ccdf684e4822e1490535b38d2d",name:"map-pin",slot:"suffix"}))))}static get is(){return"sc-checkout-geo-permission"}static get encapsulation(){return"shadow"}static get originalStyleUrls(){return{$:["sc-checkout-geo-permission.scss"]}}static get styleUrls(){return{$:["sc-checkout-geo-permission.css"]}}static get states(){return{open:{}}}}