import{Host,h}from"@stencil/core";import{__,_n,sprintf}from"@wordpress/i18n";import apiFetch from"../../../../functions/fetch";import{addQueryArgs}from"@wordpress/url";export class ScFulfillments{constructor(){this.orderId=void 0,this.heading=void 0,this.fulfillments=void 0,this.loading=void 0,this.busy=void 0,this.error=void 0}componentDidLoad(){this.fetch()}async fetch(){try{this.busy=!0,this.fulfillments=await apiFetch({path:addQueryArgs("surecart/v1/fulfillments",{expand:["trackings","fulfillment_items","fulfillment_item.line_item","line_item.price","price.product"],order_ids:[this.orderId],shipment_status:["shipped","delivered"]})})}catch(i){console.error(this.error),this.error=(null==i?void 0:i.message)||__("Something went wrong","surecart")}finally{this.busy=!1}}renderLoading(){return h("sc-flex",{flexDirection:"column",style:{gap:"1em"}},h("sc-skeleton",{style:{width:"20%",display:"inline-block"}}),h("sc-skeleton",{style:{width:"60%",display:"inline-block"}}),h("sc-skeleton",{style:{width:"40%",display:"inline-block"}}))}render(){var i,t;return this.loading||!(null===(i=this.fulfillments)||void 0===i?void 0:i.length)?h(Host,{style:{display:"none"}}):h("sc-spacing",{style:{"--spacing":"var(--sc-spacing-large)"}},h("sc-dashboard-module",{error:this.error},h("span",{slot:"heading"},this.heading||_n("Shipment","Shipments",null===(t=this.fulfillments)||void 0===t?void 0:t.length,"surecart")),this.fulfillments.map((i=>{var t,l,e,s,n,r;return h("sc-card",{noPadding:!0},h("sc-stacked-list",null,h("sc-stacked-list-row",null,h("div",{class:"fulfillment__header"},h("sc-fulfillment-shipping-status-badge",{status:i.shipment_status}),h("div",{class:"fulfillment__number"},"#",null==i?void 0:i.number))),!!(null===(l=null===(t=null==i?void 0:i.trackings)||void 0===t?void 0:t.data)||void 0===l?void 0:l.length)&&h("sc-stacked-list-row",null,h("div",{class:"trackings"},h("sc-icon",{name:"truck"}),h("div",{class:"trackings__details"},h("div",{class:"trackings__title"},_n("Tracking number","Tracking numbers",null===(s=null===(e=null==i?void 0:i.trackings)||void 0===e?void 0:e.data)||void 0===s?void 0:s.length,"surecart")),h("div",{class:"trackings__list"},((null===(n=null==i?void 0:i.trackings)||void 0===n?void 0:n.data)||[]).map((({number:i,url:t})=>h("a",{href:t,target:"_blank"},i))))))),((null===(r=null==i?void 0:i.fulfillment_items)||void 0===r?void 0:r.data)||[]).map((({id:i,line_item:t,quantity:l})=>{var e,s,n,r,d,o,a,u,c,v,m,g;return h("sc-stacked-list-row",{key:i,style:{"--columns":"2"}},h("div",null,h("div",{class:"line_item__info"},h("div",{class:"line_item__image"},!!(null===(s=null===(e=null==t?void 0:t.price)||void 0===e?void 0:e.product)||void 0===s?void 0:s.line_item_image)&&h("img",{...null===(r=null===(n=null==t?void 0:t.price)||void 0===n?void 0:n.product)||void 0===r?void 0:r.line_item_image})),h("div",{class:"line_item__text"},h("div",null,null===(o=null===(d=null==t?void 0:t.price)||void 0===d?void 0:d.product)||void 0===o?void 0:o.name),h("div",null,!!(null===(u=null===(a=null==t?void 0:t.price)||void 0===a?void 0:a.product)||void 0===u?void 0:u.weight)&&h("sc-format-number",{type:"unit",value:null===(v=null===(c=null==t?void 0:t.price)||void 0===c?void 0:c.product)||void 0===v?void 0:v.weight,unit:null===(g=null===(m=null==t?void 0:t.price)||void 0===m?void 0:m.product)||void 0===g?void 0:g.weight_unit}))))),h("span",null,sprintf(__("Qty: %d","surecart"),l||0)))}))))}))))}static get is(){return"sc-fulfillments"}static get encapsulation(){return"shadow"}static get originalStyleUrls(){return{$:["sc-fulfillments.scss"]}}static get styleUrls(){return{$:["sc-fulfillments.css"]}}static get properties(){return{orderId:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"order-id",reflect:!1},heading:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"heading",reflect:!1}}}static get states(){return{fulfillments:{},loading:{},busy:{},error:{}}}static get elementRef(){return"el"}}