import{a as x}from"./chunk-EG5GZRMN.js";import{b as le}from"./chunk-3P6GZXAL.js";import{a as pe}from"./chunk-UB23RF7S.js";import{f as E,g as me}from"./chunk-4ENQACUL.js";import{a as ce}from"./chunk-LTUVDLS3.js";import{d as se}from"./chunk-ICTDV7DB.js";import{a as ne}from"./chunk-ISCIGLF2.js";import{J as ae}from"./chunk-CWWVHRJU.js";import{a as ie}from"./chunk-RU43IR5J.js";import{a as oe}from"./chunk-Y7IP5WLG.js";import{a as re}from"./chunk-SU6CJJJO.js";import{L as h,N as g,T as D,U as Z,ba as ee,g as K,i as X,m as k}from"./chunk-FBI7JGZC.js";import{a as te}from"./chunk-ILYMG6ZI.js";import{t as J}from"./chunk-MXUIPT66.js";import{Aa as n,Ab as f,Ba as a,Bb as N,Cb as v,Db as I,Dc as G,Fb as H,Gd as Y,Kb as O,Lb as o,N as B,Og as z,Ua as S,Va as l,Xb as j,Xc as Q,a as F,bd as $,d as M,ib as b,ob as u,pa as L,rc as U,s as R,sc as W,t as P,ta as V,wc as q,zb as T}from"./chunk-N6B2OJOY.js";function de(s,c){s&1&&(v(0),T(1,"div",2),N(2,"cx-spinner"),f(),I())}function he(s,c){if(s&1){let i=H();v(0),T(1,"foerch-cart-body",4),U(2,"async"),O("addEntryToCart",function(t){n(i);let r=o(2);return a(r.addEntryToCart(t))})("applyCostCenterToEntry",function(t){n(i);let r=o(2);return a(r.applyCcmToEntry(t))})("markEntry",function(t){n(i);let r=o(2);return a(r.markCartEntry(t))})("removeEntry",function(t){n(i);let r=o(2);return a(r.removeEntry(t))})("replaceArticle",function(t){n(i);let r=o(2);return a(r.replaceArticle(t))})("sortingChange",function(t){n(i);let r=o(2);return a(r.sortingChange(t))})("sortingReset",function(){n(i);let t=o(2);return a(t.sortingReset())})("updateComment",function(t){n(i);let r=o(2);return a(r.updateComment(t))})("vpeChange",function(t){n(i);let r=o(2);return a(r.vpeChange(t))}),f(),T(3,"foerch-cart-footer",5),O("applyBulkAction",function(t){n(i);let r=o(2);return a(r.applyBulkAction(t))})("markToggle",function(){n(i);let t=o(2);return a(t.toggleAll())}),f(),I()}if(s&2){let i=o(2);S(),u("cartConfig",i.cartConfig)("checkoutInfo",W(2,11,i.checkoutInfo$))("isLoading$",i.isPending$)("items",i.orderTemplateEntries)("selectionHelper",i.selectionHelper)("sortableColumns",i.sortableColumns)("sortModel",i.sortModel),S(2),u("bulkOptions",i.bulkActionOptions)("footerConfig",i.footerConfig)("hasAnyDeletedArticle",i.hasAnyDeletedArticle)("selectAll",i.selectionHelper.areAllSelected())}}function _e(s,c){if(s&1&&b(0,he,4,13,"ng-container",3),s&2){let i=o();u("ngIf",!i.isEmptyOrderTemplate)}}var Je=(()=>{let c=class c{constructor(e,t,r,_,m,p,d,C,y){this.activeCartService=e,this.watchlistService=t,this.orderTemplateService=r,this.barcodeCatalogItemService=_,this.permissionsService=m,this.modalService=p,this.cd=d,this.activatedRoute=C,this.translationService=y,this.iconTypes=te,this.cartModes=k,this.orderTemplateEntries=[],this.isEmptyOrderTemplate=!0,this.isLoggedIn=!1,this.markItems=!1,this.selectionHelper=new ce,this.hasAnyDeletedArticle=!1,this.watchlistItems=[],this.isEmptyWatchList=!0,this.cartConfig={isEditable:!0,cartMode:k.ORDER_TEMPLATE_DETAILS,highlightDeletedArticles:!0,showAddToCart:!0,showCCM:!0,showVPE:!0,showPrice:!0,showRememberedAt:!1,showVolumePrices:!0},this.footerConfig={addCoupon:!1,showCheckout:!1,showTotal:!1,disableAll:!1},this.bulkActionOptions=[h.ADD_TO_CART,h.DELETE,h.ADD_TO_BARCODE_CATALOG,h.ADD_TO_WATCH_LIST],this.sortableColumns=[E.ARTICLE_NUMBER,E.ARTICLE_NAME,E.TOTAL],this.defaultSortModel={sortBy:"code",sortDirection:ee.ASC},this.sortModel=this.defaultSortModel,this.articleNumberMode=Z.FOERCH,this.loading=!0,this.isPending$=P([this.activeCartService.isLoading(),this.orderTemplateService.isMergePending()]).pipe(R(([w,A])=>(this.isPending=w||A,this.isPending))),this.checkoutInfo$=new M(null),this.subs$=new F,this.isPending=!1,this.addablePipe=L(ne),this.orderTemplateCode=C.snapshot.params.code,this.orderTemplateService.clearState()}ngOnInit(){this.loading=!0,this.orderTemplateService.loadOrderTemplateById(this.orderTemplateCode),this.subs$.add(this.orderTemplateService.getOrderTemplate().subscribe(e=>{if(e){this.orderTemplate=e,this.orderTemplateEntries=e.entries?x(e.entries,this.sortModel,this.articleNumberMode):[];let t=[g.DELETED,g.UNAVAILABLE,g.ONLY_AVAILABLE_WITHIN_ASSORTMENT];this.hasAnyDeletedArticle=this.orderTemplateEntries.some(r=>t.includes(r.product?.saleability?.status)),this.isEmptyOrderTemplate=this.orderTemplateEntries.length===0,this.selectionHelper.updateItems(this.isEmptyOrderTemplate?[]:this.orderTemplateEntries.map(r=>({id:r.pk,item:r}))),this.loading=!1,this.checkForNonPurchasableEntries(),this.cd.detectChanges()}})),this.subs$.add(this.watchlistService.getEntries().subscribe(e=>{this.watchlistItems=e,this.isEmptyWatchList=!e||e.length===0})),this.subs$.add(this.permissionsService.getPermissionValueAsBoolean(D.COST_CENTER_MANAGEMENT_TOOL).subscribe(e=>this.cartConfig.showCCM=e)),this.subs$.add(this.permissionsService.getPermissionValue(D.ACTIVATE_WEBSHOP_ARTICLE_NUMBER).subscribe(e=>{e&&(this.articleNumberMode=e,this.cd.detectChanges())}))}ngOnDestroy(){this.subs$.unsubscribe()}changeTemplateName(e){this.orderTemplateService.changeTemplateName(this.orderTemplateCode,e)}removeEntry(e){this.orderTemplateService.removeEntries({cartId:this.orderTemplateCode,entries:[e.item]})}updateComment(e){this.orderTemplateService.updateEntryComment({cartId:this.orderTemplateCode,comment:e.comment,pk:e.item.pk})}applyCostCenterAndObject(e){let t=e.toAll?this.orderTemplateEntries:this.selectionHelper.getSelectedItems().map(r=>r.item);this.orderTemplateService.applyCostCenterForEntries({cartId:this.orderTemplateCode,costCenter:e.costCenter.code,costReason:e.purchaseReason?e.purchaseReason.code:null,entries:t})}applyCcmToEntry(e){this.orderTemplateService.applyCostCenterForEntries({cartId:this.orderTemplateCode,costCenter:e.costCenter,costReason:e.orderReason,entries:[e.item]})}quickAddEntriesToCart(e){this.orderTemplateService.addEntries({cartId:this.orderTemplateCode,entries:e})}markCartEntry(e){this.selectionHelper.toggleItemStatus(e.item.pk),this.applyMarkToOrderTemplate()}toggleAll(){this.selectionHelper.toggleAll(),this.selectionHelper=this.selectionHelper.clone(),this.applyMarkToOrderTemplate()}applyMarkToOrderTemplate(){let e=this.selectionHelper.getSelectedItems().map(t=>t.item.pk);this.orderTemplateService.applyMarkToOrderTemplate(e,this.orderTemplateCode)}applyBulkAction(e){let t=this.selectionHelper.getSelectedItems().map(r=>r.item);if(t.length>0)switch(e){case h.DELETE:{this.orderTemplateService.removeEntries({cartId:this.orderTemplateCode,entries:t});break}case h.ADD_TO_CART:{if(this.isPending===!1){let m=t.filter(p=>this.addablePipe.transform(p.product?.saleability));if(m.length>0){let p=m.map(d=>d.pk);this.orderTemplateService.safeAddToCart(this.orderTemplateCode,p,!1)}}break}case h.ADD_TO_BARCODE_CATALOG:this.translationService.translate("barcodeCatalogSources.orderTemplate").pipe(B(1)).subscribe(m=>{let p=t.map(d=>{let C=d.product,y={code:C.code,name:C.baseProductName,source:[{type:X.ORDER_TEMPLATE,name:m,code:this.orderTemplate.code}],customArticleCode:C.customArticleCode||null,supplierAlternativeAID:C.supplierAlternativeAID||null,saleability:C.saleability||null};return Object.fromEntries(Object.entries(y).filter(([w,A])=>A!==null))});this.createBarcode(p)});break;case h.ADD_TO_WATCH_LIST:let r=new Set,_=t.filter(m=>this.addablePipe.transform(m.product?.saleability));if(_.length>0){_.forEach(d=>{r.add(d.product.code)});let p=Array.from(r).map(d=>({code:d,qty:1}));this.watchlistService.addEntries(p)}break;default:console.error("unimplemented option",e)}}vpeChange(e){let t=null;if(e.packagingUnits){let r=e.packagingUnits.find(_=>_.numberContentUnit===e.selectedVPE);t=r?r.materialCode:null}this.orderTemplateService.updateQuantity({cartId:this.orderTemplateCode,pk:e.code,qty:e.quantity,material:t})}replaceArticle(e){if(e){let t=e.replacement?.preferredMaterialAndQuantity;e.item?.pk&&t&&this.orderTemplateService.updateQuantity({cartId:this.orderTemplateCode,pk:e.item.pk,qty:t.quantity||1,material:t.materialCode})}}addEntryToCart(e){this.orderTemplateService.addEntryToCartById(e.item.pk,this.orderTemplateCode)}sortingReset(){this.sortingChange(this.defaultSortModel)}sortingChange(e){this.sortModel=e,this.orderTemplateEntries=x(this.orderTemplateEntries,this.sortModel,this.articleNumberMode)}createBarcode(e){let t={name:this.orderTemplate.name,entries:e};this.barcodeCatalogItemService.presetBarcodeCatalog(t)}checkForNonPurchasableEntries(){this.orderTemplateEntries.some(e=>e.product?.saleability?.purchasable===!1)?this.checkoutInfo$.next({allowed:!1,reason:K.NOT_PURCHASABLE_ORDER_TEMPLATE}):this.checkoutInfo$.next(null)}};c.\u0275fac=function(t){return new(t||c)(l(ae),l(re),l(pe),l(se),l(ie),l(oe),l(G),l(Y),l(z))},c.\u0275cmp=V({type:c,selectors:[["foerch-order-template-detail"]],decls:3,vars:2,consts:[["loaded",""],[4,"ngIf","ngIfElse"],[2,"height","300px"],[4,"ngIf"],[3,"addEntryToCart","applyCostCenterToEntry","markEntry","removeEntry","replaceArticle","sortingChange","sortingReset","updateComment","vpeChange","cartConfig","checkoutInfo","isLoading$","items","selectionHelper","sortableColumns","sortModel"],[3,"applyBulkAction","markToggle","bulkOptions","footerConfig","hasAnyDeletedArticle","selectAll"]],template:function(t,r){if(t&1&&b(0,de,3,0,"ng-container",1)(1,_e,1,1,"ng-template",null,0,q),t&2){let _=j(2);u("ngIf",r.loading)("ngIfElse",_)}},dependencies:[me,le,J,Q,$],encapsulation:2});let s=c;return s})();export{Je as a};