1import{b as f,ab as o,ac as c,ad as d,p as b,t as g,R as p,u,i as m,J as v,ae as A,G as F,N as T,H as E,e as N,T as w,C as y,r as U}from"./storefrontApp.CsApItsi.min.js";import{$ as M,e as _,t as O}from"./chunk-vendor-lit.NvY0u02.js";import{F as S}from"./chunk-frontstore_preview_error.Dw60zC7.js";import{P as I}from"./chunk-product_stock_changed_handler.C3zWS6V.js";import{S as R}from"./chunk-storefront_base_lit_element.lX23Jvc.js";import{B as C}from"./chunk-btn_controller.BPcEEeZ.js";import{B as P}from"./chunk-base_http_api_url.H743YfZ.js";import{S as H}from"./chunk-storefront_settings_constants.DIF02Gz.js";import"./chunk-vendor-di.Bh23gdA.js";import"./chunk-vendor-rxjs.Dp7kflo.js";import"./chunk-vendor-lodash.CWHFt7Z.js";import"./chunk-vendor-phoenix.cdKS79L.js";import"./chunk-product_events_constants.BHFu5GP.js";import"./chunk-keystrokes_controller.BSiCZ0Y.js";class B extends f{constructor(t){super(),this.moduleName=o.service,this.#t=t}#t;getAllFavourites(){return this.#t.getObject(c)}has(t){return!!this.getAllFavourites()?.find(i=>i===t)}async subscribe(t,s){if(await t.subscribe(s.toString())!==d.success)return;const e=this.getAllFavourites(),r=e?[...e,s]:[s];this.#t.setObject(c,r)}async unsubscribe(t,s){if(await t.unsubscribe(s.toString())!==d.success)return;const e=this.getAllFavourites();if(!e)return;const r=e.filter(n=>n!==s);this.#t.setObject(c,r)}}var $=Object.defineProperty,l=(a,t,s,i)=>{for(var e=void 0,r=a.length-1,n;r>=0;r--)(n=a[r])&&(e=n(t,s,e)||e);return e&&$(t,s,e),e};class h extends R{constructor(){super(),this._favouritesService=null,this._addedSlotName="added",this._notAddedSlotName="not-added",this.productId=null,this.variantId=null,this.isAddedToFavourites=!1,this._getFavouritesStore=()=>{const t=this.getApiSync(b),s=t.getStore(o.store);return s||t.createStore("local",o.store)},this._toggleState=async()=>{this._favouritesService&&(await(this.isAddedToFavourites?this._unsubscribe():this._subscribe()),this._updateIsAddedToFavourites())},this._handleUpdateAddToFavourites=({body:t})=>{if(!t)return;const{productId:s,stockId:i}=t;s===this.productId&&(this.variantId=i,this._updateIsAddedToFavourites())},this._updateIsAddedToFavourites=()=>{!this.variantId||!this._favouritesService||(this.isAddedToFavourites=this._favouritesService.has(this.variantId))},this._favouritesApi=this.getApiSync(o.api),this._btnController=new C(this,this._toggleState)}static{this.moduleName=o.component}connectedCallback(){if(super.connectedCallback(),!this.productId)throw new S("product-id is not defined");if(!this.variantId)throw new S("variant-id is not defined");this.querySelector(`[slot="${this._addedSlotName}"]`)?.removeAttribute("hidden"),this._favouritesService=new B(this._getFavouritesStore()),this._updateIsAddedToFavourites(),this.addEventListener("click",this._toggleState)}async _subscribe(){await this._favouritesService?.subscribe(this._favouritesApi,this.variantId)}async _unsubscribe(){await this._favouritesService?.unsubscribe(this._favouritesApi,this.variantId)}firstUpdated(t){super.firstUpdated(t),new I(this._handleUpdateAddToFavourites)}render(){if(this._favouritesService)return M`${this.isAddedToFavourites?this.getSlot(this._addedSlotName):this.getSlot(this._notAddedSlotName)}`}}l([_({type:Number,attribute:"product-id"})],h.prototype,"productId");l([_({type:Number,attribute:"variant-id",reflect:!0})],h.prototype,"variantId");l([O()],h.prototype,"isAddedToFavourites");class L extends P{constructor(t,s){super(t,s)}getSubscribeToFavouritesUrl(t){return`${this.webapiUrl}/user/favourites/${t}`}getUnsubscribeToFavouritesUrl(t){return`${this.webapiUrl}/user/favourites/${t}`}isSubscribedUrl(t){return`${this.webapiUrl}/user/favourites/${t}`}}class x extends g{constructor({httpApi:t,settingsApi:s,localeOptions:i}){super(t),this.moduleName=o.httpApi,this.#t=new L(s,i)}#t;subscribe(t){const s=this.#t.getSubscribeToFavouritesUrl(t);return this.fetch({url:s,data:t,method:p.put},{decorators:[u.status,u.flashMessages]})}unsubscribe(t){const s=this.#t.getUnsubscribeToFavouritesUrl(t);return this.fetch({url:s,data:t,method:p.delete},{decorators:[u.status,u.flashMessages]})}isSubscribed(t){const s=this.#t.isSubscribedUrl(t);return this.fetch({url:s,data:t,method:p.get},{decorators:[u.status,u.flashMessages]})}}class z extends m{constructor({httpApi:t,settingsApi:s,localeOptions:i,flashMessengerApi:e,translationApi:r}){super(),this.moduleName=o.api,this.#t=new x({httpApi:t,settingsApi:s,localeOptions:i}),this.#s=e,this.#e=r}#t;#s;#e;async subscribe(t){try{const{data:s}=await this.#t.subscribe(t).response,{flashMessages:i,status:e}=s;return this._displayFlashmessages(i),e===d.success&&this.eventBus.emit(new v(A.added,t)),e}catch(s){this._showUnauthorizedFlashMessage(s)}}async unsubscribe(t){try{const{data:s}=await this.#t.unsubscribe(t).response,{flashMessages:i,status:e}=s;return this._displayFlashmessages(i),e===d.success&&this.eventBus.emit(new v(A.removed,t)),e}catch(s){this._showUnauthorizedFlashMessage(s)}}async isSubscribed(t){const{data:s}=await this.#t.isSubscribed(t).response,{flashMessages:i,status:e}=s;return this._displayFlashmessages(i),e}_displayFlashmessages(t){t&&this.#s.parseAndAddFlashMessages(t,F)}_showUnauthorizedFlashMessage(t){t.response.data&&t.response.data.error_description==="Unauthorized"&&this.#s.addFlashMessage({type:"warning",content:this.#e.translate("You must be logged in to add products to wishlist.")})}}class st extends T{static{this.featureName=o.feature}
1async init(){const t=this.getApiSync(E),s=await this.getApi(H.api),i=await this.getApi(N.api),e=await this.getApi(w.api);return{cores:[{type:y,instance:[h]},{type:U.api,instance:new z({httpApi:t,settingsApi:s,flashMessengerApi:i,translationApi:e})}]}}}export{st as AddToFavouritesInitializer};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.