import{d as h}from"./datatables-I6jthWPb.js";import{r as y,g as p}from"./api-DZ3cXm1m.js";import{a as w,s as m}from"./toastr-helper-Wdj0mZjX.js";import{i as x}from"./tooltip-manager-D9RaGGY4.js";import"./bootstrap.esm-Bb7dPm-H.js";import"./datatables-Bo82YvhT.js";/* empty css                                  */let b;const{permissions:g,routes:u,translations:a}=window;function T(){const i=document.getElementById("trainingDatasetsTable");i&&(b=$(i).DataTable({...h.getDefaultConfig(),serverSide:!0,dom:'<"d-flex justify-content-between align-items-center mb-3"<"d-flex align-items-center"l><"d-flex align-items-center"f>>t<"d-flex justify-content-between align-items-center mt-3"<"d-flex align-items-center"i><"d-flex align-items-center"p>>',ajax:{url:u.records,type:"GET",beforeSend:function(e){e.setRequestHeader("Authorization",`Bearer ${p()}`)},error:async function(e){e.status===401&&await y()&&b.ajax.reload()}},columns:[{data:"name",name:"name",render:function(e,n,s){var t;return(t=g.aiTraining)!=null&&t.update?`<a href="${u.edit.replace(":id",s.id)}" class="text-decoration-none fw-bold">${e}</a>`:e}},{data:"type",name:"type",render:function(e){var t;const n={conversation:"bg-primary",knowledge:"bg-info",instruction:"bg-warning",faq:"bg-success"},s=((t=a.types)==null?void 0:t[e])||e;return`<span class="badge ${n[e]||"bg-secondary"}">${s}</span>`}},{data:"status",name:"status",render:function(e){var t;const n={draft:"bg-secondary",ready:"bg-info",training:"bg-warning",completed:"bg-success",failed:"bg-danger"},s=((t=a.statuses)==null?void 0:t[e])||e;return`<span class="badge ${n[e]||"bg-secondary"}">${s}</span>`}},{data:"item_count",name:"item_count",className:"text-center",render:function(e){return e??"0"}},{data:"creator_name",name:"creator_name",render:function(e){return e||"-"}},{data:"created_at",name:"created_at",render:function(e){return e?`<small>${e}</small>`:"-"}},{data:"id",name:"actions",orderable:!1,searchable:!1,className:"text-end",render:function(e,n,s){var o,c,l,d;const t=[];if((o=g.aiTraining)!=null&&o.update){const r=u.edit.replace(":id",e);t.push(`
                            <a href="${r}"
                               class="btn btn-sm btn-outline-primary d-inline-flex align-items-center justify-content-center"
                               style="width: 32px; height: 32px;"
                               data-bs-toggle="tooltip"
                               title="${((c=a.buttons)==null?void 0:c.edit)||"Edit"}">
                                <i class="fas fa-edit"></i>
                            </a>`)}return(l=g.aiTraining)!=null&&l.delete&&t.push(`
                            <button type="button"
                                    class="btn btn-sm btn-outline-danger d-inline-flex align-items-center justify-content-center ms-1 delete-btn"
                                    style="width: 32px; height: 32px;"
                                    data-id="${e}"
                                    data-bs-toggle="tooltip"
                                    title="${((d=a.buttons)==null?void 0:d.delete)||"Delete"}">
                                <i class="fas fa-trash"></i>
                            </button>`),t.length===0?'<span class="text-muted">-</span>':`<div class="d-inline-flex justify-content-end">${t.join("")}</div>`}}],drawCallback:function(){x()}}))}async function j(i){var s,t,o,c,l,d;if(!await w(((s=a.messages)==null?void 0:s.confirm_delete)||"Are you sure you want to delete this training dataset?",{confirmClass:"btn-danger",confirmText:((t=a.buttons)==null?void 0:t.confirm)||"Confirm",cancelText:((o=a.buttons)==null?void 0:o.cancel)||"Cancel"}))return;const n=u.destroy.replace(":id",i);try{let r=await fetch(n,{method:"DELETE",headers:{Authorization:`Bearer ${p()}`,Accept:"application/json"}});r.status===401&&await y()&&(r=await fetch(n,{method:"DELETE",headers:{Authorization:`Bearer ${p()}`,Accept:"application/json"}}));const f=await r.json();f.success?(m("success",f.message||((c=a.messages)==null?void 0:c.deleted)||"Training dataset deleted successfully."),b.ajax.reload(null,!1)):m("error",f.message||((l=a.messages)==null?void 0:l.error)||"An error occurred.")}catch(r){console.error("Delete error:",r),m("error",((d=a.messages)==null?void 0:d.error)||"An error occurred.")}}function A(){document.addEventListener("click",function(i){const e=i.target.closest(".delete-btn");if(e){const n=e.getAttribute("data-id");j(n)}})}document.addEventListener("DOMContentLoaded",function(){T(),A()});
