import{t,p as e,H as s,c as a,h as i,d as o}from"./p-CAus4tQt.js";import{u as c}from"./p-DrCYkHs5.js";import{Z as r}from"./p-Yhz3X6iS.js";import{Z as d}from"./p-DBryW4-h.js";import{Z as n}from"./p-Ctdd_POP.js";import{k as m}from"./p-BZ9XO6U2.js";import{p as l}from"./p-aW_PRAJY.js";import{g as p}from"./p-DKrliuRO.js";import{n as f}from"./p-BqKsA7Kw.js";import{g as h,h as g}from"./p-CTyRvZ3_.js";import{g as u}from"./p-rZK2jtkS.js";import{h as k}from"./p-Doda7dHc.js";import{b}from"./p-CNNbCnBf.js";import{d as y}from"./p-wR0dqPJE.js";import{d as j}from"./p-CQ5Pa975.js";import{d as C}from"./p-Bq0MNGjT.js";const T=e(class extends s{constructor(t){super(),!1!==t&&this.__registerHost(),this.ticketTagsUpdated=a(this,"ticketTagsUpdated",7)}ticket;ticketTagsUpdated;isEditTag=!1;isAddTag=!1;tempTagItems=[];t=c();async componentWillLoad(){p.get("tags")?.length||l()}onSyncTagComplete(){l()}handleEditTag=()=>{this.isEditTag=!0,this.isAddTag=!1,this.tempTagItems=this.ticket.tags.map(((t,e)=>{const s=p.get("tags")?.find((e=>e.name===t.tag));return{item:{id:s?.id||t.tag_id||t.id||`temp-${e}`,name:t.tag,color:s?.color||b,sequence:e},isUnmapped:!s}}))};handleCloseTag=()=>{this.isEditTag=!1,this.isAddTag=!1,this.tempTagItems=[]};handleDeleteTag=t=>{this.tempTagItems=this.tempTagItems.map((e=>e.item.id===t?{...e,isDelete:!0}:e))};handleAddTag=t=>{if(!t)return;const e=this.tempTagItems.find((e=>e.item.id===t.id||e.item.name===t.name));e?e.isDelete&&(this.tempTagItems=this.tempTagItems.map((e=>e.item.id===t.id||e.item.name===t.name?{...e,isDelete:!1}:e))):this.tempTagItems=[...this.tempTagItems,{item:t,isAdd:!0}]};handleSaveUserTags=async()=>{const t=this.tempTagItems.filter((t=>!t.isDelete)),e=t.filter((t=>t.isUnmapped));e.length>0&&await Promise.all(e.map((t=>f({name:t.item.name,color:b}).catch((t=>k(t,"Failed to create unmapped user tag"))))));const s=t.map((t=>t.item.name));this.ticketTagsUpdated.emit(s),this.handleCloseTag()};createTagConfig=async t=>{try{const e=h(),s=g(e),a=u(e);if(!a)throw new Error("Business user not found");const[i]=await Promise.all([m({connectionId:s},{data:{business_id:a.business_id,tag:t}}),f({name:t,color:b})]);return{id:i.id,name:i.tag}}catch(t){throw k(t,"Failed to create tag config"),t}};render(){return i(o,{key:"1bb25a20d01dbeb2e4252081b150923e6424c0dc"},i("div",{key:"b44c5cddcfdf38536f8483a7c4cef5866c63eedd",class:"space-y-3"},i("div",{key:"82eb1c0b52936772f44286aa4affccfc8c7018c1",class:"flex items-center justify-between"},i("span",{key:"d3d27f4532b7d0dc721a51507ca2a9c2de9d5576",class:"text-surface font-semibold"},this.t("ticket_field.tags")),this.isEditTag?i("div",{class:"flex items-center gap-2"},i(r,{variant:"text",iconName:"X",addClassIcon:"size-4 text-primary",noPadding:!0,noPaddingY:!0,onClick:this.handleCloseTag}),i(r,{variant:"text",iconName:"Save",addClassIcon:"size-4 text-primary",noPadding:!0,noPaddingY:!0,onClick:this.handleSaveUserTags})):i(r,{variant:"text",iconName:"Pencil",addClassIcon:"size-4 text-primary",noPadding:!0,noPaddingY:!0,onClick:this.handleEditTag})),i("div",{key:"3533d128a29392311d88871932b921f3314091ab",class:"flex flex-wrap gap-2"},this.isEditTag?i("div",{class:"w-full"},i("div",{class:"flex flex-wrap gap-2"},this.tempTagItems.filter((({isDelete:t})=>!t)).map((t=>i(n,{item:t.item,onDelete:()=>this.handleDeleteTag(t.item.id)})))),i("div",{class:"mt-2"},this.isAddTag?i("autocomplete-select",{referenceSelector:"ticket-tag-select",type:"tag",selectedIds:this.tempTagItems.filter((({isDelete:t})=>!t)).map((({item:t})=>t.id)),fetchItems:l,createItem:t=>this.createTagConfig(t),onSelecting:t=>this.handleAddTag(t.detail)}):i(d,{variant:"outlined",fullWidth:!0,label:`+ ${this.t("profile_tab.add_tag")}`,onClick:()=>this.isAddTag=!0}))):this.ticket.tags.map((t=>{const e=p.get("tags")?.find((e=>e.name===t.tag));return i("tag-or-status-item",{chatId:this.ticket.id.toString(),item:{id:e?.id||t.tag_id||t.id||t.tag,name:t.tag,color:e?.color||b}})})))))}},[512,"profile-ticket-tags",{ticket:[16],isEditTag:[32],isAddTag:[32],tempTagItems:[32]},[[8,"syncTagComplete","onSyncTagComplete"]]]);function x(){"undefined"!=typeof customElements&&["profile-ticket-tags","autocomplete-select","status-badge","tag-or-status-item"].forEach((e=>{switch(e){case"profile-ticket-tags":customElements.get(t(e))||customElements.define(t(e),T);break;case"autocomplete-select":customElements.get(t(e))||y();break;case"status-badge":customElements.get(t(e))||j();break;case"tag-or-status-item":customElements.get(t(e))||C()}}))}x();export{T as P,x as d}