app_shop.run((function(){app_shop.fn.shoppingList=new ShoppingList,document.getElementById("container").appendChild(document.querySelector(".sl_choose")),document.addEventListener("click",(function(t){if(t.target.closest(".projector_buttons_obs")){if(t.preventDefault(),!app_shop.fn.sizeSelected())return app_shop.fn.alertProjector(),!1;if("function"==typeof app_shop.fn.bundleSizeSelected&&!app_shop.fn.bundleSizeSelected())return!1;app_shop.fn.shoppingList.addProductToList([[document.getElementById("projector_product_hidden").value,document.getElementById("projector_size_hidden").value]])}if(t.target.closest(".basketedit_page .tool__button.--add-to-favorite")){t.preventDefault();const e=[];document.querySelectorAll("#Basket .basket__block[data-product-id]").forEach((t=>{const s=t.getAttribute("data-product-id"),i=t.getAttribute("data-product-size");e.push([s,i])})),app_shop.fn.shoppingList.addProductToList(e)}return!1}))}),"all",".sl_choose");const ShoppingListTxt={firstListName:"Lista zakupowa"};ShoppingList=function(t){const e=this;return this.params=t||{},this.vars={selectedProducts:[]},this.fetchData=async t=>{try{const e=await fetch("/graphql/v1/",{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:t});await app_shop.graphql.trackingEvents(e);return await e.json()}catch(t){return!1}},this.getListQuery=t=>{const{idList:e,nameList:s,linkList:i,countList:a}=t;return JSON.stringify({query:`query {\n        shoppingList(ShoppingListInput: {\n          id: ${e}\n        }) {\n          ${void 0!==s&&s?"name":""}\n          ${void 0!==i&&i?"link":""}\n          ${void 0!==a&&a?"productCount":""}\n        }\n      }`})},this.addListQuery=t=>JSON.stringify({query:`mutation {\n      shoppingListCreate(ShoppingListSettingsInput: {\n        name: "${t}",\n        public: true,\n      }) {\n        status\n        id\n      }\n    }`}),this.deleteListQuery=t=>JSON.stringify({query:`mutation {\n      shoppingListDelete(ShoppingListInput: {\n        id: ${t}\n      }) {\n        status\n      }\n    }`}),this.updateListQuery=t=>{const{idList:e,nameList:s,publicList:i}=t;return JSON.stringify({query:`mutation {\n        setShoppingList(ShoppingListInput: {\n          ${void 0!==e?`id: ${e},`:""}\n        }, ShoppingListSettingsInput: {\n          ${void 0!==s?`name: "${s}",`:""}\n          ${void 0!==i?`public: ${i},`:""}\n        }) {\n          status\n        }\n      }`})},this.addProductQuery=(t,e)=>JSON.stringify({query:`mutation {\n      addToShoppingList(ShoppingListInput: {\n        id: ${t}\n      }, ShoppingListProduct: [${e.map((t=>`{\n        id: ${t[0]},\n        size: "${"onesize"===t[1]?"uniw":t[1]}",\n      }`)).join(",")}]) {\n        status\n      }\n    }`}),this.deleteProductQuery=(t,e)=>JSON.stringify({query:`mutation {\n      removeFromShoppingList(ShoppingListInput: {\n        id: ${t}\n      }, ShoppingListProduct: [${e.map((t=>`{\n        id: ${t[0]},\n        size: "${"onesize"===t[1]?"uniw":t[1]}",\n      }`)).join(",")}]) {\n        status\n      }\n    }`}),this.queryCombo=async t=>{const{statusElement:e,fetchDataQuery:s,afterSuccess:i,afterError:a}=t;e&&this.status(e,"loading");const r=await this.fetchData(s);r.data&&(r.data.shoppingListCreate&&"success"===r.data.shoppingListCreate.status||r.data.shoppingListDelete&&"success"===r.data.shoppingListDelete.status||r.data.setShoppingList&&"success"===r.data.setShoppingList.status||r.data.addToShoppingList&&"success"===r.data.addToShoppingList.status||r.data.removeFromShoppingList&&"success"===r.data.removeFromShoppingList.status)?(e&&this.status(e,"success"),i&&i(r)):(e&&this.status(e,"error"),r.errors&&r.errors[0]&&r.errors[0].message&&Alertek.Start(r.errors[0].message),a&&a(r))},this.status=(t,e,s)=>{if(t)switch(e){case"loading":t&&t.classList.add("--loading");break;case"success":t&&(t.classList.remove("--loading"),t.classList.add("--success"),s||setTimeout((function(){t.classList.remove("--success")}),1e3));break;case"error":t&&(t.classList.remove("--loading"),t.classList.add("--error"),s||setTimeout((function(){t.classList.remove("--error")}),1e3));break;default:return t&&t.classList.remove("--loading","--success","--errror"),!1}return!0},this.updateListName=(t,e)=>{document.querySelectorAll(`[data-list_id="${t}"]`).forEach((s=>{s.querySelectorAll("[data-list_name]").forEach((s=>{s.closest("[data-list_id]").getAttribute("data-list_id")===t&&(s.textContent=e)}))}))},this.updateListCount=async t=>{const{idList:e,afterSuccess:s,afterError:i}=t,a=await this.fetchData(this.getListQuery({idList:e,countList:!0}));if(a.data&&a.data.shoppingList){const t=a.data.shoppingList[0].productCount;document.querySelectorAll(`[data-list_id="${e}"]`).forEach((s=>{s.querySelectorAll("[data-list_count]").forEach((s=>{s.closest("[data-list_id]").getAttribute("data-list_id")===e&&(s.textContent=t)}))})),s&&s(a)}else i&&i(a)},this.updateListShare=t=>{document.querySelectorAll(`[data-list_id="${t}"]`).forEach((t=>{t.hasAttribute("data-shared")?t.removeAttribute("data-shared"):t.setAttribute("data-shared","true")}))},this.updateListDelete=t=>{document.querySelectorAll(`[data-list_id="${t}"]`).forEach((t=>{2===t.parentNode.querySelectorAll("[data-list_id]").length&&t.closest("[data-empty]")&&t.closest("[data-empty]").setAttribute("data-empty","true"),t.parentNode.removeChild(t)}))},this.updateListCreate=async t=>{const{idList:e,afterSuccess:s,afterError:i}=t,a=await this.fetchData(this.getListQuery({idList:e,nameList:!0,linkList:!0}));if(a.data&&a.data.shoppingList){const t=a.data.shoppingList[0].name,i=a.data.shoppingList[0].link;document.querySelectorAll("[data-list_skeleton]").forEach((s=>{const a=s.cloneNode(!0);a.hasAttribute("data-list_id")&&a.setAttribute("data-list_id",e),a.removeAttribute("data-list_skeleton"),a.querySelectorAll("[data-list_name]").forEach((e=>{e.textContent=t})),a.querySelectorAll("[data-list_href]").forEach((t=>{t.setAttribute("href",i)})),a.querySelectorAll("[data-list_position]").forEach((t=>{t.hasAttribute("id")&&t.setAttribute("id",`${t.getAttribute("id")}${s.parentNode.childElementCount}`),t.hasAttribute("for")&&t.setAttribute("for",`${t.getAttribute("for")}${s.parentNode.childElementCount}`)})),s.parentNode.appendChild(a),s.closest("[data-empty]")&&s.closest("[data-empty]").setAttribute("data-empty","false")})),s&&s(a)}else i&&i(a)},this.addProductToList=(t,e)=>{const s=document.querySelector(".sl_choose");if(this.callbackAfterAddProductToList=e,s){s.querySelectorAll(".--success").forEach((t=>{t.classList.remove("--success")}));1===s.querySelectorAll(".sl_choose__list").length&&(this.status(s.querySelector(".sl_choose__wrapper"),"loading"),this.queryCombo({fetchDataQuery:this.addListQuery(ShoppingListTxt.firstListName),afterSuccess:t=>{this.updateListCreate({idList:t.data.shoppingListCreate.id,afterSuccess:()=>{s.querySelector(".sl_choose__list:last-child input").checked=!0,this.status(s.querySelector(".sl_choose__wrapper"))}})}})),s.classList.add("--active"),Modal({element:s,classList:"--sl-choose --medium --mobile",afterClose:()=>{s.classList.remove("--active")}})}this.vars.selectedProducts=t},this.initEvents=()=>{document.addEventListener("click",(async function(t){if(t.target.closest(".sl_create__link")){t.preventDefault();const e=t.target.closest(".sl_create__link").parentNode;e.classList.toggle("--active"),e.querySelector("input").focus(),e.scrollIntoView()}if(t.target.closest(".sl_create__button")){t.preventDefault();const s=t.target.closest(".sl_create__button"),i=s.closest(".sl_create").querySelector(".sl_create__input"),a=s.closest(".sl_create");return i.checkValidity()?(e.status(a,"loading"),e.queryCombo({fetchDataQuery:e.addListQuery(i.value),afterSuccess:t=>{e.updateListCreate({idList:t.data.shoppingListCreate.id,afterSuccess:()=>{e.status(a,"success"),a.classList.remove("--active"),i.value="",a.scrollIntoView()},afterError:()=>{e.status(a,"error")}})},afterError:()=>{e.status(a,"error")}}),!1):(i.reportValidity(),!1)}if(t.target.closest(".sl_choose__button")){t.preventDefault();const s=t.target.closest(".sl_choose__button").closest(".sl_choose__wrapper").querySelector(".sl_choose__input:checked"),i=s.closest("[data-list_id]").getAttribute("data-list_id"),a=s.closest(".sl_choose__list");e.status(a,"loading"),e.queryCombo({fetchDataQuery:e.addProductQuery(i,e.vars.selectedProducts),afterSuccess:()=>{e.updateListCount({idList:i,afterSuccess:()=>{e.status(a,"success",!0)},afterError:()=>{e.status(a,"error")}}),"function"==typeof e.callbackAfterAddProductToList&&e.callbackAfterAddProductToList()},afterError:()=>{e.status(a,"error")}})}return!1}))},this.initEvents()};