PageSourceSearch

https://sabeeorbit.netlify.app/assets/Knowledge-DdNccbnM.js

js sabeeorbit.netlify.app collected 2026-10-03 11:23:10 UTC 179,490 bytes, 33 lines download raw bytes

1import{cE as Qs,cn as s,D as ts,bi as Ma,cx as U,c5 as e,H as cs,E as dt,z as Ke,R as ke,p as lt,bl as pt,q as Vt,r as De,bT as ct,S as Zs,s as Se,bU as bt,J as is,w as os,P as Vs,X as Ta,Y as La,ck as Ea,V as Hs,aR as yt,az as ea,G as et,y as ds,cN as Ra,t as ss,cP as Pa,v as Ue,B as Os,aG as ht,ae as Ut,x as as,a$ as qs,a8 as Ws}from"./index-BUmD633_.js";import{b as rs,a as $a}from"./pageHeaderStyles-B2n4gYO_.js";import{B as za}from"./BaseModal-DNPj7u_C.js";import{A as Ua}from"./ArticleEditor-DaqE9myN.js";import{c as Gs,a as Ba}from"./formatters-CQescvMV.js";import{F as Va}from"./BookmarkIcon-BZSHw8xX.js";import{a as Ha,g as Oa,i as Ks,b as ls,p as qa}from"./excelParser-ChZ5dmSq.js";import{F as ta}from"./ArrowUpTrayIcon-CUgenDBI.js";import{F as Ve}from"./ArrowDownIcon-DHuxrTRZ.js";import"./index-Cb9gU7OP.js";import"./index-BolgZwsO.js";import"./StrikethroughIcon-CTRGBeSi.js";import"./NumberedListIcon-D8w-sbLE.js";import"./ArrowUturnRightIcon-X5JhYtLL.js";import"./ArrowsPointingOutIcon-DaggEhwa.js";import"./xlsx-detbQWJx.js";const Wa=({isOpen:f,onClose:v,onSuccess:b,categories:o})=>{const{profile:g}=Qs(),[W,ee]=s.useState("url"),[ce,S]=s.useState(!1),[p,G]=s.useState(null),[E,ye]=s.useState(!1),[me,ae]=s.useState(""),[te,P]=s.useState(""),[k,B]=s.useState(""),[be,se]=s.useState(""),[le,pe]=s.useState(""),[q,Z]=s.useState(""),[V,c]=s.useState(""),[M,ne]=s.useState(""),[_,n]=s.useState(""),[D,w]=s.useState(ts),[N,a]=s.useState("PUBLISHED"),[h,I]=s.useState("v3.7"),[H,X]=s.useState(""),[Re,Ne]=s.useState([]),[He,_e]=s.useState([]),[Oe,je]=s.useState(!1),[Pe,qe]=s.useState("");Ma.useEffect(()=>{f&&(ve(),Je())},[f]);const Je=async()=>{try{const{data:l,error:d}=await U.from("knowledge_modules").select("id, name, slug").eq("is_active",!0).order("order_index",{ascending:!0});if(d)throw d;_e(l||[]);const J=l==null?void 0:l.find(L=>L.slug==="pms");J&&!q&&Z(J.id)}catch(l){console.error("Failed to fetch modules:",l)}},ve=async()=>{try{const{data:l,error:d}=await U.from("openai_vector_stores").select("id, name, is_default, is_active").eq("is_active",!0).in("store_type",["knowledge_articles","mixed"]).order("name",{ascending:!0});if(d)throw d;Ne(l||[]);const J=l==null?void 0:l.find(L=>L.is_default);J&&X(J.id)}catch(l){console.error("Failed to fetch vector stores:",l)}};if(!f)return null;const Te=q?o.filter(l=>l.module_id===q):o,Fe=Te.filter(l=>!l.parent_id),i=l=>Te.filter(d=>d.parent_id===l),u=l=>{Z(l),c(""),ne("")},O=async()=>{if(!me.trim()){G("Please enter a URL");return}ye(!0),G(null);try{const l="https://solxiyfrzksdatdzaxyy.supabase.co/functions/v1/fetch-url-content",{data:{session:d}}=await U.auth.getSession(),J=await fetch(l,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${d==null?void 0:d.access_token}`},body:JSON.stringify({url:me})}),L=await J.json();if(!J.ok)throw new Error(L.error||"Failed to fetch URL content");P(L.title),B(L.content)}catch(l){G(l.message||"Failed to fetch URL content")}finally{ye(!1)}},C=async()=>{if(!Pe.trim()){G("Please enter a category name");return}try{const l=o.filter(ge=>!ge.parent_id),d=l.length>0?Math.max(...l.map(ge=>ge.order_index||0)):-1,{data:J,error:L}=await U.from("knowledge_categories").insert({name:Pe.trim(),parent_id:null,order_index:d+1}).select().single();if(L)throw L;c(J.id),qe(""),je(!1),b()}catch(l){G(l.message||"Failed to create category")}},T=async()=>{S(!0),G(null);try{const l=W==="url"?te:be,d=W==="url"?k:le;if(!l.trim())throw new Error("Title is required");if(!d.trim())throw new Error("Content is required");const J=_.split(",").map(Y=>Y.trim()).filter(Y=>Y.length>0);if(!q)throw new Error("Please select a module");const L={title:l.trim(),content:d.trim(),source_type:W,tags:J,language:D,version:h,status:N,user_id:g==null?void 0:g.id,created_by_email:g==null?void 0:g.email,module_id:q};
1W==="url"&&(L.source_url=me),V&&(L.category_id=V),M&&(L.subcategory_id=M),H&&(L.target_vector_store_id=H);const{error:ge}=await U.from("knowledge_articles").insert(L);if(ge)throw ge;ae(""),P(""),B(""),se(""),pe(""),Z(""),c(""),ne(""),n(""),w(ts),a("PUBLISHED"),I("v3.7"),X(""),b(),v()}catch(l){G(l.message||"Failed to save article")}finally{S(!1)}},K=()=>{ae(""),P(""),B(""),se(""),pe(""),Z(""),c(""),ne(""),n(""),w(ts),a("PUBLISHED"),I("v3.7"),X(""),G(null),ee("url"),v()};return e.jsxs(za,{isOpen:f,onClose:K,title:"Add Knowledge Article",size:"xl",isProcessing:ce,footer:e.jsxs("div",{className:"flex justify-end space-x-3",children:[e.jsx("button",{onClick:K,className:"btn-secondary",children:"Cancel"}),e.jsx("button",{onClick:T,disabled:ce||(W==="url"?!te||!k:!be||!le),className:"btn-primary",children:ce?"Saving...":"Save Article"})]}),children:[e.jsxs("div",{className:"flex space-x-2 mb-6 border-b border-gray-200",children:[e.jsxs("button",{onClick:()=>ee("url"),className:`px-4 py-2 text-sm font-medium transition-colors ${W==="url"?"text-blue-600 dark:text-blue-400 border-b-2 border-blue-600":"text-gray-600 dark:text-gray-400 dark:text-gray-500 hover:text-gray-900"}`,children:[e.jsx(cs,{className:"w-4 h-4 inline-block mr-2"}),"From URL"]}),e.jsxs("button",{onClick:()=>ee("raw"),className:`px-4 py-2 text-sm font-medium transition-colors ${W==="raw"?"text-blue-600 dark:text-blue-400 border-b-2 border-blue-600":"text-gray-600 dark:text-gray-400 dark:text-gray-500 hover:text-gray-900"}`,children:[e.jsx(dt,{className:"w-4 h-4 inline-block mr-2"}),"Raw Text"]})]}),p&&e.jsxs("div",{className:"mb-4 p-3 bg-red-50 dark:bg-red-950/30 border border-red-200 dark:border-red-800 rounded-lg flex items-start",children:[e.jsx(Ke,{className:"w-5 h-5 text-red-600 dark:text-red-400 mr-2 flex-shrink-0 mt-0.5"}),e.jsx("span",{className:"text-sm text-red-600",children:p})]}),e.jsxs("div",{className:"space-y-4",children:[W==="url"?e.jsxs(e.Fragment,{children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Article URL"}),e.jsxs("div",{className:"flex space-x-2",children:[e.jsx("input",{type:"url",value:me,onChange:l=>ae(l.target.value),placeholder:"https://example.com/article",className:"flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500",disabled:E}),e.jsxs("button",{onClick:O,disabled:E||!me.trim(),className:"btn-secondary flex items-center",children:[e.jsx(ke,{className:`w-4 h-4 mr-2 ${E?"animate-spin":""}`}),"Fetch"]})]})]}),te&&e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Title"}),e.jsx("input",{type:"text",value:te,onChange:l=>P(l.target.value),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"})]}),k&&e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Content"}),e.jsx("textarea",{value:k,onChange:l=>B(l.target.value),rows:10,className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"})]})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Title"}),e.jsx("input",{type:"text",value:be,onChange:l=>se(l.target.value),placeholder:"Enter article title",className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Content"}),e.jsx("textarea",{value:le,onChange:l=>pe(l.target.value),placeholder:"Enter or paste article content",rows:10,className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Module *"}),e.jsxs("select",{value:q,onChange:l=>u(l.target.value),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500",required:!0,children:[e.jsx("option",{value:"",children:"Select module"}),He.map(l=>e.jsx("option",{value:l.id,children:l.name},l.id))]}),e.jsx("p",{className:"mt-1 text-xs text-gray-500",children:"Select the product module for this article"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Category"}),Oe?e.jsxs("div",{className:"flex space-x-2",children:[e.jsx("input",{type:"text",value:Pe,onChange:l=>qe(l.target.value),placeholder:"New category name",className:"flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"}),e.jsx("button",{onClick:C,className:"btn-primary text-sm px-3",children:"Add"}),e.jsx("button",{onClick:()=>{je(!1),qe("")},className:"btn-secondary text-sm px-3",children:"Cancel"})]}):e.jsxs("select",{value:V,onChange:l=>{l.target.value==="__new__"?(je(!0),c("")):(c(l.target.value),ne(""))},className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500",children:[e.jsx("option",{value:"",children:"Select category"}),Fe.map(l=>e.jsx("option",{value:l.id,children:l.name},l.id)),e.jsx("option",{value:"__new__",children:"+ Create New Category"})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Subcategory"}),e.jsxs("select",{value:M,onChange:l=>ne(l.target.value),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500",disabled:!V,children:[e.jsx("option",{value:"",children:"Select subcategory"}
1),V&&i(V).map(l=>e.jsx("option",{value:l.id,children:l.name},l.id))]})]})]}),e.jsxs("div",{children:[e.jsxs("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:[e.jsx(lt,{className:"w-4 h-4 inline mr-1"}),"Language *"]}),e.jsx("select",{value:D,onChange:l=>w(l.target.value),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500",children:pt.map(l=>e.jsxs("option",{value:l.code,children:[l.flag," ",l.nativeName," (",l.name,")"]},l.code))}),e.jsx("p",{className:"mt-1 text-xs text-gray-500",children:"Select the language for this article content"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Publication Status *"}),e.jsxs("select",{value:N,onChange:l=>a(l.target.value),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500",children:[e.jsx("option",{value:"PUBLISHED",children:"Published - Visible to users"}),e.jsx("option",{value:"DRAFT",children:"Draft - Hidden from users"})]}),e.jsx("p",{className:"mt-1 text-xs text-gray-500",children:"Published articles appear in the public knowledge base. Drafts are only visible to admins."})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Software Version *"}),e.jsxs("select",{value:h,onChange:l=>I(l.target.value),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500",children:[e.jsx("option",{value:"v3.7",children:"v3.7"}),e.jsx("option",{value:"v4.0",children:"v4.0"})]}),e.jsx("p",{className:"mt-1 text-xs text-gray-500",children:"Select which software version this article applies to"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Tags (comma-separated)"}),e.jsx("input",{type:"text",value:_,onChange:l=>n(l.target.value),placeholder:"tag1, tag2, tag3",className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Target Vector Store"}),e.jsxs("select",{value:H,onChange:l=>X(l.target.value),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500",children:[e.jsx("option",{value:"",children:"Select vector store (optional)"}),Re.map(l=>e.jsxs("option",{value:l.id,children:[l.name," ",l.is_default?"(Default)":""]},l.id))]}),e.jsx("p",{className:"mt-1 text-xs text-gray-500",children:"Choose which OpenAI vector store should receive this article"})]})]})]})},Js={input:2.5,output:10};function Bt(f){return Math.ceil(f.length/4)}function Ga(f){const v=Bt((f==null?void 0:f.title)||""),b=Bt((f==null?void 0:f.subtitle)||""),o=Bt((f==null?void 0:f.content)||""),g=f!=null&&f.content_html?Bt(f.content_html):0,W=v+b+o+g+500,ee=Math.ceil((v+b+o+g)*1.1);return W/1e6*Js.input+ee/1e6*Js.output}const sa=({articleId:f,articleTitle:v,onClose:b,onComplete:o})=>{const[g,W]=s.useState([]),[ee,ce]=s.useState({}),[S,p]=s.useState(new Set),[G,E]=s.useState(!1),[ye,me]=s.useState({current:0,total:0}),[ae,te]=s.useState(null),[P,k]=s.useState(null),[B,be]=s.useState(.3),[se,le]=s.useState(null);s.useEffect(()=>{pe()},[f]);const pe=async()=>{try{const{data:_,error:n}=await U.from("knowledge_articles").select("id, title, subtitle, content, content_html").eq("id",f).maybeSingle();if(n)throw n;le(_);const{data:D,error:w}=await U.from("translation_settings").select("language_code, language_name, is_active, temperature").eq("is_active",!0).order("language_name");if(w)throw w;W(D||[]),D&&D.length>0&&D[0].temperature&&be(D[0].temperature);const{data:N,error:a}=await U.from("article_translations").select("language_code, translation_status, translated_at, total_tokens, estimated_cost_usd, temperature_used").eq("article_id",f);if(a)throw a;const h={};
1N==null||N.forEach(I=>{h[I.language_code]=I}),ce(h)}catch(_){console.error("Error fetching translation data:",_),te("Failed to load translation settings")}},q=_=>{const n=new Set(S);n.has(_)?n.delete(_):n.add(_),p(n)},Z=async()=>{var N;if(S.size===0){te("Please select at least one language");return}E(!0),te(null),k(null),me({current:0,total:S.size});const _=Array.from(S);let n=0,D=0,w=0;for(let a=0;a<_.length;a++){const h=_[a];me({current:a+1,total:_.length});try{const{data:{session:I}}=await U.auth.getSession();if(!I)throw new Error("Not authenticated");const H=await fetch("https://solxiyfrzksdatdzaxyy.supabase.co/functions/v1/translate-knowledge-article",{method:"POST",headers:{Authorization:`Bearer ${I.access_token}`,"Content-Type":"application/json"},body:JSON.stringify({articleId:f,targetLanguageCode:h,temperature:B})});if(H.ok){const X=await H.json();(N=X.usage)!=null&&N.estimated_cost_usd&&(w+=X.usage.estimated_cost_usd),n++}else{const X=await H.json();console.error(`Translation failed for ${h}:`,X),D++}}catch(I){console.error(`Error translating to ${h}:`,I),D++}}k({success:n,failed:D,totalCost:w}),E(!1),D===0?(await pe(),setTimeout(()=>{o()},2e3)):await pe()},V=_=>{const n=ee[_];return n?{status:n.translation_status,date:n.translated_at,tokens:n.total_tokens,cost:n.estimated_cost_usd,temperature:n.temperature_used}:null},c=se?Ga(se):0,M=c*S.size,ne=_=>{if(!_)return null;switch(_){case"completed":return e.jsx(Se,{className:"w-5 h-5 text-green-600"});case"failed":return e.jsx(De,{className:"w-5 h-5 text-red-600"});case"pending":return e.jsx(Zs,{className:"w-5 h-5 text-yellow-600"});default:return null}};return e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4",children:e.jsxs("div",{className:"bg-white dark:bg-gray-800 rounded-lg max-w-2xl w-full max-h-[90vh] overflow-y-auto",children:[e.jsxs("div",{className:"sticky top-0 bg-white dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700 px-6 py-4 flex justify-between items-center",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-xl font-bold text-gray-900",children:"Translate Article"}),e.jsx("p",{className:"text-sm text-gray-600 dark:text-gray-400 dark:text-gray-500 mt-1",children:v})]}),e.jsx("button",{onClick:b,disabled:G,className:"p-2 text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:text-gray-400 dark:text-gray-500 disabled:opacity-50",children:e.jsx(Vt,{className:"w-6 h-6"})})]}),e.jsxs("div",{className:"p-6",children:[ae&&e.jsxs("div",{className:"mb-4 p-4 bg-red-50 dark:bg-red-950/30 border border-red-200 dark:border-red-800 rounded-lg flex items-start",children:[e.jsx(De,{className:"w-5 h-5 text-red-600 dark:text-red-400 mr-2 mt-0.5 flex-shrink-0"}),e.jsx("p",{className:"text-sm text-red-800",children:ae})]}),P&&e.jsxs("div",{className:"mb-4 p-4 bg-blue-50 dark:bg-blue-900/30 border border-blue-200 dark:border-blue-800 rounded-lg",children:[e.jsx("h3",{className:"font-semibold text-blue-900 mb-2",children:"Translation Complete"}),e.jsxs("p",{className:"text-sm text-blue-800",children:["Successfully translated: ",P.success," language",P.success!==1?"s":""]}),P.totalCost>0&&e.jsxs("p",{className:"text-sm text-blue-800 dark:text-blue-200 mt-1",children:["Total cost: $",P.totalCost<.01?P.totalCost.toFixed(6):P.totalCost.toFixed(4)]}),P.failed>0&&e.jsxs("p",{className:"text-sm text-red-800 dark:text-red-200 mt-1",children:["Failed: ",P.failed," language",P.failed!==1?"s":""]})]}),G?e.jsxs("div",{className:"py-8 text-center",children:[e.jsx("div",{className:"animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600 mx-auto mb-4"}),e.jsx("p",{className:"text-lg font-medium text-gray-900 dark:text-gray-100 mb-2",children:"Translating..."}),e.jsxs("p",{className:"text-sm text-gray-600",children:["Processing ",ye.current," of ",ye.total," languages"]}),e.jsx("div",{className:"mt-4 max-w-xs mx-auto",children:e.jsx("div",{className:"w-full bg-gray-200 dark:bg-gray-700 rounded-full h-2",children:e.jsx("div",{className:"bg-blue-600 h-2 rounded-full transition-all",style:{width:`${ye.current/ye.total*100}%`}})})})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"mb-6",children:[e.jsx("h3",{className:"text-sm font-semibold text-gray-900 dark:text-gray-100 mb-3",children:"Select Languages to Translate"}),e.jsx("p",{className:"text-sm text-gray-600 dark:text-gray-400 dark:text-gray-500 mb-4",children:"Choose one or more languages. Existing translations will be updated."}),e.jsxs("div",{className:"bg-gray-50 dark:bg-gray-900 border border-gray-200 dark:border-gray-700 rounded-lg p-4 mb-4",children:[e.jsxs("label",{className:"block text-sm font-semibold text-gray-900 dark:text-gray-100 mb-2",children:["Temperature: ",B.toFixed(1)]}
1),e.jsx("input",{type:"range",min:"0",max:"2",step:"0.1",value:B,onChange:_=>be(parseFloat(_.target.value)),className:"w-full h-2 bg-gray-200 dark:bg-gray-700 rounded-lg appearance-none cursor-pointer"}),e.jsxs("div",{className:"flex justify-between text-xs text-gray-600 dark:text-gray-400 dark:text-gray-500 mt-2",children:[e.jsx("span",{children:"0.0 (More precise)"}),e.jsx("span",{children:"1.0 (Balanced)"}),e.jsx("span",{children:"2.0 (More creative)"})]}),e.jsx("p",{className:"text-xs text-gray-600 dark:text-gray-400 dark:text-gray-500 mt-2",children:"Lower values produce more deterministic, consistent translations ideal for technical content. Higher values allow more creativity and variation."})]}),S.size>0&&se&&e.jsxs("div",{className:"bg-blue-50 dark:bg-blue-900/30 border border-blue-200 dark:border-blue-800 rounded-lg p-4 mb-4",children:[e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("span",{className:"text-sm font-medium text-blue-900",children:"Estimated cost per language:"}),e.jsxs("span",{className:"text-sm font-bold text-blue-900",children:["$",c<.01?c.toFixed(6):c.toFixed(4)]})]}),e.jsxs("div",{className:"flex justify-between items-center mt-2 pt-2 border-t border-blue-200",children:[e.jsxs("span",{className:"text-sm font-semibold text-blue-900",children:["Total estimated cost (",S.size," language",S.size!==1?"s":"","):"]}),e.jsxs("span",{className:"text-sm font-bold text-blue-900",children:["$",M<.01?M.toFixed(6):M.toFixed(4)]})]}),e.jsx("p",{className:"text-xs text-blue-700 mt-2",children:"Actual costs may vary slightly. Cost is based on GPT-4o pricing."})]})]}),e.jsx("div",{className:"space-y-2 max-h-96 overflow-y-auto",children:g.map(_=>{var w;const n=V(_.language_code),D=S.has(_.language_code);return e.jsxs("label",{className:`
2                        flex items-center justify-between p-4 border-2 rounded-lg cursor-pointer
3                        transition-all hover:bg-gray-50
4                        ${D?"border-blue-500 bg-blue-50":"border-gray-200"}
5                      `,children:[e.jsxs("div",{className:"flex items-center flex-1",children:[e.jsx("input",{type:"checkbox",checked:D,onChange:()=>q(_.language_code),className:"mr-3 rounded border-gray-300 dark:border-gray-600 text-blue-600 dark:text-blue-400 focus:ring-blue-500"}),e.jsx("span",{className:"text-2xl mr-3",children:ct(_.language_code)}),e.jsxs("div",{className:"flex-1",children:[e.jsx("div",{className:"font-medium text-gray-900",children:_.language_name}),e.jsx("div",{className:"text-xs text-gray-500 dark:text-gray-400 dark:text-gray-500 uppercase",children:_.language_code})]})]}),n&&e.jsxs("div",{className:"flex items-center ml-4",children:[ne(n.status),e.jsxs("div",{className:"ml-2",children:[e.jsx("div",{className:"text-sm text-gray-600 dark:text-gray-400 dark:text-gray-500 capitalize",children:n.status}),n.cost&&n.cost>0&&e.jsxs("div",{className:"text-xs text-gray-500",children:["$",n.cost<.01?n.cost.toFixed(6):n.cost.toFixed(4)," • ",(w=n.tokens)==null?void 0:w.toLocaleString()," tokens"]})]})]})]},_.language_code)})}),g.length===0&&e.jsxs("div",{className:"text-center py-8",children:[e.jsx(lt,{className:"w-16 h-16 text-gray-300 mx-auto mb-4"}),e.jsx("p",{className:"text-gray-600",children:"No languages configured"}),e.jsx("p",{className:"text-sm text-gray-500 dark:text-gray-400 dark:text-gray-500 mt-2",children:"Add languages in Knowledge Base Settings to enable translations"})]})]})]}),!G&&!P&&g.length>0&&e.jsxs("div",{className:"sticky bottom-0 bg-gray-50 dark:bg-gray-900 px-6 py-4 border-t border-gray-200 dark:border-gray-700 flex justify-end space-x-3",children:[e.jsx("button",{onClick:b,className:"px-4 py-2 text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50",children:"Cancel"}),e.jsxs("button",{onClick:Z,disabled:S.size===0,className:"px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed flex items-center",children:[e.jsx(lt,{className:"w-5 h-5 mr-2"}),"Translate (",Array.from(S).length,")"]})]}),P&&e.jsx("div",{className:"sticky bottom-0 bg-gray-50 dark:bg-gray-900 px-6 py-4 border-t border-gray-200 dark:border-gray-700 flex justify-end",children:e.jsx("button",{onClick:b,className:"px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700",children:"Close"})})]})})},Ka=({isOpen:f,onClose:v,onSuccess:b,article:o,categories:g,initialMode:W="preview"})=>{var L,ge,Y,re;const[ee,ce]=s.useState([]),[S,p]=s.useState(o.language||"en"),[G,E]=s.useState({}),[ye,me]=s.useState({}),[ae,te]=s.useState({}),[P,k]=s.useState(!0),[B,be]=s.useState(!1),[se,le]=s.useState(!1),[pe,q]=s.useState(null),[Z,V]=s.useState(!1),[c,M]=s.useState("preview"),[ne,_]=s.useState(!0),[n,D]=s.useState(!1),[w,N]=s.useState(o.module_id||""),[a,h]=s.useState([]),[I,H]=s.useState("PUBLISHED"),[X,Re]=s.useState(o.version||"v3.7"),[Ne,He]=s.useState(!1),_e=s.useRef(o.language||"en");s.useEffect(()=>{f&&(Oe(),N(o.module_id||""),H(o.status||"PUBLISHED"),Re(o.version||"v3.7"))}
5,[f,o.module_id,o.status,o.version]);const Oe=async()=>{try{const{data:r,error:z}=await U.from("knowledge_modules").select("id, name, slug").eq("is_active",!0).order("order_index",{ascending:!0});if(z)throw z;h(r||[])}catch(r){console.error("Failed to fetch modules:",r)}},je=r=>{N(r),Fe({categoryId:"",subcategoryId:""})};s.useEffect(()=>{if(f){k(!0);const r=o.language||"en";_e.current=r;const z=Gs(o.content,o.content_html),R={title:o.title,subtitle:o.subtitle||"",htmlContent:z,tags:(o.tags||[]).join(", "),categoryId:o.category_id||"",subcategoryId:o.subcategory_id||"",articleId:o.id,hasUnsavedChanges:!1},A={title:o.title,subtitle:o.subtitle||"",htmlContent:z,tags:(o.tags||[]).join(", "),categoryId:o.category_id||"",subcategoryId:o.subcategory_id||""},$={},ie={},Q={};pt.forEach(xe=>{xe.code===r?($[xe.code]=R,ie[xe.code]=A,Q[xe.code]={exists:!0,isLoaded:!0,status:"PUBLISHED"}):($[xe.code]={title:"",subtitle:"",htmlContent:"",tags:"",categoryId:o.category_id||"",subcategoryId:o.subcategory_id||"",hasUnsavedChanges:!1},ie[xe.code]={title:"",subtitle:"",htmlContent:"",tags:"",categoryId:o.category_id||"",subcategoryId:o.subcategory_id||""},Q[xe.code]={exists:!1,isLoaded:!1})}),E($),me(ie),te(Q),p(r),q(null),V(!1),M(W),Pe(Q,r),setTimeout(()=>{k(!1)},100)}},[f,o,W]);const Pe=async(r,z)=>{le(!0);try{const{data:R,error:A}=await U.rpc("get_article_translations",{p_article_id:o.id});if(A)throw A;ce(R||[]);const $=r||ae,ie=z||o.language||"en",Q={...$};(R||[]).forEach(xe=>{xe.language!==ie&&(Q[xe.language]={exists:!0,isLoaded:!1,status:xe.status})}),te(Q)}catch(R){console.error("Failed to load translations:",R)}finally{le(!1)}},qe=async(r,z)=>{const R=ae[r];if(!(R!=null&&R.isLoaded))try{const{data:A,error:$}=await U.from("knowledge_articles").select("*").eq("id",z).single();if($)throw $;const ie=Gs(A.content,A.content_html),Q={title:A.title,subtitle:A.subtitle||"",htmlContent:ie,tags:(A.tags||[]).join(", "),categoryId:A.category_id||"",subcategoryId:A.subcategory_id||"",articleId:A.id,hasUnsavedChanges:!1},xe={title:A.title,subtitle:A.subtitle||"",htmlContent:ie,tags:(A.tags||[]).join(", "),categoryId:A.category_id||"",subcategoryId:A.subcategory_id||""};E(Ie=>({...Ie,[r]:Q})),me(Ie=>({...Ie,[r]:xe})),te(Ie=>({...Ie,[r]:{...Ie[r],isLoaded:!0}}))}catch(A){console.error("Failed to load language data:",A),q(`Failed to load ${bt(r)} version`)}},Je=async r=>{if(r===S||ve().hasUnsavedChanges&&!window.confirm("You have unsaved changes. Do you want to switch anyway?"))return;He(!0),_e.current=r;const R=ae[r];if(R!=null&&R.exists&&!R.isLoaded){const A=ee.find($=>$.language===r);A&&await qe(r,A.article_id)}p(r),q(null),V(!1),setTimeout(()=>{He(!1)},50)};s.useEffect(()=>{const r=z=>{Object.values(G).some(A=>A.hasUnsavedChanges)&&f&&(z.preventDefault(),z.returnValue="")};return window.addEventListener("beforeunload",r),()=>window.removeEventListener("beforeunload",r)},[G,f]);const ve=()=>G[S]||{title:"",subtitle:"",htmlContent:"",tags:"",categoryId:"",subcategoryId:"",hasUnsavedChanges:!1},Te=r=>r.replace(/\s+/g," ").replace(/\s*<\s*/g,"<").replace(/\s*>\s*/g,">").trim(),Fe=r=>{if(Ne)return;const z=_e.current;if(P){E(R=>({...R,[z]:{...R[z]||ve(),...r,hasUnsavedChanges:!1}}));return}E(R=>{const $={...R[z]||{title:"",subtitle:"",htmlContent:"",tags:"",categoryId:"",subcategoryId:"",hasUnsavedChanges:!1},...r},ie=ye[z],Q=ie?$.title.trim()!==ie.title.trim()||$.subtitle.trim()!==ie.subtitle.trim()||Te($.htmlContent)!==Te(ie.htmlContent)||$.tags.trim()!==ie.tags.trim()||$.categoryId!==ie.categoryId||$.subcategoryId!==ie.subcategoryId:!0;return{...R,[z]:{...$,hasUnsavedChanges:Q}}})};if(!f)return null;const i=w?g.filter(r=>r.module_id===w):g,u=i.filter(r=>!r.parent_id),O=r=>i.filter(z=>z.parent_id===r),C=async()=>{const r=ve();if(!r.title.trim()){q("Title is required");return}if(!r.htmlContent.trim()){q("Content is required");return}be(!0),q(null),V(!1);try{const{data:{user:z},error:R}=await U.auth.getUser();if(R||!z)throw new Error("Unable to authenticate user. Please log in again.");const A=r.tags.split(",").map(Le=>Le.trim()).filter(Le=>Le.length>0),$=document.createElement("div");$.innerHTML=r.htmlContent;const ie=$.textContent||$.innerText||"",Q=Ba(r.htmlContent),xe=ae[S];if(!(xe!=null&&xe.exists)){const Le=o.translation_group_id||o.id,fe={title:r.title.trim(),content:ie,content_html:Q,subtitle:r.subtitle.trim()||null,tags:A,language:S,version:X,translation_group_id:Le,is_original:!1,source_type:o.source_type,module_id:w,category_id:r.categoryId||null,subcategory_id:r.subcategoryId||null,status:"DRAFT",user_id:z.id,created_by_email:z.email||null},{data:We,error:Ye}=await U.from("knowledge_articles").insert(fe).select().single();if(Ye)throw Ye;o.translation_group_id||await U.from("knowledge_articles").update({translation_group_id:Le}
5).eq("id",o.id),await Pe(),E(Me=>({...Me,[S]:{...r,articleId:We.id,htmlContent:Q,hasUnsavedChanges:!1}})),te(Me=>({...Me,[S]:{exists:!0,isLoaded:!0,status:"DRAFT"}})),V(!0),setTimeout(()=>{V(!1),b()},1500)}else{const Le={title:r.title.trim(),content:ie,content_html:Q,subtitle:r.subtitle.trim()||null,tags:A,language:S,version:X,module_id:w,category_id:r.categoryId||null,subcategory_id:r.subcategoryId||null,status:I,updated_at:new Date().toISOString()},fe=r.articleId||o.id,{error:We}=await U.from("knowledge_articles").update(Le).eq("id",fe);if(We)throw We;const Ye={title:r.title.trim(),subtitle:r.subtitle.trim(),htmlContent:Q,tags:A.join(", "),categoryId:r.categoryId,subcategoryId:r.subcategoryId};E(Me=>({...Me,[S]:{...r,htmlContent:Q,hasUnsavedChanges:!1}})),me(Me=>({...Me,[S]:Ye})),V(!0),setTimeout(()=>{V(!1),b()},1500)}}catch(z){console.error("Save error:",z),q(z.message||"Failed to save article")}finally{be(!1)}},T=()=>{if(!B){if(Object.values(G).some(z=>z.hasUnsavedChanges)&&!window.confirm("You have unsaved changes. Are you sure you want to close?"))return;v()}},K=r=>Ea.sanitize(r,{ALLOWED_TAGS:["h1","h2","h3","h4","h5","h6","p","br","strong","em","u","s","del","ins","blockquote","code","pre","ul","ol","li","a","img","table","thead","tbody","tr","th","td","div","span"],ALLOWED_ATTR:["href","src","alt","title","class","id","style","target","rel","width","height"],ALLOWED_URI_REGEXP:/^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i}),l=r=>{const z=document.createElement("div");return z.innerHTML=r,(z.textContent||z.innerText||"").trim().split(/\s+/).filter(A=>A.length>0).length},d=ve(),J=[pt.find(r=>r.code==="en"),...pt.filter(r=>r.code!=="en")];return e.jsxs("div",{className:"fixed inset-0 z-50 overflow-y-auto",children:[e.jsxs("div",{className:"flex items-center justify-center min-h-screen px-4 pt-4 pb-20 text-center sm:block sm:p-0",children:[e.jsx("div",{className:"fixed inset-0 transition-opacity bg-gray-500 bg-opacity-75",onClick:T}),e.jsxs("div",{className:"inline-block w-full max-w-6xl my-8 overflow-hidden text-left align-middle transition-all transform bg-white dark:bg-gray-800 rounded-lg shadow-xl",children:[e.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-gray-200 dark:border-gray-700 bg-gradient-to-r from-blue-50 to-white",children:[e.jsxs("div",{children:[e.jsxs("h3",{className:"text-lg font-semibold text-gray-900 dark:text-gray-100 flex items-center",children:[e.jsx(dt,{className:"w-5 h-5 mr-2 text-blue-600"}),c==="edit"?"Edit Article":"Article Preview"]}),d.hasUnsavedChanges&&e.jsxs("p",{className:"text-xs text-orange-600 mt-1",children:["Unsaved changes in ",bt(S)]})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx("button",{onClick:()=>M(c==="edit"?"preview":"edit"),className:`px-4 py-2 rounded-lg text-sm font-medium transition-all duration-200 flex items-center ${c==="edit"?"bg-blue-600 text-white hover:bg-blue-700 shadow-md":"bg-white text-gray-700 dark:text-gray-300 hover:bg-gray-50 border border-gray-300"}`,disabled:B,children:c==="edit"?e.jsxs(e.Fragment,{children:[e.jsx(is,{className:"w-4 h-4 mr-2"}),"Preview"]}):e.jsxs(e.Fragment,{children:[e.jsx(os,{className:"w-4 h-4 mr-2"}),"Edit"]})}),e.jsx("button",{onClick:T,disabled:B,className:"text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:text-gray-400 dark:text-gray-500 transition-colors disabled:opacity-50 p-1 rounded-lg hover:bg-gray-100",title:"Close (unsaved changes will be lost)",children:e.jsx(Vt,{className:"w-6 h-6"})})]})]}),e.jsx("div",{className:"border-b border-gray-200 dark:border-gray-700 bg-white",children:e.jsxs("div",{className:"px-6",children:[e.jsx("div",{className:"flex items-center space-x-1 overflow-x-auto py-2",children:J.map(r=>{const z=r.code===S,R=G[r.code],A=R==null?void 0:R.hasUnsavedChanges,$=ae[r.code],ie=($==null?void 0:$.status)==="DRAFT",Q=!($!=null&&$.exists);return e.jsxs("button",{onClick:()=>Je(r.code),disabled:B||se,className:`
6                        relative px-3 py-2 text-sm font-medium border-b-2 transition-all duration-200
7                        whitespace-nowrap flex items-center space-x-1.5 min-w-fit
8                        ${z?"border-blue-500 text-blue-600 dark:text-blue-400 bg-blue-50":Q?"border-transparent text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:text-gray-400 dark:text-gray-500 hover:border-gray-300":"border-transparent text-gray-600 dark:text-gray-400 dark:text-gray-500 hover:text-gray-900 dark:text-gray-100 hover:border-gray-300"}
9                        ${B||se?"opacity-50 cursor-not-allowed":"cursor-pointer"}
10                      `,children:[e.jsx("span",{className:"text-base",style:{opacity:Q?.5:1},children:ct(r.code)}),e.jsx("span",{className:"uppercase font-semibold",children:r.code}),A&&e.jsx("span",{className:"w-1.5 h-1.5 rounded-full bg-orange-500",title:"Unsaved changes"}),Q&&S==="en"&&e.jsx("span",{className:"text-xs bg-gray-100 dark:bg-gray-800 text-gray-500 dark:text-gray-400 dark:text-gray-500 px-1.5 py-0.5 rounded",children:"Empty"}),!Q&&ie&&e.jsx("span",{className:"text-xs bg-yellow-100 text-yellow-700 px-1.5 py-0.5 rounded",children:"Draft"})]},r.code)})}),S==="en"&&((L=ae.en)==null?void 0:L.exists)&&e.jsx("div",{className:"px-6 py-2 bg-blue-50 dark:bg-blue-900/30 border-t border-blue-100",children:e.jsxs("button",{onClick:()=>D(!0),className:"text-sm text-blue-600 dark:text-blue-400 hover:text-blue-700 font-medium flex items-center",children:[e.jsx(Vs,{className:"w-4 h-4 mr-1.5"}),"Translate this article to other languages with AI"]})})]})}),e.jsxs("div",{className:"px-6 py-4 max-h-[calc(100vh-280px)] overflow-y-auto",children:[pe&&e.jsxs("div",{className:"mb-4 p-3 bg-red-50 dark:bg-red-950/30 border border-red-200 dark:border-red-800 rounded-lg flex items-start animate-shake",children:[e.jsx(Ke,{className:"w-5 h-5 text-red-600 dark:text-red-400 mr-2 flex-shrink-0 mt-0.5"}),e.jsx("span",{className:"text-sm text-red-600",children:pe})]}),Z&&e.jsxs("div",{className:"mb-4 p-3 bg-green-50 dark:bg-green-900/30 border border-green-200 rounded-lg flex items-start",children:[e.jsx(Se,{className:"w-5 h-5 text-green-600 mr-2 flex-shrink-0 mt-0.5"}),e.jsx("span",{className:"text-sm text-green-600",children:"Article saved successfully!"})]}),c==="preview"?e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-xl shadow-sm overflow-hidden",children:[e.jsxs("div",{className:"bg-gradient-to-r from-gray-50 to-white px-6 py-4 border-b border-gray-200",children:[e.jsx("h1",{className:"text-3xl font-bold text-gray-900",children:d.title||"Untitled Article"}),d.subtitle&&e.jsx("p",{className:"text-lg text-gray-600 dark:text-gray-400 dark:text-gray-500 mt-2",children:d.subtitle})]}),e.jsx("div",{className:"px-6 py-8 prose prose-lg max-w-none prose-headings:font-bold prose-headings:text-gray-900 dark:text-gray-100 prose-h1:text-3xl prose-h1:mt-8 prose-h1:mb-4 prose-h2:text-2xl prose-h2:mt-8 prose-h2:mb-4 prose-h3:text-xl prose-h3:mt-6 prose-h3:mb-3 prose-p:text-gray-700 dark:text-gray-300 prose-p:leading-relaxed prose-p:my-4 prose-a:text-blue-600 dark:text-blue-400 prose-a:no-underline hover:prose-a:underline prose-strong:text-gray-900 dark:text-gray-100 prose-strong:font-semibold prose-ul:my-4 prose-ul:list-disc prose-ul:pl-6 prose-ol:my-4 prose-ol:list-decimal prose-ol:pl-6 prose-li:my-2 prose-li:text-gray-700 dark:text-gray-300 prose-blockquote:border-l-4 prose-blockquote:border-blue-500 prose-blockquote:pl-4 prose-blockquote:italic prose-blockquote:text-gray-600 dark:text-gray-400 dark:text-gray-500 prose-code:bg-gray-100 dark:bg-gray-800 prose-code:px-1.5 prose-code:py-0.5 prose-code:rounded prose-code:text-sm prose-code:text-gray-800 dark:text-gray-200 prose-pre:bg-gray-900 prose-pre:text-gray-100 prose-pre:rounded-lg prose-pre:p-4 prose-img:rounded-xl prose-img:shadow-md prose-img:my-6",dangerouslySetInnerHTML:{__html:K(d.htmlContent)}})]}),e.jsxs("div",{className:"border border-gray-200 dark:border-gray-700 rounded-xl overflow-hidden bg-white dark:bg-gray-800 shadow-sm",children:[e.jsxs("button",{onClick:()=>_(!ne),className:"w-full flex items-center justify-between px-6 py-4 bg-gray-50 dark:bg-gray-900 hover:bg-gray-100 dark:bg-gray-800 transition-colors",children:[e.jsx("h3",{className:"text-sm font-semibold text-gray-900 dark:text-gray-100 uppercase tracking-wide",children:"Article Information"}),ne?e.jsx(Ta,{className:"w-5 h-5 text-gray-600"}):e.jsx(La,{className:"w-5 h-5 text-gray-600"})]}),ne&&e.jsxs("div",{className:"px-6 py-4 space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4 text-sm",children:[e.jsxs("div",{children:[e.jsx("span",{className:"font-medium text-gray-700",children:"Subtitle:"}),e.jsx("p",{className:"text-gray-600 dark:text-gray-400 dark:text-gray-500 mt-1",children:d.subtitle||"(No subt
10itle)"})]}),e.jsxs("div",{children:[e.jsx("span",{className:"font-medium text-gray-700",children:"Word Count:"}),e.jsxs("p",{className:"text-gray-600 dark:text-gray-400 dark:text-gray-500 mt-1",children:[l(d.htmlContent)," words"]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4 text-sm",children:[e.jsxs("div",{children:[e.jsx("span",{className:"font-medium text-gray-700",children:"Category:"}),e.jsx("p",{className:"text-gray-600 dark:text-gray-400 dark:text-gray-500 mt-1",children:d.categoryId?((ge=u.find(r=>r.id===d.categoryId))==null?void 0:ge.name)||"Unknown":"(None selected)"})]}),e.jsxs("div",{children:[e.jsx("span",{className:"font-medium text-gray-700",children:"Subcategory:"}),e.jsx("p",{className:"text-gray-600 dark:text-gray-400 dark:text-gray-500 mt-1",children:d.subcategoryId?((Y=O(d.categoryId).find(r=>r.id===d.subcategoryId))==null?void 0:Y.name)||"Unknown":"(None selected)"})]})]}),e.jsx("div",{className:"grid grid-cols-2 gap-4 text-sm",children:e.jsxs("div",{children:[e.jsxs("span",{className:"font-medium text-gray-700",children:[e.jsx(lt,{className:"w-4 h-4 inline mr-1"}),"Language:"]}),e.jsxs("p",{className:"text-gray-600 dark:text-gray-400 dark:text-gray-500 mt-1 flex items-center",children:[e.jsx("span",{className:"text-lg mr-2",children:ct(S)}),bt(S)]})]})}),d.tags&&e.jsxs("div",{className:"text-sm",children:[e.jsx("span",{className:"font-medium text-gray-700",children:"Tags:"}),e.jsx("div",{className:"flex flex-wrap gap-2 mt-2",children:d.tags.split(",").map((r,z)=>{const R=r.trim();return R?e.jsx("span",{className:"px-3 py-1 bg-blue-100 text-blue-700 rounded-full text-xs font-medium",children:R},z):null})})]})]})]})]}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Title *"}),e.jsx("input",{type:"text",value:d.title,onChange:r=>Fe({title:r.target.value}),placeholder:"Article title",className:"w-full px-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-shadow",disabled:B})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Subtitle"}),e.jsx("input",{type:"text",value:d.subtitle,onChange:r=>Fe({subtitle:r.target.value}),placeholder:"Optional subtitle",className:"w-full px-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-shadow",disabled:B})]}),e.jsxs("div",{children:[e.jsxs("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:["Content * ",e.jsxs("span",{className:"text-xs text-gray-500",children:["(",l(d.htmlContent)," words)"]})]}),e.jsx(Ua,{value:d.htmlContent,onChange:r=>Fe({htmlContent:r}),articleId:d.articleId||o.id,placeholder:"Start writing your article content...",readOnly:B},S)]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Module *"}),e.jsxs("select",{value:w,onChange:r=>je(r.target.value),className:"w-full px-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-shadow",disabled:B,required:!0,children:[e.jsx("option",{value:"",children:"Select module"}),a.map(r=>e.jsx("option",{value:r.id,children:r.name},r.id))]}),e.jsx("p",{className:"mt-1 text-xs text-gray-500",children:"Select the product module for this article"}),w!==o.module_id&&e.jsx("div",{className:"mt-2 p-2 bg-yellow-50 border border-yellow-200 rounded text-xs text-yellow-800",children:"Changing the module will reset category and subcategory selections."})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Category"}),e.jsxs("select",{value:d.categoryId,onChange:r=>{Fe({categoryId:r.target.value,subcategoryId:""})},className:"w-full px-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-shadow",disabled:B,children:[e.jsx("option",{value:"",children:"Select category"}),u.map(r=>
10e.jsx("option",{value:r.id,children:r.name},r.id))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Subcategory"}),e.jsxs("select",{value:d.subcategoryId,onChange:r=>Fe({subcategoryId:r.target.value}),className:"w-full px-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-shadow",disabled:!d.categoryId||B,children:[e.jsx("option",{value:"",children:"Select subcategory"}),d.categoryId&&O(d.categoryId).map(r=>e.jsx("option",{value:r.id,children:r.name},r.id))]})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Tags (comma-separated)"}),e.jsx("input",{type:"text",value:d.tags,onChange:r=>Fe({tags:r.target.value}),placeholder:"tag1, tag2, tag3",className:"w-full px-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-shadow",disabled:B})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Publication Status *"}),e.jsxs("select",{value:I,onChange:r=>H(r.target.value),className:"w-full px-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-shadow",disabled:B,children:[e.jsx("option",{value:"PUBLISHED",children:"Published - Visible to users"}),e.jsx("option",{value:"DRAFT",children:"Draft - Hidden from users"})]}),e.jsx("p",{className:"mt-1 text-xs text-gray-500",children:"Published articles appear in the public knowledge base. Drafts are only visible to admins."})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"Software Version *"}),e.jsxs("select",{value:X,onChange:r=>Re(r.target.value),className:"w-full px-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-shadow",disabled:B,children:[e.jsx("option",{value:"v3.7",children:"v3.7"}),e.jsx("option",{value:"v4.0",children:"v4.0"})]}),e.jsx("p",{className:"mt-1 text-xs text-gray-500",children:"Select which software version this article applies to"})]})]})]}),e.jsxs("div",{className:"flex items-center justify-between px-6 py-4 bg-gray-50 dark:bg-gray-900 border-t border-gray-200",children:[e.jsxs("div",{className:"flex items-center space-x-4",children:[e.jsx("div",{className:"text-xs text-gray-500",children:c==="edit"?e.jsx("span",{children:"Editing mode - Click Preview to see how your article will look"}):e.jsx("span",{children:"Preview mode - Click Edit to make changes"})}),S==="en"&&((re=ae.en)==null?void 0:re.exists)&&e.jsxs("button",{onClick:()=>D(!0),className:"px-3 py-1.5 text-xs bg-gradient-to-r from-purple-500 to-blue-500 text-white rounded-lg hover:from-purple-600 hover:to-blue-600 font-medium transition-all flex items-center shadow-sm",disabled:B,children:[e.jsx(Vs,{className:"w-3.5 h-3.5 mr-1.5"}),"AI Translate"]})]}),e.jsxs("div",{className:"flex space-x-3",children:[e.jsx("button",{onClick:T,disabled:B,className:"px-4 py-2 text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:bg-gray-900 font-medium transition-colors disabled:opacity-50 disabled:cursor-not-allowed",children:"Cancel"}),e.jsx("button",{onClick:C,disabled:B||!d.title.trim()||!d.htmlContent.trim(),className:"px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 font-medium transition-colors disabled:opacity-50 disabled:cursor-not-allowed flex items-center shadow-md hover:shadow-lg",children:B?e.jsxs(e.Fragment,{children:[e.jsxs("svg",{className:"animate-spin -ml-1 mr-2 h-4 w-4 text-white",xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",children:[e.jsx("circle",{className:"opacity-25",cx:"12",cy:"12",r:"10",stroke:"currentColor",strokeWidth:"4"}),e.jsx("path",{className:"opacity-75",fill:"currentColor",d:"M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"})]}),"Saving..."]}):e.jsxs(e.Fragment,{children:[e.jsx(Se,{className:"w-5 h-5 mr-2"}),"Save Changes"]})})]})]})]})]}),n&&e.jsx(sa,{articleId:o.id,articleTitle:o.title,onClose:()=>D(!1),onComplete:()=>{D(!1),Pe()}})]})},Ja=({csvHeaders:f,csvData:v,onMappingComplete:b,onBack:o})=>{var ne,_;const[g,W]=s.useState([]),[ee,ce]=s.useState([]),[S,p]=s.useState([]),[G,E]=s.useState(null),[ye,me]=s.useState(!0),[ae,te]=s.useState(!1),[P,k]=s.useState(""),[B,be]=s.useState("");s.useEffect(()=>{se(),le()},[]),s.useEffect(()=>{g.length>0&&f.length>0&&pe()},[g,f]);const se=async()=>{const{data:n,error:D}=await U.from("csv_import_field_definitions").select("*").order("sort_order");!D&&n&&W(n)},le=async()=>{const{data:n,error:D}=await U.from("csv_column_mapping_templates").select("*").order("is_default",{ascending:!1}).order("last_used_at",{ascending:!1});if(!D&&n){ce(n);const w=n.find(N=>N.is_default);w&&E(w.id)}me(!1)}
10,pe=()=>{const n=f.map(D=>{const w=D.toLowerCase().trim();let N=null;const a=g.find(h=>h.db_field_name.toLowerCase()===w||h.display_label.toLowerCase()===w);return a?N=a:N=g.find(I=>{const H=I.db_field_name.toLowerCase(),X=I.display_label.toLowerCase();return w.includes(H)||H.includes(w)||w.includes(X)||X.includes(w)})||null,{csvColumn:D,dbField:(N==null?void 0:N.db_field_name)||null,fieldDef:N}});p(n)},q=n=>{const D=ee.find(N=>N.id===n);if(!D)return;const w=f.map(N=>{const a=D.mapping_config[N],h=a&&g.find(I=>I.db_field_name===a)||null;return{csvColumn:N,dbField:a||null,fieldDef:h}});p(w),E(n)},Z=(n,D)=>{p(w=>w.map(N=>{if(N.csvColumn===n){const a=D&&g.find(h=>h.db_field_name===D)||null;return{...N,dbField:D,fieldDef:a}}return N}))},V=async()=>{if(!P.trim())return;const n={};S.forEach(N=>{N.dbField&&(n[N.csvColumn]=N.dbField)});const{data:{user:D}}=await U.auth.getUser();if(!D)return;const{error:w}=await U.from("csv_column_mapping_templates").insert({name:P.trim(),description:B.trim()||null,is_default:!1,mapping_config:n,created_by:D.id});w||(te(!1),k(""),be(""),le())},c=async()=>{const n={};S.forEach(D=>{D.dbField&&(n[D.csvColumn]=D.dbField)}),G&&await U.from("csv_column_mapping_templates").update({last_used_at:new Date().toISOString(),use_count:U.rpc("increment",{row_id:G})}).eq("id",G),b(n)},M=s.useMemo(()=>{const n=g.filter(h=>h.is_required),D=new Set(S.filter(h=>h.dbField).map(h=>h.dbField)),w=n.filter(h=>!D.has(h.db_field_name)),N=S.filter(h=>h.dbField).length,a=S.length-N;return{isValid:w.length===0,missingRequired:w,mappedCount:N,unmappedCount:a,totalColumns:S.length}},[S,g]);return ye?e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(ke,{className:"w-8 h-8 text-blue-600 dark:text-blue-300 animate-spin"})}):e.jsxs("div",{className:"space-y-6",children:[e.jsx("div",{className:"bg-blue-50 border border-blue-200 rounded-lg p-4",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx(Hs,{className:"w-5 h-5 text-blue-600 dark:text-blue-300 mr-2 flex-shrink-0 mt-0.5"}),e.jsxs("div",{className:"flex-1",children:[e.jsx("h4",{className:"text-sm font-semibold text-gray-900 dark:text-gray-50 mb-1",children:"Map Your CSV Columns"}),e.jsx("p",{className:"text-sm text-gray-700 dark:text-gray-200",children:"Match each column from your CSV file to the corresponding database field. Required fields are marked with an asterisk (*)."})]})]})}),ee.length>0&&e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-200 mb-2",children:"Quick Apply Template"}),e.jsxs("select",{value:G||"",onChange:n=>n.target.value&&q(n.target.value),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500",children:[e.jsx("option",{value:"",children:"-- Select a template --"}),ee.map(n=>e.jsxs("option",{value:n.id,children:[n.name," ",n.is_default?"(Default)":""]},n.id))]}),G&&((ne=ee.find(n=>n.id===G))==null?void 0:ne.description)&&e.jsx("p",{className:"mt-1 text-xs text-gray-600 dark:text-gray-300",children:(_=ee.find(n=>n.id===G))==null?void 0:_.description})]}),e.jsxs("div",{className:"bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("h4",{className:"text-sm font-semibold text-gray-900 dark:text-gray-50",children:["Column Mappings (",M.mappedCount,"/",M.totalColumns," mapped)"]}),e.jsxs("button",{onClick:()=>te(!ae),className:"text-sm text-blue-600 dark:text-blue-300 hover:text-blue-700 flex items-center",children:[e.jsx(Va,{className:"w-4 h-4 mr-1"}),"Save as Template"]})]}),ae&&e.jsx("div",{className:"mb-4 p-3 bg-gray-50 dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-700",children:e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-gray-700 dark:text-gray-200 mb-1",children:"Template Name"}),e.jsx("input",{type:"text",value:P,onChange:n=>k(n.target.value),placeholder:"My Custom Mapping",className:"w-full px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-gray-700 dark:text-gray-200 mb-1",children:"Description (Optional)"}),e.jsx("input",{type:"text",value:B,onChange:n=>be(n.target.value),placeholder:"Description of this mapping configuration",className:"w-full px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"})]}),e.jsxs("div",{className:"flex space-x-2",children:[e.jsx("button",{onClick:V,disabled:!P.trim(),className:"btn-primary text-xs disabled:opacity-50 disabled:cursor-not-allowed",children:"Save Template"}),e.jsx("button",{onClick:()=>{te(!1),k(""),be("")},className:"btn-secondary text-xs",children:"Cancel"})]})]})}),e.jsx("div",{className:"space-y-3 max-h-96 overflow-y-auto",children:S.map((n,D)=>{var w;return e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-start space-y-2 sm:space-y-0 sm:space-x-4 p-3 border border-gray-200 dark:border-gray-700 rounded-lg hover:border-blue-300 transition-colors",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center justify-between mb-2",children:[e.jsx("label",{className:"block text-xs font-medium text-gray-700 dark:text-gray-200",children:"CSV Column"}),n.dbField&&e.jsx(Se,{className:"w-4 h-4 text-green-600"})]}),e.jsx("div",{className:"px-3 py-2 bg-gray-50 dark:bg-gray-900 rounded border border-gray-200 dark:border-gray-700 text-sm font-mono text-blue-600 dar
10k:text-blue-300 truncate",children:n.csvColumn}),v.length>0&&e.jsxs("div",{className:"mt-1 text-xs text-gray-600 dark:text-gray-300 truncate",children:["Sample: ",v[0][n.csvColumn]||"(empty)"]})]}),e.jsxs("div",{className:"flex-1",children:[e.jsx("label",{className:"block text-xs font-medium text-gray-700 dark:text-gray-200 mb-2",children:"Maps to Database Field"}),e.jsxs("select",{value:n.dbField||"",onChange:N=>Z(n.csvColumn,N.target.value||null),className:"w-full px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500",children:[e.jsx("option",{value:"",children:"-- Skip this column --"}),g.map(N=>e.jsxs("option",{value:N.db_field_name,children:[N.display_label," ",N.is_required?"*":""," (",N.field_type,")"]},N.id))]}),((w=n.fieldDef)==null?void 0:w.description)&&e.jsx("div",{className:"mt-1 text-xs text-gray-600 dark:text-gray-300",children:n.fieldDef.description})]})]},`${n.csvColumn}-${D}`)})})]}),!M.isValid&&e.jsx("div",{className:"bg-red-50 dark:bg-red-950/30 border border-red-200 dark:border-red-800 rounded-lg p-4",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx(Ke,{className:"w-5 h-5 text-red-600 dark:text-red-300 mr-2 flex-shrink-0 mt-0.5"}),e.jsxs("div",{className:"flex-1",children:[e.jsx("h4",{className:"text-sm font-semibold text-red-900 mb-2",children:"Required Fields Missing"}),e.jsx("p",{className:"text-sm text-red-800 mb-2",children:"The following required fields must be mapped before you can continue:"}),e.jsx("ul",{className:"text-sm text-red-800 list-disc list-inside",children:M.missingRequired.map(n=>e.jsx("li",{children:n.display_label},n.id))})]})]})}),M.unmappedCount>0&&M.isValid&&e.jsx("div",{className:"bg-yellow-50 border border-yellow-200 rounded-lg p-4",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx(Hs,{className:"w-5 h-5 text-yellow-600 mr-2 flex-shrink-0 mt-0.5"}),e.jsx("div",{className:"flex-1",children:e.jsxs("p",{className:"text-sm text-yellow-800",children:[M.unmappedCount," column(s) will be skipped during import. This is okay if they contain data you don't need."]})})]})}),e.jsxs("div",{className:"flex justify-between items-center pt-4 border-t border-gray-200 dark:border-gray-700",children:[e.jsx("button",{onClick:o,className:"btn-secondary",children:"Back"}),e.jsxs("button",{onClick:c,disabled:!M.isValid,className:"btn-primary disabled:opacity-50 disabled:cursor-not-allowed",children:["Continue to Preview (",M.mappedCount," fields mapped)"]})]})]})},Ya=({isOpen:f,onClose:v,onSuccess:b})=>{const[o,g]=s.useState("upload"),[W,ee]=s.useState(null),[ce,S]=s.useState([]),[p,G]=s.useState([]),[E,ye]=s.useState({}),[me,ae]=s.useState(!1),[te,P]=s.useState(null),[k,B]=s.useState(null),[be,se]=s.useState(""),[le,pe]=s.useState("all"),[q,Z]=s.useState("all"),[V,c]=s.useState(!0),[M,ne]=s.useState(""),[_,n]=s.useState(""),[D,w]=s.useState(!1),N=s.useRef(null),[a,h]=s.useState(null);if(!f)return null;const I=i=>{i.preventDefault(),i.stopPropagation(),i.type==="dragenter"||i.type==="dragover"?w(!0):i.type==="dragleave"&&w(!1)},H=i=>{if(i.preventDefault(),i.stopPropagation(),w(!1),i.dataTransfer.files&&i.dataTransfer.files[0]){const u=i.dataTransfer.files[0];Ks(u)||ls(u)?Re(u):P("Please upload a CSV or Excel file (.csv, .xlsx, .xls)")}},X=i=>{const u=i.split(`
11`).filter(d=>d.trim());if(u.length===0)throw new Error("CSV file is empty");const O=[];let C="",T=!1;const K=u[0];for(let d=0;d<K.length;d++){const J=K[d],L=K[d+1];J==='"'?T&&L==='"'?(C+='"',d++):T=!T:J===","&&!T?(O.push(C.trim()),C=""):C+=J}O.push(C.trim());const l=[];for(let d=1;d<u.length;d++){const J=u[d].trim();if(!J)continue;const L=[];C="",T=!1;for(let Y=0;Y<J.length;Y++){const re=J[Y],r=J[Y+1];re==='"'?T&&r==='"'?(C+='"',Y++):T=!T:re===","&&!T?(L.push(C.trim()),C=""):C+=re}if(L.push(C.trim()),L.length>O.length){console.warn(`CSV row ${d}: Too many fields (${L.length} vs ${O.length}). Row skipped to prevent data misalignment.`);continue}for(;L.length<O.length;)L.push("");const ge={};O.forEach((Y,re)=>{ge[Y]=L[re]||""}),l.push(ge)}return{headers:O,data:l}},Re=async i=>{try{if(ee(i),P(null),ae(!0),ls(i)){const u=await qa(i);if("error"in u){P(`${u.error}${u.details?": "+u.details:""}`),ee(null);return}S(u.headers),G(u.data),h({totalRows:u.totalRows,rowsParsed:u.rowsParsed,rowsDropped:u.rowsDropped,emptyRowsDropped:u.emptyRowsDropped,nullRowsDropped:u.nullRowsDropped,emptyHeadersIgnored:u.emptyHeadersIgnored}),u.totalSheets>1&&console.log(`Note: Excel file contains ${u.totalSheets} sheets. Using first sheet: "${u.sheetName}"`),u.rowsDropped>0&&(console.log(`Parsing statistics: ${u.rowsParsed} rows parsed, ${u.rowsDropped} rows dropped (${u.emptyRowsDropped} empty, ${u.nullRowsDropped}
11 null)`),u.droppedRowDetails&&u.droppedRowDetails.length<=50&&console.table(u.droppedRowDetails)),u.emptyHeadersIgnored>0&&console.log(`Warning: ${u.emptyHeadersIgnored} empty header(s) replaced with auto-generated names (Column_A, Column_B, etc.)`)}else if(Ks(i)){const u=await i.text(),{headers:O,data:C}=X(u);S(O),G(C)}else P("Please upload a CSV or Excel file"),ee(null)}catch(u){P(u.message||"Failed to parse file"),ee(null)}finally{ae(!1)}},Ne=async i=>{i.target.files&&i.target.files[0]&&await Re(i.target.files[0])},He=()=>{if(!W||ce.length===0){P("Please select a valid CSV file");return}g("mapping")},_e=i=>{ye(i),g("preview")},Oe=(i,u)=>{const O=u.title,C=u.content,T=u.language,K=u.status,l=u.archived,d=u.last_modified_date,J=O?i[O]:"",L=C?i[C]:"";if(!J||!J.trim()||!L||!L.trim())return{willImport:!1,reason:"missing_content"};const ge=T?i[T]:"",Y=ge?ge.toLowerCase().trim():"en",re=le&&le!=="all"?le.toLowerCase().trim():null;if(re&&Y!==re)return{willImport:!1,reason:"language",value:Y};if(q!=="all"){if(!K)return console.error("Status filter is active but status column is not mapped!"),{willImport:!1,reason:"validation_error",value:"no_status_column"};const R=i[K]||"",$=(R==null?void 0:R.toString().trim().toUpperCase())==="DRAFT"?"DRAFT":"PUBLISHED";if(q==="published"&&$!=="PUBLISHED")return{willImport:!1,reason:"status",value:$.toLowerCase()};if(q==="draft"&&$!=="DRAFT")return{willImport:!1,reason:"status",value:$.toLowerCase()}}const r=l?i[l]:"";if(((r==null?void 0:r.toString().toUpperCase())==="TRUE"||(r==null?void 0:r.toString().toLowerCase())==="yes"||r==="1"||r===1)&&!V)return{willImport:!1,reason:"archived",value:"archived"};if((M||_)&&d){const R=i[d];if(R&&R.toString().trim()!=="")try{const A=new Date(R.toString().trim());if(!isNaN(A.getTime())){if(M){const $=new Date(M);if($.setHours(0,0,0,0),A<$)return{willImport:!1,reason:"date",value:"before_start"}}if(_){const $=new Date(_);if($.setHours(23,59,59,999),A>$)return{willImport:!1,reason:"date",value:"after_end"}}}}catch{}}return{willImport:!0,reason:null}},je=()=>{if(!p||p.length===0||!E)return{totalRows:0,rowsToImport:0,rowsFilteredLanguage:0,rowsFilteredStatus:0,rowsFilteredArchived:0,rowsFilteredDate:0};const i={};Object.entries(E).forEach(([l,d])=>{i[d]=l});let u=0,O=0,C=0,T=0,K=0;return p.forEach(l=>{const d=Oe(l,i);d.willImport?u++:d.reason==="language"?O++:d.reason==="status"?C++:d.reason==="archived"?T++:d.reason==="date"?K++:d.reason}),{totalRows:p.length,rowsToImport:u,rowsFilteredLanguage:O,rowsFilteredStatus:C,rowsFilteredArchived:T,rowsFilteredDate:K}},Pe=()=>{const i=[],u=["title","content"],O=Object.values(E);return u.forEach(C=>{O.includes(C)||i.push(`Required field '${C}' is not mapped`)}),Object.entries(E).forEach(([C])=>{ce.includes(C)||i.push(`Mapped column '${C}' does not exist in CSV headers`)}),i},qe=()=>p.map((u,O)=>{const C={};return Object.entries(E).forEach(([T,K])=>{const l=u[T];l!=null&&l!==""&&(C[K]=l)}),C.rowNumber=O+2,C}),Je=i=>{const u={};return Object.entries(E).forEach(([C,T])=>{u[T]=C}),i.filter(C=>{const T={};return Object.entries(E).forEach(([l,d])=>{T[l]=C[d]}),Oe(T,u).willImport})},ve=async()=>{if(!W){P("Please select a file");return}const i=Pe();if(i.length>0){P(`Mapping validation failed: ${i.join("; ")}`);return}const u={};Object.entries(E).forEach(([T,K])=>{u[K]=T});const O=u.status;if(q!=="all"&&!O){P('Status filter is active but the "status" column is not mapped. Please go back to the mapping step and map the status column, or change the status filter to "All Status".');return}const C=je();if(C.rowsToImport===0){P("No rows will be imported with current filters. Please adjust your filters and try again.");return}g("importing"),ae(!0),P(null),B(null),se("Preparing data...");try{se("Applying column mapping and filters...");const T=qe(),K=Je(T);if(K.length===0)throw new Error("No articles to import after applying filters");const l=150,d=Math.ceil(K.length/l);se(`Preparing to import ${K.length} articles in ${d} batch${d>1?"es":""}...`);const J="https://solxiyfrzksdatdzaxyy.supabase.co/functions/v1/import-csv-articles",{data:{session:L}}=await U.auth.getSession();if(!L)throw new Error("Not authenticated");let ge=0,Y=0,re=0,r=0,z=0,R=0,A=0;const $=[];for(let Q=0;Q<d;Q++){const xe=Q*l,Ie=Math.min(xe+l,K.length),Le=K.slice(xe,Ie),fe=Q+1,We=xe,Ye=Math.round(We/K.length*100);se(`Processing batch ${fe} of ${d} (${We}/${K.length} articles - ${Ye}%)
12Created: ${ge} | Updated: ${Y} | Skipped: ${re}`);const Me=await fetch(J,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${L.access_token}`},body:JSON.stringify({articles:Le,filename:W.name,importMetadata:{totalRows:(a==null?void 0:a.totalRows)||p.length,rowsParsed:(a==null?void 0:a.rowsParsed)||p.length,rowsDroppedDuringParsing:((a==null?void 0:a.totalRows)||p.length)-p.length,mappedColumns:Object.values(E),sourceType:ls(W)?"excel":"csv",totalArticlesAfterFiltering:K.length,dateFilter:M||_?{startDate:M,endDate:_}:null},filterSettings:{languageFilter:le,statusFilter:q,includeArchived:V,startDate:M,endDate:_},batchInfo:{current:fe,total:d,batchSize:Le.length}})}),Ee=await Me.json();if(!Me.ok)throw new Error(Ee.error||`Failed to import batch ${fe}`);ge+=Ee.articlesCreated||0,Y+=Ee.articlesUpdated||0,re+=Ee.articlesSkipped||0,r+=Ee.articlesNeedingResync||0,z+=Ee.rowsSkippedValidation||0,R+=Ee.categoriesCreated||0,A+=Ee.subcategoriesCreated||0,Ee.errors&&Ee.errors.length>0&&$.push(...Ee.errors),Q<d-1&&await new Promise(ft=>setTimeout(ft,300))}se("Import completed!");const ie={totalRows:(a==null?void 0:a.totalRows)||p.length,rowsParsed:(a==null?void 0:a.rowsParsed)||p.length,rowsDroppedDuringParsing:((a==null?void 0:a.totalRows)||p.length)-p.length,rowsSkippedValidation:z,rowsSkippedLanguageFilter:C.rowsFilteredLanguage,rowsSkippedStatus:C.rowsFilteredStatus,rowsSkippedArchived:C.rowsFilteredArchived,rowsSkippedDateFilter:C.rowsFilteredDate,articlesCreated:ge,articlesUpdated:Y,articlesSkipped:re,articlesNeedingResync:r,categoriesCreated:R,subcategoriesCreated:A,errors:$.length>0?$:void 0};B(ie),g("complete"),b()}catch(T){console.error("Import error:",T),P(T.message||"Failed to import data"),se(""),g("preview")}finally{ae(!1)}},Te=()=>{me||(g("upload"),ee(null),S([]),G([]),ye({}),B(null),P(null),se(""),pe("all"),Z("all"),c(!0),ne(""),n(""),v())},Fe=()=>{if(!k)return null;const i=k.errors&&k.errors.length>0,u=k.articlesCreated+k.articlesUpdated,O=k.rowsDroppedDuringParsing>0||k.rowsSkippedValidation>0;return e.jsxs("div",{className:"space-y-4",children:[O&&e.jsx("div",{className:"p-4 bg-yellow-50 border border-yellow-200 rounded-lg",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx(Ke,{className:"w-5 h-5 text-yellow-600 mr-2 flex-shrink-0 mt-0.5"}),e.jsxs("div",{className:"flex-1",children:[e.jsx("h4",{className:"text-sm font-semibold text-yellow-900 mb-2",children:"Some rows were not imported"}),e.jsxs("div",{className:"text-sm text-yellow-800 space-y-1",children:[e.jsx("p",{className:"font-medium mb-2",children:"Import Pipeline Breakdown:"}),e.jsxs("div",{className:"ml-3 space-y-1 border-l-2 border-yellow-300 pl-3",children:[e.jsxs("p",{children:["1. Excel file contained: ",e.jsx("span",{className:"font-semibold",children:k.totalRows})," data rows"]}),k.rowsDroppedDuringParsing>0&&e.jsxs("p",{className:"text-yellow-900",children:["2. Rows dropped during parsing: ",e.jsx("span",{className:"font-semibold",children:k.rowsDroppedDuringParsing})," (empty rows)"]}),e.jsxs("p",{children:["3. Rows successfully parsed: ",e.jsx("span",{className:"font-semibold",children:k.rowsParsed})]}),k.rowsSkippedValidation>0&&e.jsxs("p",{className:"text-yellow-900",children:["4. Rows skipped (missing title/content): ",e.jsx("span",{className:"font-semibold",children:k.rowsSkippedValidation})]}),k.rowsSkippedLanguageFilter>0&&e.jsxs("p",{children:["5. Rows skipped (language filter): ",e.jsx("span",{className:"font-semibold",children:k.rowsSkippedLanguageFilter})]}),k.rowsSkippedStatus>0&&e.jsxs("p",{children:["6. Rows skipped (status filter): ",e.jsx("span",{className:"font-semibold",children:k.rowsSkippedStatus})]}),k.rowsSkippedArchived>0&&e.jsxs("p",{children:["7. Rows skipped (archived): ",e.jsx("span",{className:"font-semibold",children:k.rowsSkippedArchived})]}),k.rowsSkippedDateFilter>0&&e.jsxs("p",{children:["8. Rows skipped (date range filter): ",e.jsx("span",{className:"font-semibold",children:k.rowsSkippedDateFilter})]}),e.jsxs("p",{className:"font-semibold text-green-900 pt-1",children:["✓ Final articles imported: ",e.jsx("span",{className:"font-bold",children:u})]})]}),a&&a.rowsDropped>0&&e.jsx("p",{className:"text-xs text-yellow-700 mt-2 italic",children:"Check browser console for detailed list of dropped rows"})]})]})]})}),e.jsx("div",{className:"p-4 bg-green-50 border border-green-200 rounded-lg",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx(Se,{className:"w-5 h-5 text-green-600 mr-2 flex-shrink-0 mt-0.5"}),e.jsxs("div",{className:"flex-1",children:[e.jsx("h4",{className:"text-sm font-semibold text-green-900 mb-2",children:"Import Completed"}),e.jsxs("div",{className:"text-sm text-green-800 dark:text-green-200 space-y-1",children:[e.jsxs("p",{children:["Articles successfully imported: ",e.jsx("span",{className:"font-semibold",children:u})]}),e.jsxs("p",{className:"ml-4",children:["New articles created: ",e.jsx("span",{className:"font-semibold",children:k.articlesCreated})]}),e.jsxs("p",{className:"ml-4",children:["Existing articles updated: ",e.jsx("span",{className:"font-semibold",children:k.articlesUpdated})]}),k.articlesNeedingResync>0&&e.jsxs("p",{className:"ml-4 text-blue-800 dark:text-blue-200",children:["Articles needing resync to OpenAI: ",e.jsx("span",{className:"font-semibold",children:k.articlesNeedingResync}),e.jsx("span",{className:"text-xs block ml-6 mt-1",children:"(These articles were previously synced but have been updated with new content)"})]}),e.jsxs("p",{children:["Categories created: ",e.jsx("span",{className:"font-semibold",children:k.categoriesCreated})]}),e.jsxs("p",{children:["Subcategories created: ",e.jsx("span",{className:"font-semibold",children:k.subcategoriesCreated})]}),k.imageStats&&k.imageStats.imagesFound>0&&e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"border-t border-green-300 my-2 pt-2",children:e.jsx("p",{className:"font-medium text-green-900 mb-1",children:"Image Processing:"})}),e.jsxs("p",{className:"ml-4",children:["Images found: ",e.jsx("span",{className:"font-semibold",children:k.imageStats.imagesFound})]}),e.jsxs("p",{className:"ml-4 text-green-700",children:["Images downloaded: ",e.jsx("span",{className:"font-semibold",children:k.imageStats.imagesDownloaded})]}),k.imageStats.imagesFailed>0&&e.jsxs("p",{className:"ml-4 text-yellow-700",children:["Images failed: ",e.jsx("span",{className:"font-semibold",children:k.imageStats.imagesFailed})]}),e.jsxs("p",{className:"ml-4 text-xs text-gray-600 dark:text-gray-300",children:["Total size: ",k.imageStats.totalSizeMB,"MB"]})]})]})]})]})}),i&&e.jsx("div",{className:"p-4 bg-yellow-50 border border-yellow-200 rounded-lg",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx(Ke,{className:"w-5 h-5 text-yellow-600 mr-2 flex-shrink-0 mt-0.5"}),e.jsxs("div",{className:"flex-1",children:[e.jsx("h4",{className:"text-sm font-semibold text-yellow-900 mb-2",children:"Some rows failed to import"}),e.jsxs("div",{className:"text-sm text-yellow-800",children:[e.jsxs("p",{className:"mb-2",children:[k.errors.length," row(s) encountered errors:"]}),e.jsx("div",{className:"max-h-40 overflow-y-auto space-y-2",children:k.errors.map((C,T)=>e.jsxs("div",{className:"bg-white dark:bg-gray-800 rounded p-2 text-xs",children:[e.jsxs("p",{className:"font-medium",children:["Row ",C.rowNumber,": ",C.title]}),e.jsx("p",{className:"text-yellow-700 mt-1",children:C.error})]},T))})]})]})]})}),e.jsx("div",{className:"bg-blue-50 border border-blue-200 rounded-lg p-3",children:e.jsxs("p",{className:"text-sm text-blue-800 dark:text-blue-200",children:[e.jsx("span",{className:"font-semibold",children:"Next Steps:"})," All imported articles are now available in your Knowledge Base. Don't forget to sync them to OpenAI Vector Store to make them available for AI chat!"]})})]})};return e.jsx("div",{className:"fixed inset-0 z-50 overflow-y-auto",children:e.jsxs("div",{className:"flex items-center justify-center min-h-screen px-4 pt-4 pb-20 text-center sm:block sm:p-0",children:[e.jsx("div",{className:"fixed inset-0 transition-opacity bg-gray-500 bg-opacity-75",onClick:Te}),e.jsxs("div",{className:"inline-block w-full max-w-2xl my-8 overflow-hidden text-left align-middle transition-all transform bg-white dark:bg-gray-800 rounded-lg shadow-xl",children:[e.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b border-gray-200 dark:border-gray-700",children:[e.jsxs("div",{className:"flex items-center",children:[e.jsx(yt,{className:"w-6 h-6 text-blue-600 dar
12k:text-blue-300 mr-2"}),e.jsx("h3",{className:"text-lg font-semibold text-gray-900 dark:text-gray-50",children:"Import from CSV"})]}),e.jsx("button",{onClick:Te,disabled:me,className:"text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:text-gray-300 transition-colors disabled:opacity-50",children:e.jsx(Vt,{className:"w-6 h-6"})})]}),e.jsxs("div",{className:"px-6 py-4",children:[te&&e.jsxs("div",{className:"mb-4 p-3 bg-red-50 dark:bg-red-950/30 border border-red-200 dark:border-red-800 rounded-lg flex items-start",children:[e.jsx(Ke,{className:"w-5 h-5 text-red-600 dark:text-red-300 mr-2 flex-shrink-0 mt-0.5"}),e.jsx("div",{className:"flex-1",children:e.jsx("span",{className:"text-sm text-red-600 dark:text-red-300",children:te})})]}),o==="upload"&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:`border-2 border-dashed rounded-lg p-8 text-center transition-colors ${D?"border-blue-500 bg-blue-50":W?"border-green-500 bg-green-50":"border-gray-300 dark:border-gray-600 hover:border-gray-400"}`,onDragEnter:I,onDragLeave:I,onDragOver:I,onDrop:H,children:[e.jsx("input",{ref:N,type:"file",accept:Ha(),onChange:Ne,className:"hidden"}),W?e.jsxs("div",{className:"space-y-3",children:[e.jsx(Se,{className:"w-12 h-12 text-green-600 mx-auto"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-gray-900 dark:text-gray-50",children:W.name}),e.jsxs("p",{className:"text-xs text-gray-600 dark:text-gray-300 mt-1",children:[Oa(W)," file • ",(W.size/1024).toFixed(2)," KB"]}),a&&e.jsxs("div",{className:"text-xs text-gray-600 dark:text-gray-300 mt-2 space-y-1",children:[e.jsxs("p",{children:[a.totalRows," total rows → ",a.rowsParsed," parsed successfully"]}),a.rowsDropped>0&&e.jsxs("p",{className:"text-yellow-700 font-medium",children:["⚠ ",a.rowsDropped," empty rows excluded"]}),a.emptyHeadersIgnored>0&&e.jsxs("p",{className:"text-blue-700",children:["ℹ ",a.emptyHeadersIgnored," empty header(s) auto-named"]})]})]}),e.jsx("button",{onClick:()=>{var i;return(i=N.current)==null?void 0:i.click()},className:"text-sm text-blue-600 dark:text-blue-300 hover:text-blue-700 underline",children:"Choose a different file"})]}):e.jsxs("div",{className:"space-y-3",children:[e.jsx(ta,{className:"w-12 h-12 text-gray-400 dark:text-gray-500 mx-auto"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-gray-900 dark:text-gray-50",children:"Drop your file here, or click to browse"}),e.jsx("p",{className:"text-xs text-gray-600 dark:text-gray-300 mt-1",children:"Supports CSV and Excel files (.csv, .xlsx, .xls)"})]}),e.jsx("button",{onClick:()=>{var i;return(i=N.current)==null?void 0:i.click()},className:"btn-secondary text-sm",children:"Select File"})]})]}),e.jsxs("div",{className:"bg-blue-50 border border-blue-200 rounded-lg p-4",children:[e.jsx("h4",{className:"text-sm font-semibold text-gray-900 dark:text-gray-50 mb-2",children:"Import Filters:"}),e.jsx("p",{className:"text-xs text-gray-700 dark:text-gray-200 mb-3 italic",children:"All filters work together - articles must meet ALL active filter criteria to be imported"}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-200 mb-1",children:"Language Filter"}),e.jsxs("select",{value:le,onChange:i=>pe(i.target.value),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500",children:[e.jsx("option",{value:"all",children:"All Languages"}),e.jsx("option",{value:"en",children:"English Only"}),e.jsx("option",{value:"hu",children:"Hungarian Only"}),e.jsx("option",{value:"es",children:"Spanish Only"}),e.jsx("option",{value:"fr",children:"French Only"}),e.jsx("option",{value:"de",children:"German Only"})]})]}),e.jsxs("div",{children:[e.jsxs("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-200 mb-1",children:["Status Filter",e.jsx("span",{className:"text-xs text-gray-500 dark:text-gray-400 font-normal ml-1",children:"(requires status column mapping)"})]}),e.jsxs("select",{value:q,onChange:i=>Z(i.target.value),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500",children:[e.jsx("option",{value:"all",children:"All Status"}),e.jsx("option",{value:"published",children:"Published Only"}),e.jsx("option",{value:"draft",children:"Draft Only"})]}),q!=="all"&&e.jsxs("div",{className:"text-xs mt-1 space-y-1",children:[e.jsx("p",{className:"text-blue-600 dar
12k:text-blue-300",children:"ℹ️ Make sure to map the 'status' column in the next step"}),e.jsx("p",{className:"text-gray-600 dark:text-gray-300",children:'Note: Articles with empty/missing status values are treated as "Published"'})]})]}),e.jsxs("div",{className:"flex items-center",children:[e.jsx("input",{type:"checkbox",id:"includeArchived",checked:V,onChange:i=>c(i.target.checked),className:"rounded border-gray-300 dark:border-gray-600 text-blue-600 dark:text-blue-300 focus:ring-blue-500"}),e.jsx("label",{htmlFor:"includeArchived",className:"ml-2 text-sm text-gray-700 dark:text-gray-200",children:"Include archived articles"})]}),e.jsxs("div",{className:"border-t border-blue-300 pt-3",children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-200 mb-2",children:"Date Range Filter (Optional)"}),e.jsx("p",{className:"text-xs text-gray-600 dark:text-gray-300 mb-2",children:"Import only articles modified within this date range"}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-gray-600 dark:text-gray-300 mb-1",children:"Start Date"}),e.jsx("input",{type:"date",value:M,onChange:i=>ne(i.target.value),max:_||void 0,className:"w-full px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-medium text-gray-600 dark:text-gray-300 mb-1",children:"End Date"}),e.jsx("input",{type:"date",value:_,onChange:i=>n(i.target.value),min:M||void 0,className:"w-full px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"})]})]}),(M||_)&&e.jsx("p",{className:"text-xs text-blue-700 mt-2",children:M&&_?`Articles between ${new Date(M).toLocaleDateString()} and ${new Date(_).toLocaleDateString()}`:M?`Articles from ${new Date(M).toLocaleDateString()} onwards`:`Articles until ${new Date(_).toLocaleDateString()}`})]})]})]}),e.jsxs("div",{className:"bg-gray-50 dark:bg-gray-900 border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:[e.jsx("h4",{className:"text-sm font-semibold text-gray-900 dark:text-gray-50 mb-2",children:"What's Next:"}),e.jsxs("ul",{className:"text-sm text-gray-700 dark:text-gray-200 space-y-1 list-disc list-inside",children:[e.jsx("li",{children:"Upload your CSV or Excel file with knowledge base articles"}),e.jsx("li",{children:"Excel files (.xlsx, .xls) are recommended for better reliability"}),e.jsx("li",{children:"Map your file columns to database fields"}),e.jsx("li",{children:"Preview and confirm the import"}),e.jsx("li",{children:"HTML content will be converted to Markdown automatically"}),e.jsx("li",{children:"Categories and subcategories will be created as needed"})]})]}),e.jsx("div",{className:"bg-yellow-50 border border-yellow-200 rounded-lg p-4",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx(Ke,{className:"w-5 h-5 text-yellow-600 mr-2 flex-shrink-0 mt-0.5"}),e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-semibold text-gray-900 dark:text-gray-50 mb-1",children:"Important Notes:"}),e.jsxs("ul",{className:"text-sm text-gray-700 dark:text-gray-200 space-y-1 list-disc list-inside",children:[e.jsx("li",{children:"Excel files handle empty fields better than CSV (recommended)"}),e.jsx("li",{children:"Large files may take several minutes to process"}),e.jsx("li",{children:"All imported articles will need to be synced to OpenAI separately"}),e.jsx("li",{children:"Duplicate articles (same title) will be updated automatically"})]})]})]})})]}),o==="mapping"&&e.jsx(Ja,{csvHeaders:ce,csvData:p,onMappingComplete:_e,onBack:()=>g("upload")}),o==="preview"&&e.jsxs("div",{className:"space-y-4",children:[(()=>{const i=je(),u=i.rowsFilteredLanguage>0||i.rowsFilteredStatus>0||i.rowsFilteredArchived>0||i.rowsFilteredDate>0,O=i.rowsToImport===0&&i.totalRows>0;return e.jsxs(e.Fragment,{children:[(()=>{const C={};Object.entries(E).forEach(([d,J])=>{C[J]=d});const T=C.status,l=q!=="all"&&!T;return e.jsxs(e.Fragment,{children:[l&&e.jsx("div",{className:"border-2 border-red-400 bg-red-50 dark:bg-red-950/30 rounded-lg p-4 mb-4",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx(Ke,{className:"w-6 h-6 text-red-600 dark:text-red-300 mr-2 flex-shrink-0 mt-0.5"}),e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-bold text-red-900 mb-2",children:"⛔ Cannot Import: Status Filter Requirements Not Met"}),e.jsxs("div",{className:"text-sm text-red-800 space-y-2",children:[e.jsxs("p",{className:"font-medium",children:['The status filter is set to "',q==="published"?"Published Only":"Draft Only",`" but the 'status' column is not mapped!`]}),e.jsxs("div",{className:"bg-red-100 border border-red-300 rounded p-2 mt-2",children:[e.jsx("p",{className:"font-semibold text-red-900 mb-1",children:"Required Action:"}),e.jsxs("ul",{className:"text-xs space-y-1 ml-4 list-disc",children:[e.jsx("li",{children:"Go back to the mapping step and map your CSV's status column to the 'status' database field"}),e.jsx("li",{children:'OR change the Status Filter to "All Status" to import all articles regardless of status'})]})]}),e.jsx("p",{className:"text-xs mt-2 text-red-700",children:'ℹ️ The status column should contain values like "DRAFT" or "PUBLISHED". Without mapping this column, the status filter cannot work correctly.'})]})]})]})}),a&&a.rowsDropped>0&&e.jsx("div",{className:"border border-yellow-300 bg-yellow-50 rounded-lg p-4 mb-4",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx(Ke,{className:"w-5 h-5 text-yellow-600 mr-2 flex-shrink-0 mt-0.5"}),e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-semibold text-yellow-900 mb-2",children:"Parsing Information"}),e.jsxs("div",{className:"text-sm text-yellow-800 space-y-1",children:[e.jsxs("p",{children:[a.rowsDropped," empty row(s) were automatically excluded during file parsing"]}),a.emptyRowsDropped>0&&e.jsxs("p",{className:"text-xs",children:["• ",a.emptyRowsDropped," completely empty rows"]}),a.nullRowsDropped>0&&e.jsxs("p",{className:"text-xs",children:["• ",a.nullRowsDropped," null rows"]}),e.jsx("p",{className:"text-xs mt-2 italic",children:"Check browser console for detailed row numbers"})]})]})]})})]})})(),e.jsxs("div",{className:`border rounded-lg p-4 ${O?"bg-red-50 dark:bg-red-950/30 border-red-200 dark:border-red-800":"bg-blue-50 border-blue-200"}`,children:[e.jsx("h4",{className:"text-sm font-semibold text-gray-900 dark:text-gray-50 mb-2",children:"Import Preview"}),e.jsxs("div",{className:"space-y-2",children:[a?e.jsxs(e.Fragment,{children:[e.jsxs("p",{className:"text-sm text-gray-700 dark:text-gray-200",children:[e.jsx("span",{className:"font-semibold",children:"Total rows in file:"})," ",a.totalRows]}),e.jsxs("p",{className:"text-sm text-gray-700 dark:text-gray-200",children:[e.jsx("span",{className:"font-semibold",children:"Rows parsed successfully:"}
12)," ",a.rowsParsed]})]}):e.jsxs("p",{className:"text-sm text-gray-700 dark:text-gray-200",children:[e.jsx("span",{className:"font-semibold",children:"Total rows in file:"})," ",i.totalRows]}),e.jsxs("p",{className:"text-sm text-gray-700 dark:text-gray-200",children:[e.jsx("span",{className:"font-semibold",children:"Rows to be imported:"})," ",e.jsx("span",{className:O?"text-red-600 dark:text-red-300 font-bold":"text-green-600 font-bold",children:i.rowsToImport})]}),u&&e.jsxs("div",{className:"mt-2 pt-2 border-t border-blue-300",children:[e.jsx("p",{className:"text-xs font-medium text-gray-700 dark:text-gray-200 mb-1",children:"Rows excluded by filters (articles must pass ALL filter criteria):"}),i.rowsFilteredLanguage>0&&e.jsxs("p",{className:"text-xs text-gray-600 dark:text-gray-300 ml-2",children:["• ",i.rowsFilteredLanguage," row(s) due to language filter (",le,")"]}),i.rowsFilteredStatus>0&&e.jsxs("p",{className:"text-xs text-gray-600 dark:text-gray-300 ml-2",children:["• ",i.rowsFilteredStatus," row(s) due to status filter (",q==="published"?"excluding drafts":q==="draft"?"excluding published":"",")"]}),i.rowsFilteredArchived>0&&e.jsxs("p",{className:"text-xs text-gray-600 dark:text-gray-300 ml-2",children:["• ",i.rowsFilteredArchived," row(s) because they are archived"]}),i.rowsFilteredDate>0&&e.jsxs("p",{className:"text-xs text-gray-600 dark:text-gray-300 ml-2",children:["• ",i.rowsFilteredDate," row(s) due to date range filter",M&&_?` (${new Date(M).toLocaleDateString()} - ${new Date(_).toLocaleDateString()})`:M?` (from ${new Date(M).toLocaleDateString()})`:_?` (until ${new Date(_).toLocaleDateString()})`:""]})]}),e.jsxs("p",{className:"text-sm text-gray-700 dark:text-gray-200 mt-2",children:[e.jsx("span",{className:"font-semibold",children:"Mapped columns:"})," ",Object.keys(E).length]})]}),O&&e.jsx("div",{className:"mt-3 p-2 bg-red-100 border border-red-300 rounded",children:e.jsx("p",{className:"text-sm text-red-800 font-medium",children:"Warning: All rows will be filtered out. No articles will be imported. Please adjust your language filter or archived settings."})})]})]})})(),e.jsxs("div",{className:"bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:[e.jsx("h4",{className:"text-sm font-semibold text-gray-900 dark:text-gray-50 mb-3",children:"Column Mappings"}),e.jsx("div",{className:"space-y-2",children:Object.entries(E).map(([i,u])=>e.jsxs("div",{className:"flex items-center justify-between text-sm p-2 bg-gray-50 dark:bg-gray-900 rounded",children:[e.jsx("span",{className:"font-mono text-blue-600 dark:text-blue-300",children:i}),e.jsx("span",{className:"text-gray-500 dark:text-gray-400",children:"→"}),e.jsx("span",{className:"font-medium text-gray-900 dark:text-gray-50",children:u})]},i))})]}),e.jsxs("div",{className:"bg-gray-50 dark:bg-gray-900 border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden",children:[e.jsxs("div",{className:"p-4 border-b border-gray-200 dark:border-gray-700",children:[e.jsx("h4",{className:"text-sm font-semibold text-gray-900 dark:text-gray-50",children:"Preview: First 3 Rows"}),e.jsxs("p",{className:"text-xs text-gray-600 dark:text-gray-300 mt-1",children:[e.jsx("span",{className:"inline-block w-3 h-3 bg-green-100 dark:bg-green-900/40 border border-green-300 rounded mr-1"}),"Will be imported",e.jsx("span",{className:"inline-block w-3 h-3 bg-red-100 border border-red-300 rounded ml-3 mr-1"}),"Will be skipped"]})]}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-gray-100 dark:bg-gray-800",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-3 py-2 text-left font-medium text-gray-700 dark:text-gray-200 w-20",children:"Status"}),Object.keys(E).map(i=>e.jsx("th",{className:"px-3 py-2 text-left font-medium text-gray-700 dark:text-gray-200",children:i},i))]})}),e.jsx("tbody",{children:(()=>{const i={};return Object.entries(E).forEach(([u,O])=>{i[O]=u}),p.slice(0,3).map((u,O)=>{const C={...u,_index:O+2},T=Oe(C,i),K=T.willImport?"bg-green-50":"bg-red-50 dark:bg-red-950/30",l=i.status,d=i.language,J=l?u[l]:"",L=d?u[d]:"",ge=(J==null?void 0:J.toString().trim().toUpperCase())==="DRAFT"?"DRAFT":"PUBLISHED",Y=L?L.toLowerCase().trim():"en";return e.jsxs("tr",{className:`border-b border-gray-100 dark:border-gray-700 ${K}`,children:[e.jsx("td",{className:"px-3 py-2",children:T.willImport?e.jsxs("div",{className:"text-xs",children:[e.jsxs("span",{className:"inline-flex items-center font-medium text-green-700",children:[e.jsx(Se,{className:"w-4 h-4 mr-1"}),"Import"]}),(l||d)&&e.jsxs("div",{className:"text-[10px] text-gray-600 dark:text-gray-300 mt-1 space-y-0.5",children:[d&&e.jsxs("div",{children:["Lang: ",e.jsx("span",{className:"font-semibold",children:Y.toUpperCase()})]}),l&&e.jsxs("div",{children:["Status: ",e.jsx("span",{className:"font-semibold",children:ge})]})]})]}):e.jsxs("div",{className:"text-xs",children:[e.jsxs("span",{className:"inline-flex items-center font-medium text-red-700",children:[e.jsx(Vt,{className:"w-4 h-4 mr-1"}),"Skip"]}
12),e.jsxs("div",{className:"text-[10px] text-red-600 dark:text-red-300 mt-1 space-y-0.5",children:[T.reason==="language"&&e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"font-semibold",children:"Language mismatch"}),e.jsxs("div",{children:["Has: ",T.value]}),e.jsxs("div",{children:["Need: ",le]})]}),T.reason==="status"&&e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"font-semibold",children:"Status mismatch"}),e.jsxs("div",{children:["Has: ",ge]}),e.jsxs("div",{children:["Need: ",q.toUpperCase()]})]}),T.reason==="archived"&&e.jsx("div",{className:"font-semibold",children:"Archived"}),T.reason==="missing_content"&&e.jsx("div",{className:"font-semibold",children:"Missing content"}),T.reason==="validation_error"&&e.jsx("div",{className:"font-semibold text-red-800",children:"Validation error"})]})]})}),Object.keys(E).map(re=>e.jsxs("td",{className:`px-3 py-2 ${T.willImport?"text-gray-900 dark:text-gray-50":"text-gray-500 dark:text-gray-400"}`,children:[String(u[re]||"").substring(0,50),String(u[re]||"").length>50?"...":""]},re))]},O)})})()})]})})]})]}),o==="importing"&&e.jsxs("div",{className:"flex flex-col items-center justify-center py-8 space-y-4",children:[e.jsx("div",{className:"animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600"}),e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"text-sm font-medium text-gray-900 dark:text-gray-50 whitespace-pre-line",children:be}),e.jsx("p",{className:"text-xs text-gray-600 dark:text-gray-300 mt-3",children:"This may take a few minutes. Please don't close this window."})]})]}),o==="complete"&&Fe()]}),e.jsxs("div",{className:"flex justify-end space-x-3 px-6 py-4 bg-gray-50 dark:bg-gray-900 border-t border-gray-200 dark:border-gray-700",children:[o==="upload"&&e.jsxs(e.Fragment,{children:[e.jsx("button",{onClick:Te,className:"btn-secondary",children:"Cancel"}),e.jsx("button",{onClick:He,disabled:!W||ce.length===0,className:"btn-primary flex items-center disabled:opacity-50 disabled:cursor-not-allowed",children:"Next: Map Columns"})]}),o==="preview"&&e.jsxs(e.Fragment,{children:[e.jsx("button",{onClick:()=>g("mapping"),className:"btn-secondary",children:"Back to Mapping"}),e.jsxs("button",{onClick:ve,disabled:(()=>{const i={};Object.entries(E).forEach(([T,K])=>{i[K]=T});const u=i.status,C=q!=="all"&&!u;return Object.keys(E).length===0||je().rowsToImport===0||C})(),className:"btn-primary flex items-center disabled:opacity-50 disabled:cursor-not-allowed",children:[e.jsx(yt,{className:"w-4 h-4 mr-2"}),"Start Import"]})]}),o==="complete"&&e.jsx("button",{onClick:Te,className:"btn-primary",children:"Close"})]})]})]})})},Xa=/^(?:hu|en|lt|de|es|el|tr|th|fr|ro)(?:\/|$)/i;function xt(f){return f.toLowerCase().normalize("NFKD").replace(/[\u0300-\u036f]/g,"").replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,"")}function Ys(f){if(!f||!f.trim())return null;try{let v=f.trim();/^https?:\/\//i.test(v)?v=new URL(v).pathname:v.includes("?")&&(v=v.split("?")[0]);const b=v.split("/").map(g=>g.trim()).filter(Boolean);if(b.length===0)return null;Xa.test(b[0]+"/")&&b.shift();const o=b[b.length-1];return o?xt(decodeURIComponent(o)):null}catch{return null}}function ns(f){const v=xt(f);return v?new Set(v.split("-").filter(b=>b.length>=2)):new Set}function Xs(f,v){if(f.size===0||v.size===0)return 0;let b=0;for(const g of f)v.has(g)&&(b+=1);const o=f.size+v.size-b;return o===0?0:b/o}const rt={url_slug:"URL slug",title_slug:"Title slug",title_tokens:"Similar title"};function Qa(f,v){const b=Ys(f.source_url),o=Ys(v.source_url),g=xt(v.title),W=xt(f.title);if(b&&o&&b===o)return{...v,score:1,reason:"url_slug",reasonLabel:rt.url_slug};if(b&&g&&b===g)return{...v,score:.95,reason:"url_slug",reasonLabel:rt.url_slug};if(o&&W&&o===W)return{...v,score:.9,reason:"title_slug",reasonLabel:rt.title_slug};if(W&&g&&W===g)return{...v,score:.85,reason:"title_slug",reasonLabel:rt.title_slug};if(b&&o&&(b.includes(o)||o.includes(b))){const p=Math.min(b.length,o.length),G=Math.max(b.length,o.length),E=.55+.35*(p/G);return{...v,score:E,reason:"url_slug",reasonLabel:rt.url_slug}}if(b&&g&&(b.includes(g)||g.includes(b))){const p=Math.min(b.length,g.length),G=Math.max(b.length,g.length),E=.5+.3*(p/G);return{...v,score:E,reason:"url_slug",reasonLabel:rt.url_slug}}const ee=Xs(ns(f.title),ns(v.title)),ce=b&&g?Xs(new Set(b.split("-").filter(p=>p.length>=2)),ns(v.title)):0,S=Math.max(ee*.7,ce*.85);return{...v,score:S,reason:(S>
120,"title_tokens"),reasonLabel:rt.title_tokens}}function Za(f,v,b=5){return v.map(o=>Qa(f,o)).filter(o=>o.score>=.15).sort((o,g)=>g.score-o.score||o.title.localeCompare(g.title)).slice(0,b)}function er(f,v){const b=v.trim().toLowerCase();if(!b)return[];const o=xt(b);return f.filter(g=>{const W=g.title.toLowerCase(),ee=(g.source_url||"").toLowerCase(),ce=xt(g.title);return W.includes(b)||ee.includes(b)||g.id.toLowerCase().includes(b)||o.length>0&&(ce.includes(o)||ce===o)}).slice(0,25)}const tr=({onBack:f})=>{var D,w,N;const[v,b]=s.useState([]),[o,g]=s.useState([]),[W,ee]=s.useState(!0),[ce,S]=s.useState(null),[p,G]=s.useState(""),[E,ye]=s.useState(""),[me,ae]=s.useState(null),[te,P]=s.useState(null),[k,B]=s.useState(null),[be,se]=s.useState(!1),[le,pe]=s.useState(null),q=s.useCallback(async()=>{ee(!0),S(null);try{const[a,h]=await Promise.all([U.rpc("list_unmatched_hu_knowledge_articles"),U.from("knowledge_articles").select(`
13            id,
14            title,
15            source_url,
16            module_id,
17            category_id,
18            subcategory_id,
19            tags,
20            version,
21            status,
22            translation_group_id,
23            module:knowledge_modules!module_id(name),
24            category:knowledge_categories!knowledge_articles_category_id_fkey(name),
25            subcategory:knowledge_categories!knowledge_articles_subcategory_id_fkey(name)
26          `).eq("language","en").eq("archived",!1).order("title",{ascending:!0})]);if(a.error)throw a.error;if(h.error)throw h.error;const I=a.data||[];b(I),g((h.data||[]).map(H=>({...H,module:Array.isArray(H.module)?H.module[0]??null:H.module??null,category:Array.isArray(H.category)?H.category[0]??null:H.category??null,subcategory:Array.isArray(H.subcategory)?H.subcategory[0]??null:H.subcategory??null}))),ae(H=>{var X;return H&&I.some(Re=>Re.id===H)?H:((X=I[0])==null?void 0:X.id)??null}),P(null),B(null)}catch(a){console.error("Failed to load matching data:",a),S(a.message||"Failed to load articles for matching")}finally{ee(!1)}},[]);s.useEffect(()=>{q()},[q]);const Z=s.useMemo(()=>v.find(a=>a.id===me)??null,[v,me]),V=s.useMemo(()=>o.find(a=>a.id===te)??null,[o,te]),c=s.useMemo(()=>{const a=p.trim().toLowerCase();return a?v.filter(h=>h.title.toLowerCase().includes(a)||(h.source_url||"").toLowerCase().includes(a)||(h.module_name||"").toLowerCase().includes(a)):v},[v,p]),M=s.useMemo(()=>Z?Za({title:Z.title,source_url:Z.source_url},o.map(a=>({id:a.id,title:a.title,source_url:a.source_url})),5):[],[Z,o]),ne=s.useMemo(()=>E.trim()?er(o.map(h=>({id:h.id,title:h.title,source_url:h.source_url})),E).map(h=>o.find(I=>I.id===h.id)).filter(h=>!!h):[],[o,E]);s.useEffect(()=>{let a=!1;async function h(){if(!V){B(null);return}const I=V.translation_group_id||V.id,{data:H,error:X}=await U.from("knowledge_articles").select("id, title").eq("language","hu").or(`translation_group_id.eq.${I},translation_group_id.eq.${V.id},id.eq.${I}`).limit(1).maybeSingle();if(!a){if(X&&X.code!=="PGRST116"){console.error("Failed to check existing HU sibling:",X),B(null);return}B(H?{id:H.id,title:H.title}:null)}}return h(),()=>{a=!0}},[V]);const _=a=>{ae(a),P(null),B(null),pe(null),ye("")},n=async()=>{if(!(!Z||!V)){se(!0),S(null),pe(null);try{const{data:a,error:h}=await U.rpc("match_hu_knowledge_article_to_en",{p_hu_id:Z.id,p_en_id:V.id});if(h)throw h;const I=a;if(!(I!=null&&I.success))throw new Error((I==null?void 0:I.error)||"Match failed");
26const H=I.displaced_hu_title?` Previous Hungarian pair “${I.displaced_hu_title}” was unlinked.`:"";pe(`Linked “${Z.title}” to “${V.title}”. Taxonomy and tags copied; kept as DRAFT.${H}`),P(null),await q()}catch(a){console.error("Match failed:",a),S(a.message||"Failed to match articles")}finally{se(!1)}}};return W?e.jsxs("div",{className:"flex items-center justify-center py-24",children:[e.jsx(ke,{className:"w-6 h-6 text-gray-400 animate-spin mr-2"}),e.jsx("span",{className:"text-gray-600",children:"Loading unmatched Hungarian articles…"})]}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3",children:[e.jsxs("div",{children:[e.jsxs("button",{type:"button",onClick:f,className:"inline-flex items-center text-sm text-gray-600 hover:text-gray-900 mb-2",children:[e.jsx(ea,{className:"w-4 h-4 mr-1"}),"Back to articles"]}),e.jsx("h2",{className:"text-xl font-semibold text-gray-900",children:"Match Hungarian articles"}),e.jsx("p",{className:"text-sm text-gray-600 mt-1",children:"Pair unmatched Hungarian articles with their English originals. Hungarian rows inherit module, category, subcategory, and tags; they stay DRAFT and keep their version."})]}),e.jsxs("div",{className:"text-sm text-gray-500 whitespace-nowrap",children:[v.length," unmatched"]})]}),ce&&e.jsxs("div",{className:"p-3 bg-red-50 border border-red-200 rounded-lg flex items-start",children:[e.jsx(et,{className:"w-5 h-5 text-red-600 mr-2 flex-shrink-0"}),e.jsx("span",{className:"text-sm text-red-600",children:ce})]}),le&&e.jsxs("div",{className:"p-3 bg-green-50 border border-green-200 rounded-lg flex items-start",children:[e.jsx(Se,{className:"w-5 h-5 text-green-600 mr-2 flex-shrink-0"}),e.jsx("span",{className:"text-sm text-green-700",children:le})]}),v.length===0?e.jsx("div",{className:"bg-white border border-gray-200 rounded-lg p-10 text-center text-gray-600",children:"All Hungarian articles are already linked to an English pair."}):e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-4 items-stretch",children:[e.jsxs("div",{className:"bg-white border border-gray-200 rounded-lg flex flex-col overflow-hidden min-h-[calc(100vh-11rem)] max-h-[calc(100vh-11rem)]",children:[e.jsx("div",{className:"p-3 border-b border-gray-200",children:e.jsxs("div",{className:"relative",children:[e.jsx(ds,{className:"w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-gray-400"}),e.jsx("input",{type:"search",value:p,onChange:a=>G(a.target.value),placeholder:"Search Hungarian articles…",className:"w-full pl-9 pr-3 py-2 text-sm border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"})]})}),e.jsxs("ul",{className:"overflow-y-auto flex-1 min-h-0 divide-y divide-gray-100",children:[c.map(a=>{const h=a.id===me;return e.jsx("li",{children:e.jsxs("button",{type:"button",onClick:()=>_(a.id),className:`w-full text-left px-4 py-3 transition-colors ${h?"bg-blue-50 border-l-2 border-blue-600":"hover:bg-gray-50 border-l-2 border-transparent"}`,children:[e.jsx("div",{className:"text-sm font-medium text-gray-900 line-clamp-2",children:a.title}),e.jsxs("div",{className:"mt-1 flex flex-wrap gap-1.5 text-xs text-gray-500",children:[e.jsx("span",{className:"px-1.5 py-0.5 rounded bg-gray-100",children:a.version}),e.jsx("span",{className:"px-1.5 py-0.5 rounded bg-gray-100",children:a.status}),a.module_name&&e.jsx("span",{className:"px-1.5 py-0.5 rounded bg-gray-100",children:a.module_name})]})]})},a.id)}),c.length===0&&e.jsx("li",{className:"px-4 py-8 text-sm text-gray-500 text-center",children:"No articles match your search."})]})]}),e.jsx("div",{className:"bg-white border border-gray-200 rounded-lg flex flex-col overflow-hidden min-h-[calc(100vh-11rem)] max-h-[calc(100vh-11rem)]",children:Z?e.jsxs("div",{className:"flex flex-col flex-1 min-h-0 overflow-hidden",children:[e.jsxs("div",{className:"p-4 border-b border-gray-200 space-y-2 flex-shrink-0",children:[e.jsx("div",{className:"text-xs font-semibold uppercase tracking-wide text-gray-500",children:"Selected Hungarian"}),e.jsx("div",{className:"text-base font-medium text-gray-900",children:Z.title}),Z.source_url&&e.jsx("a",{href:Z.source_url,target:"_blank",rel:"noreferrer",className:"text-xs text-blue-600 hover:underline break-all",children:Z.source_url})]}),e.jsxs("div",{className:"flex-1 min-h-0 overflow-y-auto",children:[e.jsxs("div",{className:"p-4 border-b border-gray-200 space-y-3",children:[e.jsx("div",{className:"text-xs font-semibold uppercase tracking-wide text-gray-500",children:"Suggested English matches"}),M.length===0?e.jsx("p",{className:"text-sm text-gray-500",children:"No strong suggestions. Search below."}):e.jsx("ul",{className:"space-y-2",children:M.map(a=>{var H;const h=o.find(X=>X.id===a.id),I=te===a.id;return e.jsx("li",{children:e.jsxs("button",{type:"button",onClick:()=>{P(a.id),pe(null)},className:`w-full text-left px-3 py-2 rounded-lg border transition-colors ${I?"border-blue-500 bg-blue-50":"border-gray-200 hover:border-gray-300 hover:bg-gray-50"}`,children:[e.jsx("div",{className:"text-sm font-medium text-gray-900",children:a.title}),e.jsxs("div",{className:"mt-1 flex flex-wrap gap-1.5 text-xs text-gray-500",children:[e.jsx("span",{className:"px-1.5 py-0.5 rounded bg-amber-50 text-amber-800",children:a.reasonLabel}),(h==null?void 0:h.version)&&e.jsx("span",{className:"px-1.5 py-0.5 rounded bg-gray-100",children:h.version}),((H=h==null?void 0:h.module)==null?void 0:H.name)&&e.jsx("span",{className:"px-1.5 py-0.5 rounded bg-gray-100",children:h.module.name})]})]})},a.id)})})]}),e.jsxs("div",{className:"p-4 border-b border-gray-200 space-y-3",children:[e.jsx("div",{className:"text-xs font-semibold uppercase tracking-wide text-gray-500",children:"Search all English articles"}),e.jsxs("div",{className:"relative",children:[e.jsx(ds,{className:"w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-gray-400"}),e.jsx("input",{type:"search",value:E,onChange:a=>ye(a.target.value),placeholder:"Search by title, URL, or id…",className:"w-full pl-9 pr-3 py-2 text-sm border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"})]}),E.trim()&&e.jsx("ul",{className:"max-h-64 overflow-y-auto space-y-1",children:ne.length===0?e.jsx("li",{className:"text-sm text-gray-500 px-1 py-2",children:"No English articles found."}):ne.map(a=>{const h=te===a.id;return e.jsx("li",{children:e.jsx("button",{type:"button",onClick:()=>{P(a.id),pe(null)},className:`w-full text-left px-3 py-2 rounded-lg border text-sm ${h?"border-blue-500 bg-blue-50":"border-gray-200 hover:bg-gray-50"}`,children:a.title})},a.id)})})]}),V&&e.jsxs("div",{className:"p-4 space-y-3 bg-gray-50 border-t border-gray-200",children:[e.jsx("div",{className:"text-xs font-semibold uppercase tracking-wide text-gray-500",children:"Confirm match"}),e.jsxs("p",{className:"text-sm text-gray-700",children:["Link to ",e.jsx("span",{className:"font-medium",children:V.title}),". Hungarian will inherit:"]}),e.jsxs("ul",{className:"text-sm text-gray-600 list-disc pl-5 space-y-1",children:[e.jsxs("li",{children:["Module: ",((D=V.module)==null?void 0:D.name)||"—"]}
26),e.jsxs("li",{children:["Category: ",((w=V.category)==null?void 0:w.name)||"—"]}),e.jsxs("li",{children:["Subcategory: ",((N=V.subcategory)==null?void 0:N.name)||"—"]}),e.jsxs("li",{children:["Tags:"," ",(V.tags||[]).length>0?(V.tags||[]).join(", "):"—"]})]}),e.jsxs("p",{className:"text-xs text-gray-500",children:["Status stays DRAFT. Version stays ",Z.version," (English is"," ",V.version,")."]}),k&&e.jsxs("div",{className:"p-3 bg-amber-50 border border-amber-200 rounded-lg flex items-start",children:[e.jsx(Ke,{className:"w-5 h-5 text-amber-600 mr-2 flex-shrink-0"}),e.jsxs("span",{className:"text-sm text-amber-800",children:["This English article already has a Hungarian pair (“",k.title,"”). Matching will unlink it so it returns to the unmatched list."]})]}),e.jsxs("button",{type:"button",disabled:be,onClick:()=>void n(),className:"inline-flex items-center px-4 py-2 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 disabled:opacity-50",children:[be?e.jsx(ke,{className:"w-4 h-4 mr-2 animate-spin"}):e.jsx(cs,{className:"w-4 h-4 mr-2"}),"Confirm match"]})]})]})]}):e.jsx("div",{className:"p-8 text-sm text-gray-500 text-center",children:"Select a Hungarian article to begin."})})]})]})},sr=()=>{var Us,Bs;const f=Ra(),{profile:v}=Qs(),[b,o]=s.useState([]),[g,W]=s.useState([]),[ee,ce]=s.useState(!0),[S,p]=s.useState(null),[G,E]=s.useState(!1),[ye,me]=s.useState(!1),[ae,te]=s.useState(null),[P,k]=s.useState("preview"),[B,be]=s.useState(!1),[se,le]=s.useState(!1),[pe,q]=s.useState(!1),[Z,V]=s.useState(null),[c,M]=s.useState(new Set),[ne,_]=s.useState(""),[n,D]=s.useState(""),[w,N]=s.useState(""),[a,h]=s.useState(""),[I,H]=s.useState(""),[X,Re]=s.useState(""),[Ne,He]=s.useState(""),[_e,Oe]=s.useState(""),[je,Pe]=s.useState(!1),[qe,Je]=s.useState({current:0,total:0}),[ve,Te]=s.useState(!1),[Fe,i]=s.useState({current:0,total:0}),[u,O]=s.useState(!1),[C,T]=s.useState(!0),[K,l]=s.useState("cards"),[d,J]=s.useState("created_at"),[L,ge]=s.useState("desc"),[Y,re]=s.useState(1),[r,z]=s.useState(25),[R,A]=s.useState(null),[$,ie]=s.useState(!1),[Q,xe]=s.useState(""),[Ie,Le]=s.useState([]),[fe,We]=s.useState(!1),[Ye,Me]=s.useState({current:0,total:0}),[Ee,ft]=s.useState(!1),[jt,xs]=s.useState(null),[aa,wt]=s.useState(!1),[Xe,Nt]=s.useState(""),[vt,ut]=s.useState(""),[$e,us]=s.useState(!1),[ms,Ht]=s.useState({current:0,total:0}),[ra,gs]=s.useState(!1),[nt,hs]=s.useState(null),[la,kt]=s.useState(!1),[St,_t]=s.useState("PUBLISHED"),[Ge,ps]=s.useState(!1),[bs,Ot]=s.useState({current:0,total:0}),[na,ys]=s.useState(!1),[Ct,fs]=s.useState(null),[Ce,qt]=s.useState(""),[ia,At]=s.useState(!1),[Wt,Dt]=s.useState("v3.7"),[tt,js]=s.useState(!1),[ws,Ns]=s.useState({current:0,total:0}),[oa,vs]=s.useState(!1),[Ft,ks]=s.useState(null),[da,It]=s.useState(!1),[we,Mt]=s.useState(null),[Gt,Tt]=s.useState("v3.7"),[Lt,Ss]=s.useState(!1),[ca,Et]=s.useState(!1),[Kt,Rt]=s.useState("v3.7"),[st,_s]=s.useState(!1),[Pt,Cs]=s.useState({current:0,total:0}),[xa,Jt]=s.useState(!1),[it,Yt]=s.useState(null),[at,$t]=s.useState(!1),[Xt,As]=s.useState(!1),Ae=s.useCallback(async()=>{try{ce(!0),p(null);const{data:t,error:x}=await U.from("knowledge_articles").select(`
27          *,
28          category:knowledge_categories!knowledge_articles_category_id_fkey(name),
29          subcategory:knowledge_categories!knowledge_articles_subcategory_id_fkey(name),
30          module:knowledge_modules!module_id(name, slug)
31        `).order("created_at",{ascending:!1});if(x)throw x;const{data:j}=await U.from("profiles").select("email, full_name"),y=new Map((j||[]).map(F=>[F.email,F.full_name])),m=(t||[]).map(F=>({...F,created_by_name:F.created_by_email&&y.get(F.created_by_email)||null,last_modified_by_name:F.last_modified_by_email&&y.get(F.last_modified_by_email)||null}));return o(m),m}catch(t){return p(t.message||"Failed to load articles"),[]}finally{ce(!1)}},[]),mt=s.useCallback(async()=>{try{const{data:t,error:x}=await U.from("knowledge_categories").select("*").order("order_index",{ascending:!0});if(x)throw x;W(t||[])}catch(t){console.error("Failed to load categories:",t)}},[]),Ds=s.useCallback(async()=>{try{const{data:t,error:x}=await U.from("knowledge_modules").select("id, name, slug").eq("is_active",!0).order("order_index",{ascending:!0});if(x)throw x;Le(t||[])}catch(t){console.error("Failed to load modules:",t)}},[]),Fs=s.useCallback(async()=>{try{const{data:t}=await U.from("organization_openai_settings").select("is_configured").maybeSingle();O(!!(t!=null&&t.is_configured))}catch(t){console.error("Failed to check OpenAI config:",t),O(!1)}finally{T(!1)}},[]);s.useEffect(()=>{Ae(),mt(),Ds(),Fs()},[Ae,mt,Ds,Fs]),s.useEffect(()=>{re(1)},[ne,n,w,a,I,X,Ne,_e,Ce]),s.useEffect(()=>{$t(!1)},[n,Ce]),s.useEffect(()=>{const t=x=>{x.target.closest(".dropdown-menu-container")||A(null)};return document.addEventListener("mousedown",t),()=>document.removeEventListener("mousedown",t)},[]);const ua=async()=>{var F,de;if(c.size===0){p("Please select at least one article to index");return}const t=Array.from(c),x=t.length;Pe(!0),p(null),Je({current:0,total:x});const j="https://solxiyfrzksdatdzaxyy.supabase.co/functions/v1/index-knowledge-articles";let y=0,m=0;try{for(let oe=0;oe<t.length;oe++){const he=t[oe],{data:{session:ue}}=await U.auth.getSession();if(!(ue!=null&&ue.access_token))throw new Error("Session expired — please sign in again");let Ze;try{Ze=await fetch(j,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${ue.access_token}`},body:JSON.stringify({articleIds:[he],useContextualHaiku:!0})})}catch(zt){m++,console.error("Index request failed:",he,zt),Je({current:oe+1,total:x});continue}const es=await Ze.json().catch(()=>({}));if(!Ze.ok)m++,console.error("Index failed:",he,es.error||Ze.status);else if((es.failed??0)>0){m++;
31const zt=(de=(F=es.results)==null?void 0:F[0])==null?void 0:de.error;zt&&console.error("Index failed:",he,zt)}else y++;Je({current:oe+1,total:x}),((oe+1)%10===0||oe===t.length-1)&&await Ae()}await Ae(),M(new Set),m>0&&y>0?p(`Indexed ${y} of ${x} articles. ${m} failed — check failed rows and retry.`):m>0?p(`Indexing failed for all ${m} selected articles.`):y>0&&p(null)}catch(oe){p(oe.message||"Failed to index articles")}finally{Pe(!1)}},ma=()=>{f("/knowledge-base-settings")},Is=async t=>{const x=b.find(m=>m.id===t),y=(x==null?void 0:x.openai_file_id)||(x==null?void 0:x.openai_vector_store_file_id)?"Are you sure you want to delete this article? This will permanently remove it from both the database and OpenAI vector store. This action cannot be undone.":"Are you sure you want to delete this article? This action cannot be undone.";if(confirm(y))try{const m="https://solxiyfrzksdatdzaxyy.supabase.co/functions/v1/delete-openai-file",{data:{session:F}}=await U.auth.getSession(),de=await fetch(m,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${F==null?void 0:F.access_token}`},body:JSON.stringify({articleId:t})}),oe=await de.json();if(!de.ok)throw new Error(oe.error||"Failed to delete article");await Ae()}catch(m){p(m.message||"Failed to delete article")}},ga=async()=>{if(c.size===0){p("Please select at least one article to delete");return}const x=b.filter(m=>c.has(m.id)).filter(m=>m.openai_file_id||m.openai_vector_store_file_id).length,j=c.size;let y=`Are you sure you want to delete ${j} article${j!==1?"s":""}?`;if(x>0&&(y+=` ${x} ${x===1?"is":"are"} synced to OpenAI and will be permanently removed from the vector store.`),y+=" This action cannot be undone.",!!confirm(y)){Te(!0),p(null),i({current:0,total:c.size});try{const m="https://solxiyfrzksdatdzaxyy.supabase.co/functions/v1/delete-openai-files-bulk",{data:{session:F}}=await U.auth.getSession(),de=await fetch(m,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${F==null?void 0:F.access_token}`},body:JSON.stringify({articleIds:Array.from(c)})}),oe=await de.json();if(!de.ok)throw new Error(oe.error||"Failed to delete articles");if(await Ae(),M(new Set),i({current:oe.deletedCount,total:c.size}),oe.failedCount>0){const he=oe.errors&&oe.errors.length>0?`
32
33Failed articles: ${oe.errors.map(ue=>ue.error).join(", ")}`:"";p(`Successfully deleted ${oe.deletedCount} article${oe.deletedCount!==1?"s":""}. ${oe.failedCount} failed to delete.${he}`)}}catch(m){p(m.message||"Failed to delete articles")}finally{Te(!1)}}},ha=async()=>{if(!Q){p("Please select a module");return}We(!0),p(null),Me({current:0,total:c.size});try{const t=Ie.find(m=>m.id===Q),x=Array.from(c),j=10;let y=0;for(let m=0;m<x.length;m+=j){const F=x.slice(m,m+j),{error:de}=await U.from("knowledge_articles").update({module_id:Q,category_id:null,subcategory_id:null,updated_at:new Date().toISOString()}).in("id",F);if(de)throw de;y+=F.length,Me({current:y,total:c.size})}ie(!1),xs({count:y,moduleName:(t==null?void 0:t.name)||"Unknown"}),ft(!0)}catch(t){p(t.message||"Failed to assign module")}finally{We(!1),Me({current:0,total:0})}},pa=()=>{ft(!1),xs(null),M(new Set),xe(""),window.location.reload()},ot=()=>{const t=b.filter(m=>c.has(m.id));if(t.length===0)return null;const x=t[0].module_id;if(!t.every(m=>m.module_id===x))return null;const y=Ie.find(m=>m.id===x);return{moduleId:x,moduleName:(y==null?void 0:y.name)||"Unknown"}},Ms=()=>{const t=ot();return t?g.filter(x=>x.module_id===t.moduleId&&x.parent_id===null):[]},ba=()=>Xe?g.filter(t=>t.parent_id===Xe):[],ya=async()=>{if(!ot()){p("Selected articles must be from the same module");return}if(!Xe){p("Please select a category");return}us(!0),p(null),Ht({current:0,total:c.size});try{const x=g.find(de=>de.id===Xe),j=vt?g.find(de=>de.id===vt):null,y=Array.from(c),m=10;let F=0;for(let de=0;de<y.length;de+=m){const oe=y.slice(de,de+m),{error:he}=await U.from("knowledge_articles").update({category_id:Xe,subcategory_id:vt||null,updated_at:new Date().toISOString()}).in("id",oe);if(he)throw he;F+=oe.length,Ht({current:F,total:c.size})}
33wt(!1),hs({count:F,categoryName:(x==null?void 0:x.name)||"Unknown",subcategoryName:j==null?void 0:j.name}),gs(!0)}catch(x){p(x.message||"Failed to assign category")}finally{us(!1),Ht({current:0,total:0})}},fa=()=>{gs(!1),hs(null),M(new Set),Nt(""),ut(""),window.location.reload()},ja=async()=>{if(!St){p("Please select a status");return}ps(!0),p(null),Ot({current:0,total:c.size});try{const t=Array.from(c),x=10;let j=0;for(let y=0;y<t.length;y+=x){const m=t.slice(y,y+x),{error:F}=await U.from("knowledge_articles").update({status:St,updated_at:new Date().toISOString()}).in("id",m);if(F)throw F;j+=m.length,Ot({current:j,total:c.size})}kt(!1),fs({count:j,status:St}),ys(!0)}catch(t){p(t.message||"Failed to change status")}finally{ps(!1),Ot({current:0,total:0})}},wa=()=>{ys(!1),fs(null),M(new Set),_t("PUBLISHED"),window.location.reload()},Na=async()=>{js(!0),p(null),Ns({current:0,total:c.size});try{const t=Array.from(c),x=10;let j=0;for(let y=0;y<t.length;y+=x){const m=t.slice(y,y+x),{error:F}=await U.from("knowledge_articles").update({version:Wt,updated_at:new Date().toISOString()}).in("id",m);if(F)throw F;j+=m.length,Ns({current:j,total:c.size})}At(!1),ks({count:c.size,version:Wt}),vs(!0),await Ae()}catch(t){p(t.message||"Failed to assign version")}finally{js(!1)}},va=()=>{vs(!1),ks(null),M(new Set),Dt("v3.7")},ka=t=>{Mt(t),Tt("v3.7"),It(!0),A(null)},Sa=async()=>{if(we){Ss(!0),p(null);try{const{data:{user:t}}=await U.auth.getUser(),x={title:we.title,content:we.content,content_html:we.content_html||null,image_urls:we.image_urls||[],subtitle:we.subtitle||null,source_type:"raw",source_url:null,module_id:we.module_id,category_id:we.category_id||null,subcategory_id:we.subcategory_id||null,tags:we.tags||[],language:we.language,version:Gt,status:"DRAFT",user_id:(t==null?void 0:t.id)||we.user_id,created_by_email:(t==null?void 0:t.email)||null},{error:j}=await U.from("knowledge_articles").insert(x);if(j)throw j;It(!1),Yt({count:1,version:Gt}),Jt(!0),await Ae()}catch(t){p(t.message||"Failed to duplicate article")}finally{Ss(!1)}}},_a=async()=>{_s(!0),p(null),Cs({current:0,total:c.size});try{const{data:{user:t}}=await U.auth.getUser(),x=Array.from(c),j=b.filter(F=>x.includes(F.id)),y=10;let m=0;for(let F=0;F<j.length;F+=y){const de=j.slice(F,F+y),oe=de.map(ue=>({title:ue.title,content:ue.content,content_html:ue.content_html||null,image_urls:ue.image_urls||[],subtitle:ue.subtitle||null,source_type:"raw",source_url:null,module_id:ue.module_id,category_id:ue.category_id||null,subcategory_id:ue.subcategory_id||null,tags:ue.tags||[],language:ue.language,version:Kt,status:"DRAFT",user_id:(t==null?void 0:t.id)||ue.user_id,created_by_email:(t==null?void 0:t.email)||null})),{error:he}=await U.from("knowledge_articles").insert(oe);if(he)throw he;m+=de.length,Cs({current:m,total:c.size})}Et(!1),Yt({count:c.size,version:Kt}),Jt(!0),await Ae()}catch(t){p(t.message||"Failed to duplicate articles")}finally{_s(!1)}},Ca=()=>{Jt(!1),Yt(null),Mt(null),M(new Set),Rt("v3.7")},Ts=async(t,x)=>{if(Xt)return;const j=Be.slice(),y=j.findIndex(he=>he.id===t);if(y<0)return;const m=x==="up"?y-1:y+1;if(m<0||m>=j.length)return;const F=j.map((he,ue)=>({...he,order_index:ue})),de=F[y].order_index;F[y]={...F[y],order_index:F[m].order_index},F[m]={...F[m],order_index:de};const oe=b.map(he=>{const ue=F.find(Ze=>Ze.id===he.id);return ue?{...he,order_index:ue.order_index}:he});o(oe),As(!0);try{const he=[{id:F[y].id,order_index:F[y].order_index},{id:F[m].id,order_index:F[m].order_index}];for(const ue of he){const{error:Ze}=await U.from("knowledge_articles").update({order_index:ue.order_index}).eq("id",ue.id);if(Ze)throw Ze}}catch(he){p(he.message||"Failed to save order"),await Ae()}finally{As(!1)}},Ls=t=>{te(t),k("edit"),me(!0)},Es=t=>{te(t),k("preview"),me(!0)},Rs=t=>{const x=new Set(c);x.has(t)?x.delete(t):x.add(t),M(x)},Aa=()=>{c.size===Be.length?M(new Set):M(new Set(Be.map(t=>t.id)))},Ps=t=>{const x=t.kb_index_status;if(x)switch(x){case"indexed":return{label:"Indexed",icon:Se,color:"text-green-600",bg:"bg-green-50"};case"indexing":return{label:"Indexing",icon:ke,color:"text-blue-600",bg:"bg-blue-50"};case"stale":return{label:"Stale",icon:ke,color:"text-orange-600",bg:"bg-orange-50"};case"failed":return{label:"Index failed",icon:et,color:"text-red-600",bg:"bg-red-50"};default:return{label:"Not indexed",icon:et,color:"text-gray-500",bg:"bg-gray-50"}}switch(t.openai_sync_status){case"synced":return{label:"Synced",icon:Se,color:"text-green-600",bg:"bg-green-50"};case"uploading":return{label:"Uploading",icon:ke,color:"text-blue-600",bg:"bg-blue-50"};case"needs_resync":return{label:"Needs Resync",icon:ke,color:"text-orange-600",bg:"bg-orange-50"};case"failed":return{label:"Failed",icon:et,color:"text-red-600",bg:"bg-red-50"};case"not_synced":default:return{label:"Not Synced",icon:Zs,color:"text-gray-500",bg:"bg-gray-50"}}},Qe=t=>{d===t?ge(L==="asc"?"desc":"asc"):(J(t),ge("asc"))},$s=g.filter(t=>t.parent_id===null),Qt=g.filter(t=>t.parent_id===n),Be=b.filter(t=>{if(ne){const x=ne.toLowerCase(),j=t.title.toLowerCase().includes(x),y=t.content.toLowerCase().includes(x),m=t.tags.some(F=>F.toLowerCase().includes(x));if(!j&&!y&&!m)return!1}if(n)if(w){if(t.subcategory_id!==w)return!1}else{const x=g.filter(j=>j.parent_id===n).map(j=>j.id);if(t.category_id!==n&&!x.includes(t.subcategory_id||""))return!1}return!(_e&&t.module_id!==_e||a==="synced"&&t.kb_index_status!=="indexed"&&t.openai_sync_status!=="synced"||a==="not_synced"&&t.kb_index_status!=="not_indexed"&&t.openai_sync_status!=="not_synced"||a==="failed"&&t.kb_index_status!=="failed"&&t.openai_sync_status!=="failed"||a==="needs_resync"&&t.kb_index_status!=="stale"&&t.openai_sync_status!=="needs_resync"||I==="archived"&&!t.archived||I==="active"&&t.archived||X==="PUBLISHED"&&t.status!=="PUBLISHED"||X==="DRAFT"&&t.status!=="DRAFT"||Ne&&t.language!==Ne||Ce&&t.version!==Ce)}).sort((t,x)=>{if(at)return t.order_index-x.order_index;const j=t[d],y=x[d];if(j==null)return 1;if(y==null)return-1;let m=0;return typeof j=="string"&&typeof y=="string"?m=j.localeCompare(y):Array.isArray(j)&&Array.isArray(y)?m=j.length-y.length:m=j>y?1:-1,L==="asc"?m:-m}),ze=Math.ceil(Be.length/r),Zt=(Y-1)*r,zs=Zt+r,gt=Be.slice(Zt,zs),Da=t=>{const x=Ps(t),j=c.has(t.id);return e.jsxs("div",{className:`bg-white rounded-lg border-2 transition-all ${j?"border-blue-500 shadow-md":"border-gray-200 hover:border-gray-300"} p-4`,children:[e.jsxs("div",{className:"flex items-start justify-between mb-3",children:[e.jsxs("div",{className:"flex items-start space-x-3 flex-1 min-w-0",children:[e.jsx("input",{type:"checkbox",checked:j,onChange:()=>
33Rs(t.id),className:"mt-1 rounded border-gray-300 text-blue-600 focus:ring-blue-500"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx("h3",{className:"text-sm font-semibold text-gray-900 truncate",children:t.title}),t.status==="DRAFT"&&e.jsx("span",{className:"inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-yellow-100 text-yellow-800 border border-yellow-200 flex-shrink-0",children:"DRAFT"})]}),t.subtitle&&e.jsx("p",{className:"text-xs font-medium text-gray-700 mb-1 line-clamp-1",children:t.subtitle}),e.jsxs("p",{className:"text-xs text-gray-600 line-clamp-2",children:[t.content.substring(0,150),"..."]})]})]}),e.jsxs("div",{className:`px-2 py-1 rounded-full ${x.bg} flex items-center space-x-1 ml-2 flex-shrink-0`,children:[e.jsx(x.icon,{className:`w-3 h-3 ${x.color}`}),e.jsx("span",{className:`text-xs font-medium ${x.color}`,children:x.label})]})]}),e.jsxs("div",{className:"flex flex-wrap gap-2 mb-3",children:[t.source_type==="url"?e.jsxs("span",{className:"inline-flex items-center px-2 py-1 text-xs bg-blue-50 text-blue-700 rounded",children:[e.jsx(cs,{className:"w-3 h-3 mr-1"}),"URL"]}):t.source_type==="csv"?e.jsxs("span",{className:"inline-flex items-center px-2 py-1 text-xs bg-green-50 text-green-700 rounded",children:[e.jsx(yt,{className:"w-3 h-3 mr-1"}),"CSV"]}):e.jsxs("span",{className:"inline-flex items-center px-2 py-1 text-xs bg-gray-100 text-gray-700 rounded",children:[e.jsx(dt,{className:"w-3 h-3 mr-1"}),"Raw"]}),e.jsxs("span",{className:"inline-flex items-center px-2 py-1 text-xs bg-indigo-50 text-indigo-700 rounded font-medium",children:[ct(t.language)," ",bt(t.language)]}),t.category&&e.jsx("span",{className:"px-2 py-1 text-xs bg-purple-50 text-purple-700 rounded",children:t.category.name}),t.subcategory&&e.jsx("span",{className:"px-2 py-1 text-xs bg-purple-50 text-purple-700 rounded",children:t.subcategory.name})]}),t.tags.length>0&&e.jsxs("div",{className:"flex flex-wrap gap-1 mb-3",children:[t.tags.slice(0,3).map((y,m)=>e.jsxs("span",{className:"inline-flex items-center px-2 py-0.5 text-xs bg-gray-100 text-gray-600 rounded",children:[e.jsx(ht,{className:"w-3 h-3 mr-1"}),y]},m)),t.tags.length>3&&e.jsxs("span",{className:"px-2 py-0.5 text-xs text-gray-500",children:["+",t.tags.length-3," more"]})]}),e.jsxs("div",{className:"flex items-center justify-between text-xs text-gray-500 mb-3",children:[e.jsxs("div",{className:"flex flex-col space-y-1",children:[e.jsxs("span",{className:"flex items-center",children:[e.jsx(Ws,{className:"w-3 h-3 mr-1"}),"Created: ",new Date(t.created_at).toLocaleDateString()]}),t.last_modified_date&&e.jsxs("span",{className:"flex items-center",children:[e.jsx(Ws,{className:"w-3 h-3 mr-1"}),"Modified: ",new Date(t.last_modified_date).toLocaleDateString()]}),(t.created_by_name||t.created_by_email)&&e.jsxs("span",{className:"flex items-center text-blue-600",children:["Added by: ",t.created_by_name||t.created_by_email]}),(t.last_modified_by_name||t.last_modified_by_email)&&e.jsxs("span",{className:"flex items-center text-orange-600",children:["Modified by: ",t.last_modified_by_name||t.last_modified_by_email]})]}),t.last_synced_at&&t.openai_sync_status==="synced"&&e.jsxs("span",{className:"text-right",children:["Synced ",new Date(t.last_synced_at).toLocaleDateString()]})]}),t.openai_sync_status==="failed"&&t.openai_sync_error&&e.jsxs("div",{className:"mb-3 p-2 bg-red-50 border border-red-200 rounded text-xs text-red-700",children:[e.jsx(et,{className:"w-3 h-3 inline mr-1"}),t.openai_sync_error]}),t.openai_file_id&&t.openai_sync_status==="synced"&&e.jsxs("div",{className:"mb-3 p-2 bg-blue-50 rounded",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-xs text-blue-700 font-medium",children:"OpenAI File ID:"}),e.jsx("button",{onClick:()=>{navigator.clipboard.writeText(t.openai_file_id||""),alert("File ID copied to clipboard!")},className:"text-xs text-blue-600 hover:text-blue-700 underline",children:"Copy"})]}),e.jsx("p",{className:"text-xs text-blue-600 font-mono mt-1 break-all",children:t.openai_file_id})]}),e.jsxs("div",{className:"flex justify-end dropdown-menu-container relative",children:[e.jsx("button",{onClick:()=>A(R===t.id?null:t.id),className:"p-2 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded-lg transition-colors",title:"Actions",children:e.jsx(qs,{className:"w-5 h-5"})}),R===t.id&&e.jsx("div",{className:"absolute right-0 bottom-full mb-1 w-48 bg-white rounded-lg shadow-lg border border-gray-200 z-50",children:e.jsxs("div",{className:"py-1",children:[e.jsxs("button",{onClick:()=>{Es(t),A(null)},className:"flex items-center w-full px-4 py-2 text-sm text-gray-700 hover:bg-gray-50 transition-colors",children:[e.jsx(is,{className:"w-4 h-4 mr-3 text-blue-600"}),"Preview"]}),e.jsxs("button",{onClick:()=>{Ls(t),A(null)},className:"flex items-center w-full px-4 py-2 text-sm text-gray-700 hover:bg-gray-50 transition-colors",children:[e.jsx(os,{className:"w-4 h-4 mr-3 text-blue-600"}),"Edit"]}),e.jsxs("button",{onClick:()=>{V({id:t.id,title:t.title}),q(!0),A(null)},className:"flex items-center w-full px-4 py-2 text-sm text-gray-700 hover:bg-gray-50 transition-colors",children:[e.jsx(lt,{className:"w-4 h-4 mr-3 text-purple-600"}),"Translate"]}),e.jsx("hr",{className:"my-1 border-gray-200"}),e.jsxs("button",{onClick:()=>{Is(t.id),A(null)},className:"flex items-center w-full px-4 py-2 text-sm text-red-600 hover:bg-red-50 transition-colors",children:[e.jsx(as,{className:"w-4 h-4 mr-3"}),"Delete"]})]})})]})]},t.id)},Fa=s.useMemo(()=>se?e.jsx("button",{onClick:()=>le(!1),className:rs,children:"Back to articles"}):e.jsxs(e.Fragment,{children:[e.jsxs("button",{onClick:()=>le(!0),className:rs,children:[e.jsx(lt,{className:"w-5 h-5 mr-2"}),"Match Hungarian articles"]}),e.jsxs("button",{onClick:()=>be(!0),className:rs,children:[e.jsx(yt,{className:"w-5 h-5 mr-2"}),"Import from CSV"]}),e.jsxs("button",{onClick:()=>E(!0),className:$a,children:[e.jsx(ss,{className:"w-5 h-5 mr-2"}),"Add Article"]})]}),[se]),Ia=s.useMemo(()=>se?[{id:"back-articles",label:"Back to articles",icon:ea,onClick:()=>le(!1)}]:[{id:"match-hu",label:"Match Hungarian articles",icon:lt,onClick:()=>le(!0)},{id:"import-csv",label:"Import from CSV",icon:yt,onClick:()=>be(!0)},{id:"add-article",label:"Add article",icon:ss,onClick:()=>E(!0),variant:"primary"}],[se]);return Pa({title:se?"Match Hungarian articles":"Knowledge Base",actions:Fa,mobileActions:Ia}),se?e.jsx("div",{className:"p-4 sm:p-6 space-y-6 max-w-full bg-gray-50",children:e.jsx(tr,{onBack:()=>{le(!1),Ae()}})}):ee?e.jsx("div",{className:"flex items-center justify-center h-full",children:e.jsxs("div",{className:"flex flex-col items-center space-y-4",children:[e.jsx("div",{className:"animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"}),e.jsx("span",{className:"text-gray-600",children:"Loading knowledge base..."})]})}):e.jsxs("div",{className:"p-4 sm:p-6 space-y-6 max-w-full bg-gray-50",children:[e.jsxs("div",{className:"mb-6",children:[e.jsx("p",{className:"text-gray-600",children:"Manage articles for AI-powered chat support"}),S&&e.jsxs("div",{className:"mb-4 p-3 bg-red-50 border border-red-200 rounded-lg flex items-start",children:[e.jsx(et,{className:"w-5 h-5 text-red-600 mr-2 flex-shrink-0"}),e.jsx("span",{className:"text-sm text-red-600",children:S})]}),e.jsx("div",{className:"bg-white rounded-lg border border-gray-200 p-4 mb-4",children:e.jsxs("div",{className:"flex flex-col space-y-4",children:[e.jsx("div",{className:"flex flex-col lg:flex-row lg:items-center lg:justify-between space-y-4 lg:space-y-0 lg:space-x-4",children:e.jsx("div",{className:"flex-1",children:e.jsxs("div",{className:"relative",children:[e.jsx(ds,{className:"w-5 h-5 absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400"}),e.jsx("input",{type:"text",placeholder:"Search articles by title, content, or tags...",value:ne,onChange:t=>_(t.target.value),className:"w-full pl-10 pr-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"})]})})}),e.jsxs("div",{className:"flex flex-col sm:flex-row sm:flex-wrap gap-2",children:[e.jsxs("select",{value:n,onChange:t=>{D(t.target.value),N("")},className:"px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm",children:[e.jsx("option",{value:"",children:"All Categories"}),$s.map(t=>e.jsx("option",{value:t.id,children:t.name},t.id))]}),e.jsxs("select",{value:w,onChange:t=>N(t.target.value),disabled:!n||Qt.length===0,className:"px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm disabled:opacity-50 disabled:cursor-not-allowed",children:[e.jsx("option",{value:"",children:"All Subcategories"}),Qt.map(t=>e.jsx("option",{value:t.id,children:t.name},t.id))]}),e.jsxs("select",{value:_e,onChange:t=>Oe(t.target.value),className:"px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm",children:[e.jsx("option",{value:"",children:"All Modules"}),Ie.map(t=>e.jsx("option",{value:t.id,children:t.name},t.id))]}),e.jsxs("select",{value:a,onChange:t=>h(t.target.value),className:"px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm",children:[e.jsx("option",{value:"",children:"All Sync Status"}),e.jsx("option",{value:"synced",children:"Synced to OpenAI"}),e.jsx("option",{value:"not_synced",children:"Not Synced"}),e.jsx("option",{value:"needs_resync",children:"Needs Resync"}),e.jsx("option",{value:"failed",children:"Failed"})]}),e.jsxs("select",{value:X,onChange:t=>Re(t.target.value),className:"px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm",children:[e.jsx("option",{value:"",children:"All Status"}),e.jsx("option",{value:"PUBLISHED",children:"Published"}),e.jsx("option",{value:"DRAFT",children:"Draft"})]}),e.jsxs("select",{value:I,onChange:t=>H(t.target.value),className:"px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm",children:[e.jsx("option",{value:"",children:"All Articles"}),e.jsx("option",{value:"active",children:"Active Only"}),e.jsx("option",{value:"archived",children:"Archived Only"})]}),e.jsxs("select",{value:Ne,onChange:t=>He(t.target.value),className:"px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm",children:[e.jsx("option",{value:"",children:"All Languages"}),pt.map(t=>e.jsxs("option",{value:t.code,children:[t.flag," ",t.nativeName]},t.code))]}),e.jsxs("select",{value:Ce,onChange:t=>qt(t.target.value),className:"px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm",children:[e.jsx("option",{value:"",children:"All Versions"}),e.jsx("option",{value:"v3.7",children:"v3.7"}),e.jsx("option",{value:"v4.0",children:"v4.0"})]}),e.jsxs("button",{onClick:()=>{!n||!Ce||$t(t=>!t)},disabled:!n||!Ce,title:!n||!Ce?"Select a category and version to enable reordering":at?"Exit reorder mode":"Enter reorder mode",className:`px-3 py-2 text-sm rounded-lg transition-colors flex items-center border ${at?"bg-blue-600 text-white border-blue-600 hover:bg-blue-700":n&&Ce?"border-gray-300 text-gray-700 hover:bg-gray-100":"border-gray-200 text-gray-400 cursor-not-allowed"}`,children:[e.jsx(Ue,{className:"w-3 h-3 mr-1"}),e.jsx(Ve,{className:"w-3 h-3 mr-1.5"}),at?"Exit Reorder":"Reorder"]}),(ne||n||w||a||I||X||Ne||_e||Ce)&&e.jsxs("button",{onClick:()=>{_(""),D(""),N(""),h(""),H(""),Re(""),He(""),Oe(""),qt(""),$t(!1)},className:"px-3 py-2 text-sm text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded-lg transition-colors flex items-center",children:[e.jsx(De,{className:"w-4 h-4 mr-1"}),"Clear Filters"]})]}),(n||w||a||I||Ne||Ce)&&e.jsxs("div",{className:"flex flex-wrap gap-2 items-center",children:[e.jsx("span",{className:"text-sm text-gray-600 font-medium",children:"Active Filters:"}),n&&e.jsxs("span",{className:"inline-flex items-center px-2 py-1 bg-blue-100 text-blue-700 rounded-md text-xs",children:["Category: ",(Us=$s.find(t=>
33t.id===n))==null?void 0:Us.name,e.jsx("button",{onClick:()=>{D(""),N("")},className:"ml-1 hover:text-blue-900",children:e.jsx(De,{className:"w-3 h-3"})})]}),w&&e.jsxs("span",{className:"inline-flex items-center px-2 py-1 bg-blue-100 text-blue-700 rounded-md text-xs",children:["Subcategory: ",(Bs=Qt.find(t=>t.id===w))==null?void 0:Bs.name,e.jsx("button",{onClick:()=>N(""),className:"ml-1 hover:text-blue-900",children:e.jsx(De,{className:"w-3 h-3"})})]}),a&&e.jsxs("span",{className:"inline-flex items-center px-2 py-1 bg-green-100 text-green-700 rounded-md text-xs",children:["Status: ",a==="synced"?"Synced to OpenAI":a==="not_synced"?"Not Synced":a==="needs_resync"?"Needs Resync":"Failed",e.jsx("button",{onClick:()=>h(""),className:"ml-1 hover:text-green-900",children:e.jsx(De,{className:"w-3 h-3"})})]}),I&&e.jsxs("span",{className:"inline-flex items-center px-2 py-1 bg-amber-100 text-amber-700 rounded-md text-xs",children:[I==="archived"?"Archived Only":"Active Only",e.jsx("button",{onClick:()=>H(""),className:"ml-1 hover:text-amber-900",children:e.jsx(De,{className:"w-3 h-3"})})]}),Ne&&e.jsxs("span",{className:"inline-flex items-center px-2 py-1 bg-purple-100 text-purple-700 rounded-md text-xs",children:["Language: ",ct(Ne)," ",bt(Ne),e.jsx("button",{onClick:()=>He(""),className:"ml-1 hover:text-purple-900",children:e.jsx(De,{className:"w-3 h-3"})})]}),Ce&&e.jsxs("span",{className:"inline-flex items-center px-2 py-1 bg-teal-100 text-teal-700 rounded-md text-xs",children:["Version: ",Ce,e.jsx("button",{onClick:()=>qt(""),className:"ml-1 hover:text-teal-900",children:e.jsx(De,{className:"w-3 h-3"})})]})]})]})}),at&&e.jsxs("div",{className:"mb-4 p-3 bg-blue-50 border border-blue-200 rounded-lg flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(Ue,{className:"w-4 h-4 text-blue-600"}),e.jsx("span",{className:"text-sm font-medium text-blue-800",children:"Reorder mode active — use the arrows in each row to adjust article order within this category + version."})]}),e.jsx("button",{onClick:()=>$t(!1),className:"text-sm text-blue-600 hover:text-blue-800 underline",children:"Done"})]}),!u&&!C&&e.jsx("div",{className:"mb-4 p-4 bg-yellow-50 border border-yellow-200 rounded-lg",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx(et,{className:"w-5 h-5 text-yellow-600 mr-3 flex-shrink-0"}),e.jsxs("div",{className:"flex-1",children:[e.jsx("h4",{className:"text-sm font-medium text-yellow-900 mb-1",children:"Organization OpenAI Configuration Required"}),(v==null?void 0:v.role)==="admin"?e.jsxs(e.Fragment,{children:[e.jsx("p",{className:"text-sm text-yellow-800 mb-2",children:"To sync articles to OpenAI, you need to configure your organization's API credentials first. These settings will be shared across all users in your organization."}),e.jsx("button",{onClick:ma,className:"btn-primary text-sm",children:"Configure OpenAI Settings"})]}):e.jsx("p",{className:"text-sm text-yellow-800",children:"Your organization has not configured OpenAI integration yet. Please contact an administrator to set up the API credentials. Once configured, all users will be able to sync articles and use AI-powered features."})]})]})}),c.size>0&&e.jsx("div",{className:"bg-blue-50 border border-blue-200 rounded-lg p-4 mb-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center space-x-4",children:[e.jsxs("span",{className:"text-sm font-medium text-blue-900",children:[c.size," article",c.size!==1?"s":""," selected"]}),e.jsx("button",{onClick:()=>M(new Set),className:"text-sm text-blue-600 hover:text-blue-700 underline",children:"Clear selection"})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsxs("button",{onClick:()=>ie(!0),disabled:fe||ve||je||$e,className:"btn-secondary flex items-center",children:[e.jsx(Os,{className:"w-4 h-4 mr-2"}),"Assign Module"]}),e.jsxs("button",{onClick:()=>wt(!0),disabled:$e||ve||je||fe||Ge,className:"btn-secondary flex items-center",children:[e.jsx(ht,{className:"w-4 h-4 mr-2"}),"Assign Category"]}),e.jsxs("button",{onClick:()=>kt(!0),disabled:Ge||ve||je||fe||$e||tt,className:"btn-secondary flex items-center",children:[e.jsx(dt,{className:"w-4 h-4 mr-2"}),"Change Status"]}),e.jsxs("button",{onClick:()=>At(!0),disabled:tt||ve||je||fe||$e||Ge||st,className:"btn-secondary flex items-center",children:[e.jsx(ht,{className:"w-4 h-4 mr-2"}),"Change Version"]}),e.jsx("button",{onClick:()=>Et(!0),disabled:st||ve||je||fe||$e||Ge||tt,className:"btn-secondary flex items-center",children:st?e.jsxs(e.Fragment,{children:[e.jsx(ke,{className:"w-4 h-4 mr-2 animate-spin"}),"Duplicating ",Pt.current,"/",Pt.total]}):e.jsxs(e.Fragment,{children:[e.jsx(Ut,{className:"w-4 h-4 mr-2"}),"Duplicate"]})}),e.jsx("button",{onClick:ga,disabled:ve||je||fe||$e||Ge,className:"btn-secondary text-red-600 hover:bg-red-50 flex items-center",children:ve?e.jsxs(e.Fragment,{children:[e.jsx(ke,{className:"w-4 h-4 mr-2 animate-spin"}),"Deleting ",Fe.current,"/",Fe.total]}):e.jsxs(e.Fragment,{children:[e.jsx(as,{className:"w-4 h-4 mr-2"}),"Delete Selected"]})}),e.jsx("button",{onClick:ua,disabled:je||ve||fe||$e||Ge,className:"btn-primary flex items-center",children:je?e.jsxs(e.Fragment,{children:[e.jsx(ke,{className:"w-4 h-4 mr-2 animate-spin"}),"Indexing ",qe.current,"/",qe.total]}):e.jsxs(e.Fragment,{children:[e.jsx(ta,{className:"w-4 h-4 mr-2"}),"Index for AI"]})})]})]})})]}),e.jsxs("div",{className:"mb-4 flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center space-x-4",children:[e.jsxs("span",{className:"text-sm text-gray-600",children:["Showing ",Zt+1,"-",Math.min(zs,Be.length)," of ",Be.length," article",Be.length!==1?"s":""]}),e.jsxs("select",{value:r,onChange:t=>{z(Number(t.target.value)),re(1)},className:"px-2 py-1 text-sm border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500",children:[e.jsx("option",{value:"10",children:"10 per page"}),e.jsx("option",{value:"25",children:"25 per page"}),e.jsx("option",{value:"50",children:"50 per page"}),e.jsx("option",{value:"100",children:"100 per page"})]})]}),e.jsxs("div",{className:"lg:hidden flex bg-gray-100 rounded-lg p-1",children:[e.jsx("button",{onClick:()=>l("cards"),className:`px-3 py-2 text-sm font-medium rounded-md transition-colors ${K==="cards"?"bg-white text-blue-600 shadow-sm":"text-gray-600 hover:text-gray-900"}`,children:"Cards"}),e.jsx("button",{onClick:()=>l("table"),className:`px-3 py-2 text-sm font-medium rounded-md transition-colors ${K==="table"?"bg-white text-blue-600 shadow-sm":"text-gray-600 hover:text-gray-900"}`,children:"Table"})]})]}),Be.length===0?e.jsxs("div",{className:"bg-white rounded-lg border border-gray-200 p-12 text-center",children:[e.jsx(dt,{className:"w-12 h-12 text-gray-400 mx-auto mb-4"}),e.jsx("h3",{className:"text-lg font-medium text-gray-900 mb-2",children:"No articles found"}),e.jsx("p",{className:"text-gray-600 mb-4",children:ne||n||w||a||I||X||Ne||_e||Ce?"Try adjusting your filters":"Get started by adding your first knowledge article"}),!ne&&!n&&!w&&!a&&!I&&!X&&!Ne&&!_e&&!Ce&&e.jsxs("button",{onClick:()=>E(!0),className:"btn-primary",children:[e.jsx(ss,{className:"w-5 h-5 mr-2 inline"}),"Add Your First Article"]})]}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:`lg:hidden ${K==="table"?"hidden":"grid grid-cols-1 gap-4"}`,children:gt.map(Da)}),e.jsx("div",{className:`${K==="cards"?"hidden lg:block":"block"} bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 shadow-sm overflow-hidden`,children:e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"min-w-full divide-y divide-gray-200 dark:divide-gray-700",children:[e.jsx("thead",{className:"bg-gray-50 dark:bg-gray-900",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-4 py-3 text-left w-12",children:e.jsx("input",{type:"checkbox",checked:c.size===Be.length&&Be.length>0,onChange:Aa,className:"rounded border-gray-300 text-blue-600 focus:ring-blue-500"})}),e.jsx("th",{className:"px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700",onClick:()=>Qe("title"),children:e.jsxs("div",{className:"flex items-center space-x-1",children:[e.jsx("span",{children:"Title"}),d==="title"&&(L==="asc"?e.jsx(Ue,{className:"w-4 h-4"}):e.jsx(Ve,{className:"w-4 h-4"}))]})}),e.jsx("th",{className:"px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700 hidden md:table-cell",onClick:()=>Qe("language"),children:e.jsxs("div",{className:"flex items-center space-x-1",children:[e.jsx("span",{children:"Language"}),d==="language"&&(L==="asc"?e.jsx(Ue,{className:"w-4 h-4"}):e.jsx(Ve,{className:"w-4 h-4"}))]})}),e.jsx("th",{className:"px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700 hidden md:table-cell",onClick:()=>Qe("version"),children:e.jsxs("div",{className:"flex items-center space-x-1",children:[e.jsx("span",{children:"Version"}),d==="version"&&(L==="asc"?e.jsx(Ue,{className:"w-4 h-4"}):e.jsx(Ve,{className:"w-4 h-4"}))]})}),e.jsx("th",{className:"px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider hidden md:table-cell",children:"Module"}),e.jsx("th",{className:"px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider hidden lg:table-cell",children:"Category"}),e.jsx("th",{className:"px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700 hidden md:table-cell",onClick:()=>Qe("status"),children:e.jsxs("div",{className:"flex items-center space-x-1",children:[e.jsx("span",{children:"Article Status"}),d==="status"&&(L==="asc"?e.jsx(Ue,{className:"w-4 h-4"}):e.jsx(Ve,{className:"w-4 h-4"}))]})}),e.jsx("th",{className:"px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700",onClick:()=>Qe("openai_sync_status"),children:e.jsxs("div",{className:"flex items-center space-x-1",children:[e.jsx("span",{children:"Sync Status"}),d==="openai_sync_status"&&(L==="asc"?e.jsx(Ue,{className:"w-4 h-4"}):e.jsx(Ve,{className:"w-4 h-4"}))]})}),e.jsx("th",{className:"px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider hidden xl:table-cell",children:"Tags"}),e.jsx("th",{className:"px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700 hidden md:table-cell",onClick:()=>Qe("created_at"),children:e.jsxs("div",{className:"flex items-center space-x-1",children:[e.jsx("span",{children:"Created"}),d==="created_at"&&(L==="asc"?e.jsx(Ue,{className:"w-4 h-4"}):e.jsx(Ve,{className:"w-4 h-4"}))]})}),e.jsx("th",{className:"px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700 hidden lg:table-cell",onClick:()=>Qe("last_modified_date"),children:e.jsxs("div",{className:"flex items-center space-x-1",children:[e.jsx("span",{children:"Last Modified"}),d==="last_modified_date"&&(L==="asc"?e.jsx(Ue,{className:"w-4 h-4"}):e.jsx(Ve,{className:"w-4 h-4"}))]})}),e.jsx("th",{className:"px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700 hidden xl:table-cell",onClick:()=>Qe("created_by_name"),children:e.jsxs("div",{className:"flex items-center space-x-1",children:[e.jsx("span",{children:"Added By"}),d==="created_by_name"&&(L==="asc"?e.jsx(Ue,{className:"w-4 h-4"}):e.jsx(Ve,{className:"w-4 h-4"}))]})}),e.jsx("th",{className:"px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700 hidden xl:table-cell",onClick:()=>Qe("last_modified_by_name"),children:e.jsxs("div",{className:"flex items-center space-x-1",children:[e.jsx("span",{children:"Modified By"}),d==="last_modified_by_name"&&(L==="asc"?e.jsx(Ue,{className:"w-4 h-4"}):e.jsx(Ve,{className:"w-4 h-4"}))]})}),at&&e.jsx("th",{className:"px-4 py-3 text-center text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider w-20",children:"Order"}),e.jsx("th",{className:"px-4 py-3 text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider",children:"Actions"})]})}),e.jsx("tbody",{className:"bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700",children:gt.map(t=>{var y,m;const x=Ps(t),j=c.has(t.id);return e.jsxs("tr",{className:`hover:bg-gray-50 transition-colors ${j?"bg-blue-50":""}`,children:[e.jsx("td",{className:"px-4 py-4",children:e.jsx("input",{type:"checkbox",checked:j,onChange:()=>
33Rs(t.id),className:"rounded border-gray-300 text-blue-600 focus:ring-blue-500"})}),e.jsx("td",{className:"px-4 py-4",children:e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"text-sm font-medium text-gray-900 truncate max-w-xs",title:t.title,children:t.title}),t.subtitle&&e.jsx("span",{className:"text-xs text-gray-600 truncate max-w-xs",title:t.subtitle,children:t.subtitle})]})}),e.jsx("td",{className:"px-4 py-4 hidden md:table-cell",children:e.jsxs("div",{className:"flex items-center space-x-1",children:[e.jsx("span",{className:"text-lg",children:ct(t.language)}),e.jsx("span",{className:"text-xs text-gray-700 font-medium",children:t.language.toUpperCase()})]})}),e.jsx("td",{className:"px-4 py-4 hidden md:table-cell",children:e.jsx("span",{className:`inline-flex items-center px-2 py-0.5 text-xs rounded-full font-medium ${t.version==="v4.0"?"bg-teal-100 text-teal-800":"bg-gray-100 text-gray-700"}`,children:t.version})}),e.jsx("td",{className:"px-4 py-4 hidden md:table-cell",children:t.module?e.jsx("span",{className:"inline-flex items-center px-2 py-1 text-xs rounded-full font-medium bg-blue-100 text-blue-800",children:t.module.name}):e.jsx("span",{className:"text-xs text-gray-400",children:"-"})}),e.jsx("td",{className:"px-4 py-4 hidden lg:table-cell",children:e.jsxs("div",{className:"flex flex-col space-y-1",children:[t.category&&e.jsx("span",{className:"text-xs text-gray-700",children:t.category.name}),t.subcategory&&e.jsx("span",{className:"text-xs text-gray-600",children:t.subcategory.name})]})}),e.jsx("td",{className:"px-4 py-4 hidden md:table-cell",children:e.jsx("span",{className:`inline-flex items-center px-2 py-1 rounded-full text-xs font-medium ${t.status==="PUBLISHED"?"bg-green-100 text-green-800 border border-green-200":"bg-yellow-100 text-yellow-800 border border-yellow-200"}`,children:t.status==="PUBLISHED"?"PUBLISHED":"DRAFT"})}),e.jsx("td",{className:"px-4 py-4",children:e.jsxs("span",{className:`inline-flex items-center px-2 py-1 rounded-full text-xs font-medium ${x.bg} ${x.color}`,children:[e.jsx(x.icon,{className:"w-3 h-3 mr-1"}),x.label]})}),e.jsx("td",{className:"px-4 py-4 hidden xl:table-cell",children:e.jsxs("div",{className:"flex flex-wrap gap-1",children:[t.tags.slice(0,2).map((F,de)=>e.jsx("span",{className:"inline-flex items-center px-2 py-0.5 text-xs bg-gray-100 text-gray-600 rounded",children:F},de)),t.tags.length>2&&e.jsxs("span",{className:"text-xs text-gray-500",children:["+",t.tags.length-2]})]})}),e.jsx("td",{className:"px-4 py-4 text-sm text-gray-600 hidden md:table-cell",children:new Date(t.created_at).toLocaleDateString()}),e.jsx("td",{className:"px-4 py-4 text-sm text-gray-600 hidden lg:table-cell",children:t.last_modified_date?new Date(t.last_modified_date).toLocaleDateString():"-"}),e.jsx("td",{className:"px-4 py-4 text-sm text-gray-600 hidden xl:table-cell",children:e.jsx("span",{className:"truncate block max-w-xs",title:t.created_by_name||t.created_by_email||void 0,children:t.created_by_name||t.created_by_email||"-"})}),e.jsx("td",{className:"px-4 py-4 text-sm text-gray-600 hidden xl:table-cell",children:e.jsx("span",{className:"truncate block max-w-xs",title:t.last_modified_by_name||t.last_modified_by_email||void 0,children:t.last_modified_by_name||t.last_modified_by_email||"-"})}),at&&e.jsx("td",{className:"px-4 py-4",children:e.jsxs("div",{className:"flex items-center justify-center space-x-1",children:[e.jsx("button",{onClick:()=>Ts(t.id,"up"),disabled:Xt||((y=gt[0])==null?void 0:y.id)===t.id,className:"p-1 rounded text-gray-500 hover:text-gray-900 hover:bg-gray-100 disabled:opacity-30 disabled:cursor-not-allowed transition-colors",title:"Move up",children:e.jsx(Ue,{className:"w-4 h-4"})}),e.jsx("button",{onClick:()=>Ts(t.id,"down"),disabled:Xt||((m=gt[gt.length-1])==null?void 0:m.id)===t.id,className:"p-1 rounded text-gray-500 hover:text-gray-900 hover:bg-gray-100 disabled:opacity-30 disabled:cursor-not-allowed transition-colors",title:"Move down",children:e.jsx(Ve,{className:"w-4 h-4"})})]})}),e.jsx("td",{className:"px-4 py-4 text-right",children:e.jsxs("div",{className:"flex items-center justify-end dropdown-menu-container relative",children:[e.jsx("button",{onClick:()=>A(R===t.id?null:t.id),className:"p-2 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded-lg transition-colors",title:"Actions",children:e.jsx(qs,{className:"w-5 h-5"})}),R===t.id&&e.jsx("div",{className:"absolute right-0 top-full mt-1 w-48 bg-white rounded-lg shadow-lg border border-gray-200 z-50",children:e.jsxs("div",{className:"py-1",children:[e.jsxs("button",{onClick:()=>{Es(t),A(null)},className:"flex items-center w-full px-4 py-2 text-sm text-gray-700 hover:bg-gray-50 transition-colors",children:[e.jsx(is,{className:"w-4 h-4 mr-3 text-blue-600"}),"Preview"]}),e.jsxs("button",{onClick:()=>{Ls(t),A(null)},className:"flex items-center w-full px-4 py-2 text-sm text-gray-700 hover:bg-gray-50 transition-colors",children:[e.jsx(os,{className:"w-4 h-4 mr-3 text-blue-600"}),"Edit"]}),e.jsxs("button",{onClick:()=>ka(t),className:"flex items-center w-full px-4 py-2 text-sm text-gray-700 hover:bg-gray-50 transition-colors",children:[e.jsx(Ut,{className:"w-4 h-4 mr-3 text-teal-600"}),"Duplicate"]}),e.jsx("hr",{className:"my-1 border-gray-200"}),e.jsxs("button",{onClick:()=>{Is(t.id),A(null)},className:"flex items-center w-full px-4 py-2 text-sm text-red-600 hover:bg-red-50 transition-colors",children:[e.jsx(as,{className:"w-4 h-4 mr-3"}),"Delete"]})]})})]})})]},t.id)})})]})})}),ze>1&&e.jsxs("div",{className:"bg-white border-t border-gray-200 px-4 py-3 flex items-center justify-between sm:px-6",children:[e.jsxs("div",{className:"flex-1 flex justify-between sm:hidden",children:[e.jsx("button",{onClick:()=>re(Math.max(1,Y-1)),disabled:Y===1,className:"relative inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 text-sm font-medium rounded-md text-gray-700 dark:text-gray-200 bg-white dark:bg-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600 disabled:opacity-50 disabled:cursor-not-allowed",children:"Previous"}),e.jsx("button",{onClick:()=>re(Math.min(ze,Y+1)),disabled:Y===ze,className:"ml-3 relative inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 text-sm font-medium rounded-md text-gray-700 dark:text-gray-200 bg-white dark:bg-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600 disabled:opacity-50 disabled:cursor-not-allowed",children:"Next"})]}),e.jsxs("div",{className:"hidden sm:flex-1 sm:flex sm:items-center sm:justify-between",children:[e.jsx("div",{children:e.jsxs("p",{className:"text-sm text-gray-700",children:["Page ",e.jsx("span",{className:"font-medium",children:Y})," of ",e.jsx("span",{className:"font-medium",children:ze})]})}),e.jsx("div",{children:e.jsxs("nav",{className:"relative z-0 inline-flex rounded-md shadow-sm -space-x-px","aria-label":"Pagination",children:[e.jsx("button",{onClick:()=>re(Math.max(1,Y-1)),disabled:Y===1,className:"relative inline-flex items-center px-2 py-2 rounded-l-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-sm font-medium text-gray-500 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-600 disabled:opacity-50 disabled:cursor-not-allowed",children:"Previous"}),(()=>{const t=[];let j=Math.max(1,Y-Math.floor(2.5)),y=Math.min(ze,j+5-1);y-j<4&&(j=Math.max(1,y-5+1)),j>1&&(t.push(e.jsx("button",{onClick:()=>re(1),className:"relative inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-sm font-medium text-gray-700 dark:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-600",children:"1"},1)),j>2&&t.push(e.jsx("span",{className:"relative inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-sm font-medium text-gray-700 dark:text-gray-200",children:"..."},"ellipsis1")));for(let m=j;m<=y;m++)t.push(e.jsx("button",{onClick:()=>re(m),className:`relative inline-flex items-center px-4 py-2 border text-sm font-medium ${Y===m?"z-10 bg-blue-50 dark:bg-blue-900/30 border-blue-500 dark:border-blue-400 text-blue-600 dar
33k:text-blue-300":"bg-white dark:bg-gray-700 border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-600"}`,children:m},m));return y<ze&&(y<ze-1&&t.push(e.jsx("span",{className:"relative inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-sm font-medium text-gray-700 dark:text-gray-200",children:"..."},"ellipsis2")),t.push(e.jsx("button",{onClick:()=>re(ze),className:"relative inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-sm font-medium text-gray-700 dark:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-600",children:ze},ze))),t})(),e.jsx("button",{onClick:()=>re(Math.min(ze,Y+1)),disabled:Y===ze,className:"relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-sm font-medium text-gray-500 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-600 disabled:opacity-50 disabled:cursor-not-allowed",children:"Next"})]})})]})]})]}),G&&e.jsx(Wa,{isOpen:G,onClose:()=>E(!1),onSuccess:()=>{Ae(),mt()},categories:g}),ye&&ae&&e.jsx(Ka,{isOpen:ye,onClose:()=>{me(!1),te(null)},onSuccess:async()=>{const t=await Ae();await mt();const x=t.find(j=>j.id===ae.id);x&&te(x)},article:ae,categories:g,initialMode:P}),B&&e.jsx(Ya,{isOpen:B,onClose:()=>be(!1),onSuccess:()=>{Ae(),mt()}}),pe&&Z&&e.jsx(sa,{articleId:Z.id,articleTitle:Z.title,onClose:()=>{q(!1),V(null)},onComplete:()=>{Ae(),q(!1),V(null)}}),$&&e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4",children:e.jsxs("div",{className:"bg-white rounded-lg shadow-xl max-w-2xl w-full max-h-[90vh] overflow-y-auto",children:[e.jsxs("div",{className:"sticky top-0 bg-white border-b border-gray-200 px-6 py-4 flex items-center justify-between",children:[e.jsx("h2",{className:"text-xl font-semibold text-gray-900",children:"Assign Module to Selected Articles"}),e.jsx("button",{onClick:()=>{ie(!1),xe("")},className:"text-gray-400 hover:text-gray-600",children:e.jsx(De,{className:"w-6 h-6"})})]}),e.jsxs("div",{className:"px-6 py-4",children:[e.jsxs("p",{className:"text-sm text-gray-700 mb-4",children:["Assign module to ",e.jsx("span",{className:"font-semibold",children:c.size})," selected article",c.size!==1?"s":""]}),e.jsx("div",{className:"mb-4 p-4 bg-yellow-50 border border-yellow-200 rounded-lg",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx(et,{className:"w-5 h-5 text-yellow-600 mr-2 flex-shrink-0 mt-0.5"}),e.jsxs("div",{className:"text-sm text-yellow-800",children:[e.jsx("strong",{children:"Warning:"})," Changing the module will reset category and subcategory assignments for these articles."]})]})}),e.jsxs("div",{className:"mb-6",children:[e.jsxs("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:["Select Module ",e.jsx("span",{className:"text-red-500",children:"*"})]}),e.jsxs("select",{value:Q,onChange:t=>xe(t.target.value),className:"w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500",disabled:fe,children:[e.jsx("option",{value:"",children:"Choose a module..."}),Ie.map(t=>e.jsx("option",{value:t.id,children:t.name},t.id))]})]}),e.jsxs("div",{className:"mb-4",children:[e.jsx("h3",{className:"text-sm font-medium text-gray-700 mb-2",children:"Articles to be updated:"}),e.jsxs("div",{className:"bg-gray-50 rounded-lg p-3 max-h-48 overflow-y-auto",children:[e.jsx("ul",{className:"space-y-1.5",children:b.filter(t=>c.has(t.id)).slice(0,5).map(t=>e.jsxs("li",{className:"text-sm text-gray-700 flex items-start",children:[e.jsx("span",{className:"text-blue-600 mr-2",children:"•"}),e.jsx("span",{className:"flex-1",children:t.title})]},t.id))}),c.size>5&&e.jsxs("p",{className:"text-sm text-gray-500 mt-2 ml-4",children:["... and ",c.size-5," more article",c.size-5!==1?"s":""]})]})]})]}),e.jsxs("div",{className:"sticky bottom-0 bg-gray-50 px-6 py-4 flex justify-end space-x-3 border-t border-gray-200",children:[e.jsx("button",{onClick:()=>{ie(!1),xe("")},disabled:fe,className:"btn-secondary",children:"Cancel"}),e.jsx("button",{onClick:ha,disabled:!Q||fe,className:"btn-primary flex items-center",children:fe?e.jsxs(e.Fragment,{children:[e.jsx(ke,{className:"w-4 h-4 mr-2 animate-spin"}),"Assigning ",Ye.current,"/",Ye.total]}):e.jsxs(e.Fragment,{children:[e.jsx(Os,{className:"w-4 h-4 mr-2"}),"Assign Module"]})})]})]})}),Ee&&jt&&e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4",children:e.jsxs("div",{className:"bg-white rounded-lg shadow-xl max-w-md w-full",children:[e.jsxs("div",{className:"px-6 pt-6 pb-4 text-center",children:[e.jsx("div",{className:"w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-4",children:e.jsx(Se,{className:"w-10 h-10 text-green-600"})}),e.jsx("h2",{className:"text-xl font-semibold text-gray-900 mb-2",children:"Module Assignment Successful!"})]}),e.jsxs("div",{className:"px-6 pb-4",children:[e.jsxs("p",{className:"text-gray-700 text-center mb-4",children:["Successfully assigned ",e.jsx("span",{className:"font-semibold",children:jt.count})," article",jt.count!==1?"s":""," to module ",e.jsxs("span",{className:"font-semibold",children:['"',jt.moduleName,'"']}),"."]}),e.jsx("div",{className:"bg-green-50 border border-green-200 rounded-lg p-3 mb-4",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx("svg",{className:"w-5 h-5 text-green-600 mr-2 flex-shrink-0 mt-0.5",fill:"currentColor",viewBox:"0 0 20 20",children:e.jsx("path",{fillRule:"evenodd",d:"M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z",clipRule:"evenodd"})}),e.jsx("p",{className:"text-sm text-green-800",children:"Categories and subcategories have been reset."})]})})]}),e.jsx("div",{className:"px-6 pb-6",children:e.jsx("button",{onClick:pa,className:"w-full bg-green-600 hover:bg-green-700 text-white font-medium py-2.5 px-4 roun
33ded-lg transition-colors",children:"OK"})})]})}),aa&&e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4",children:e.jsxs("div",{className:"bg-white rounded-lg shadow-xl max-w-2xl w-full max-h-[90vh] overflow-y-auto",children:[e.jsxs("div",{className:"sticky top-0 bg-white border-b border-gray-200 px-6 py-4 flex items-center justify-between",children:[e.jsx("h2",{className:"text-xl font-semibold text-gray-900",children:"Assign Category to Selected Articles"}),e.jsx("button",{onClick:()=>{wt(!1),Nt(""),ut("")},className:"text-gray-400 hover:text-gray-600",children:e.jsx(De,{className:"w-6 h-6"})})]}),e.jsxs("div",{className:"px-6 py-4",children:[e.jsxs("p",{className:"text-sm text-gray-700 mb-4",children:["Assign category to ",e.jsx("span",{className:"font-semibold",children:c.size})," selected article",c.size!==1?"s":""]}),(()=>{const t=ot();return t?e.jsx("div",{className:"mb-4 p-4 bg-blue-50 border border-blue-200 rounded-lg",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx(Se,{className:"w-5 h-5 text-blue-600 mr-2 flex-shrink-0 mt-0.5"}),e.jsxs("div",{className:"text-sm text-blue-800",children:["All selected articles are from module: ",e.jsx("strong",{children:t.moduleName})]})]})}):e.jsx("div",{className:"mb-4 p-4 bg-red-50 border border-red-200 rounded-lg",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx(et,{className:"w-5 h-5 text-red-600 mr-2 flex-shrink-0 mt-0.5"}),e.jsxs("div",{className:"text-sm text-red-800",children:[e.jsx("strong",{children:"Error:"})," Selected articles are from different modules. Please select articles from the same module to assign categories."]})]})})})(),e.jsx("div",{className:"mb-4 p-3 bg-blue-50 border-l-4 border-blue-400 rounded",children:e.jsxs("p",{className:"text-sm text-blue-800",children:[e.jsx("strong",{children:"Tip:"})," This will only update the category and subcategory. The module assignment will remain unchanged."]})}),e.jsxs("div",{className:"mb-4",children:[e.jsxs("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:["Select Category ",e.jsx("span",{className:"text-red-500",children:"*"})]}),e.jsxs("select",{value:Xe,onChange:t=>{Nt(t.target.value),ut("")},className:"w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500",disabled:!ot()||$e,children:[e.jsx("option",{value:"",children:"Choose a category..."}),Ms().map(t=>e.jsx("option",{value:t.id,children:t.name},t.id))]}),Ms().length===0&&ot()&&e.jsx("p",{className:"text-xs text-gray-500 mt-1",children:"No categories available for this module"})]}),e.jsxs("div",{className:"mb-6",children:[e.jsxs("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:["Select Subcategory ",e.jsx("span",{className:"text-gray-400",children:"(Optional)"})]}),e.jsxs("select",{value:vt,onChange:t=>ut(t.target.value),className:"w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500",disabled:!Xe||$e,children:[e.jsx("option",{value:"",children:"None (No subcategory)"}),ba().map(t=>e.jsx("option",{value:t.id,children:t.name},t.id))]}),!Xe&&e.jsx("p",{className:"text-xs text-gray-500 mt-1",children:"Select a category first to see subcategories"})]}),e.jsxs("div",{className:"mb-4",children:[e.jsx("h3",{className:"text-sm font-medium text-gray-700 mb-2",children:"Articles to be updated:"}),e.jsxs("div",{className:"bg-gray-50 rounded-lg p-3 max-h-48 overflow-y-auto",children:[e.jsx("ul",{className:"space-y-1.5",children:b.filter(t=>c.has(t.id)).slice(0,5).map(t=>e.jsxs("li",{className:"text-sm text-gray-700 flex items-start",children:[e.jsx("span",{className:"text-blue-600 mr-2",children:"•"}),e.jsx("span",{className:"flex-1",children:t.title})]},t.id))}),c.size>5&&e.jsxs("p",{className:"text-sm text-gray-500 mt-2 ml-4",children:["... and ",c.size-5," more article",c.size-5!==1?"s":""]})]})]})]}),e.jsxs("div",{className:"sticky bottom-0 bg-gray-50 px-6 py-4 flex justify-end space-x-3 border-t border-gray-200",children:[e.jsx("button",{onClick:()=>{wt(!1),Nt(""),ut("")},disabled:$e,className:"btn-secondary",children:"Cancel"}),e.jsx("button",{onClick:ya,disabled:!Xe||$e||!ot(),className:"btn-primary flex items-center",children:$e?e.jsxs(e.Fragment,{children:[e.jsx(ke,{className:"w-4 h-4 mr-2 animate-spin"}),"Assigning ",ms.current,"/",ms.total]}):e.jsxs(e.Fragment,{children:[e.jsx(ht,{className:"w-4 h-4 mr-2"}),"Assign Category"]})})]})]})}),ra&&nt&&e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4",children:e.jsxs("div",{className:"bg-white rounded-lg shadow-xl max-w-md w-full",children:[e.jsxs("div",{className:"px-6 pt-6 pb-4 text-center",children:[e.jsx("div",{className:"w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-4",children:e.jsx(Se,{className:"w-10 h-10 text-green-600"})}),e.jsx("h2",{className:"text-xl font-semibold text-gray-900 mb-2",children:"Category Assignment Successful!"})]}),e.jsx("div",{className:"px-6 pb-4",children:e.jsx("div",{className:"bg-gray-50 rounded-lg p-4 mb-4",children:e.jsxs("div",{className:"text-center",children:[e.jsx("p",{className:"text-sm text-gray-600 mb-2",children:"Successfully assigned:"}),e.jsxs("p",{className:"text-lg font-semibold text-gray-900",children:[nt.categoryName,nt.subcategoryName&&e.jsxs("span",{className:"text-gray-600",children:[" > ",nt.subcategoryName]})]}),e.jsxs("p",{className:"text-sm text-gray-600 mt-2",children:["to ",e.jsx("strong",{children:nt.count})," article",nt.count!==1?"s":""]})]})})}),e.jsx("div",{className:"px-6 pb-6",children:e.jsx("button",{onClick:fa,className:"w-full bg-green-600 hover:bg-green-700 text-white font-medium py-2.5 px-4 roun
33ded-lg transition-colors",children:"Done"})})]})}),la&&e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4",children:e.jsxs("div",{className:"bg-white rounded-lg shadow-xl max-w-2xl w-full max-h-[90vh] overflow-y-auto",children:[e.jsxs("div",{className:"sticky top-0 bg-white border-b border-gray-200 px-6 py-4 flex items-center justify-between",children:[e.jsx("h2",{className:"text-xl font-semibold text-gray-900",children:"Change Status for Selected Articles"}),e.jsx("button",{onClick:()=>{kt(!1),_t("PUBLISHED")},className:"text-gray-400 hover:text-gray-600",children:e.jsx(De,{className:"w-6 h-6"})})]}),e.jsxs("div",{className:"px-6 py-4",children:[e.jsxs("div",{className:"mb-6",children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:"Select Status *"}),e.jsxs("select",{value:St,onChange:t=>_t(t.target.value),className:"w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500",disabled:Ge,children:[e.jsx("option",{value:"PUBLISHED",children:"Published - Visible to users"}),e.jsx("option",{value:"DRAFT",children:"Draft - Hidden from users"})]}),e.jsx("p",{className:"text-xs text-gray-500 mt-1",children:"Published articles appear in the public knowledge base. Drafts are only visible to admins."})]}),e.jsxs("div",{className:"mb-4",children:[e.jsx("h3",{className:"text-sm font-medium text-gray-700 mb-2",children:"Articles to be updated:"}),e.jsxs("div",{className:"bg-gray-50 rounded-lg p-3 max-h-48 overflow-y-auto",children:[e.jsx("ul",{className:"space-y-1.5",children:b.filter(t=>c.has(t.id)).slice(0,5).map(t=>e.jsxs("li",{className:"text-sm text-gray-700 flex items-start",children:[e.jsx("span",{className:"text-blue-600 mr-2",children:"•"}),e.jsx("span",{className:"flex-1",children:t.title})]},t.id))}),c.size>5&&e.jsxs("p",{className:"text-sm text-gray-500 mt-2 ml-4",children:["... and ",c.size-5," more article",c.size-5!==1?"s":""]})]})]})]}),e.jsxs("div",{className:"sticky bottom-0 bg-gray-50 px-6 py-4 flex justify-end space-x-3 border-t border-gray-200",children:[e.jsx("button",{onClick:()=>{kt(!1),_t("PUBLISHED")},disabled:Ge,className:"btn-secondary",children:"Cancel"}),e.jsx("button",{onClick:ja,disabled:Ge,className:"btn-primary flex items-center",children:Ge?e.jsxs(e.Fragment,{children:[e.jsx(ke,{className:"w-4 h-4 mr-2 animate-spin"}
33),"Updating ",bs.current,"/",bs.total]}):e.jsxs(e.Fragment,{children:[e.jsx(dt,{className:"w-4 h-4 mr-2"}),"Change Status"]})})]})]})}),na&&Ct&&e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4",children:e.jsxs("div",{className:"bg-white rounded-lg shadow-xl max-w-md w-full",children:[e.jsxs("div",{className:"px-6 pt-6 pb-4 text-center",children:[e.jsx("div",{className:"w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-4",children:e.jsx(Se,{className:"w-10 h-10 text-green-600"})}),e.jsx("h2",{className:"text-xl font-semibold text-gray-900 mb-2",children:"Status Changed Successfully!"})]}),e.jsx("div",{className:"px-6 pb-4",children:e.jsx("div",{className:"bg-gray-50 rounded-lg p-4 mb-4",children:e.jsxs("div",{className:"text-center",children:[e.jsx("p",{className:"text-sm text-gray-600 mb-2",children:"Successfully changed status to:"}),e.jsx("p",{className:"text-lg font-semibold text-gray-900",children:Ct.status}),e.jsxs("p",{className:"text-sm text-gray-600 mt-2",children:["for ",e.jsx("strong",{children:Ct.count})," article",Ct.count!==1?"s":""]})]})})}),e.jsx("div",{className:"px-6 pb-6",children:e.jsx("button",{onClick:wa,className:"w-full bg-green-600 hover:bg-green-700 text-white font-medium py-2.5 px-4 rounded-lg transition-colors",children:"Done"})})]})}),ia&&e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4",children:e.jsxs("div",{className:"bg-white rounded-lg shadow-xl max-w-2xl w-full max-h-[90vh] overflow-y-auto",children:[e.jsxs("div",{className:"sticky top-0 bg-white border-b border-gray-200 px-6 py-4 flex items-center justify-between",children:[e.jsx("h2",{className:"text-xl font-semibold text-gray-900",children:"Change Version for Selected Articles"}),e.jsx("button",{onClick:()=>{At(!1),Dt("v3.7")},className:"text-gray-400 hover:text-gray-600",children:e.jsx(De,{className:"w-6 h-6"})})]}),e.jsxs("div",{className:"px-6 py-4",children:[e.jsxs("div",{className:"mb-6",children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:"Select Version *"}),e.jsxs("select",{value:Wt,onChange:t=>Dt(t.target.value),className:"w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500",disabled:tt,children:[e.jsx("option",{value:"v3.7",children:"v3.7"}),e.jsx("option",{value:"v4.0",children:"v4.0"})]}),e.jsx("p",{className:"text-xs text-gray-500 mt-1",children:"Assign a software version tag to the selected articles."})]}),e.jsxs("div",{className:"mb-4",children:[e.jsx("h3",{className:"text-sm font-medium text-gray-700 mb-2",children:"Articles to be updated:"}),e.jsxs("div",{className:"bg-gray-50 rounded-lg p-3 max-h-48 overflow-y-auto",children:[e.jsx("ul",{className:"space-y-1.5",children:b.filter(t=>c.has(t.id)).slice(0,5).map(t=>e.jsxs("li",{className:"text-sm text-gray-700 flex items-start",children:[e.jsx("span",{className:"text-blue-600 mr-2",children:"•"}),e.jsx("span",{className:"flex-1",children:t.title}),e.jsx("span",{className:`ml-2 inline-flex items-center px-1.5 py-0.5 text-xs rounded font-medium ${t.version==="v4.0"?"bg-teal-100 text-teal-700":"bg-gray-100 text-gray-600"}`,children:t.version})]},t.id))}),c.size>5&&e.jsxs("p",{className:"text-sm text-gray-500 mt-2 ml-4",children:["... and ",c.size-5," more article",c.size-5!==1?"s":""]})]})]})]}),e.jsxs("div",{className:"sticky bottom-0 bg-gray-50 px-6 py-4 flex justify-end space-x-3 border-t border-gray-200",children:[e.jsx("button",{onClick:()=>{At(!1),Dt("v3.7")},disabled:tt,className:"btn-secondary",children:"Cancel"}),e.jsx("button",{onClick:Na,disabled:tt,className:"btn-primary flex items-center",children:tt?e.jsxs(e.Fragment,{children:[e.jsx(ke,{className:"w-4 h-4 mr-2 animate-spin"}),"Updating ",ws.current,"/",ws.total]}):e.jsxs(e.Fragment,{children:[e.jsx(ht,{className:"w-4 h-4 mr-2"}),"Change Version"]})})]})]})}),oa&&Ft&&e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4",children:e.jsxs("div",{className:"bg-white rounded-lg shadow-xl max-w-md w-full",children:[e.jsxs("div",{className:"px-6 pt-6 pb-4 text-center",children:[e.jsx("div",{className:"w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-4",children:e.jsx(Se,{className:"w-10 h-10 text-green-600"})}),e.jsx("h2",{className:"text-xl font-semibold text-gray-900 mb-2",children:"Version Changed Successfully!"})]}),e.jsx("div",{className:"px-6 pb-4",children:e.jsx("div",{className:"bg-gray-50 rounded-lg p-4 mb-4",children:e.jsxs("div",{className:"text-center",children:[e.jsx("p",{className:"text-sm text-gray-600 mb-2",children:"Successfully changed ver
33sion to:"}),e.jsx("p",{className:"text-lg font-semibold text-gray-900",children:Ft.version}),e.jsxs("p",{className:"text-sm text-gray-600 mt-2",children:["for ",e.jsx("strong",{children:Ft.count})," article",Ft.count!==1?"s":""]})]})})}),e.jsx("div",{className:"px-6 pb-6",children:e.jsx("button",{onClick:va,className:"w-full bg-green-600 hover:bg-green-700 text-white font-medium py-2.5 px-4 rounded-lg transition-colors",children:"Done"})})]})}),da&&we&&e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4",children:e.jsxs("div",{className:"bg-white rounded-lg shadow-xl max-w-lg w-full",children:[e.jsxs("div",{className:"sticky top-0 bg-white border-b border-gray-200 px-6 py-4 flex items-center justify-between",children:[e.jsx("h2",{className:"text-xl font-semibold text-gray-900",children:"Duplicate Article"}),e.jsx("button",{onClick:()=>{It(!1),Mt(null),Tt("v3.7")},className:"text-gray-400 hover:text-gray-600",children:e.jsx(De,{className:"w-6 h-6"})})]}),e.jsxs("div",{className:"px-6 py-4",children:[e.jsxs("div",{className:"mb-4",children:[e.jsx("p",{className:"text-sm text-gray-600 mb-1",children:"Duplicating:"}),e.jsxs("div",{className:"bg-gray-50 rounded-lg p-3",children:[e.jsx("p",{className:"text-sm font-medium text-gray-900",children:we.title}),e.jsxs("div",{className:"flex items-center gap-2 mt-1",children:[e.jsx("span",{className:`inline-flex items-center px-1.5 py-0.5 text-xs rounded font-medium ${we.version==="v4.0"?"bg-teal-100 text-teal-700":"bg-gray-100 text-gray-600"}`,children:we.version}),e.jsx("span",{className:`inline-flex items-center px-1.5 py-0.5 text-xs rounded font-medium ${we.status==="PUBLISHED"?"bg-green-100 text-green-700":"bg-yellow-100 text-yellow-700"}`,children:we.status})]})]})]}),e.jsxs("div",{className:"mb-4",children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:"Version for Duplicate *"}),e.jsxs("select",{value:Gt,onChange:t=>Tt(t.target.value),className:"w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500",disabled:Lt,children:[e.jsx("option",{value:"v3.7",children:"v3.7"}),e.jsx("option",{value:"v4.0",children:"v4.0"})]})]}),e.jsx("div",{className:"bg-blue-50 border border-blue-200 rounded-lg p-3",children:e.jsxs("p",{className:"text-xs text-blue-700",children:["The duplicate will be created as a ",e.jsx("strong",{children:"DRAFT"}),". All content, categories, and tags will be copied from the original."]})})]}),e.jsxs("div",{className:"sticky bottom-0 bg-gray-50 px-6 py-4 flex justify-end space-x-3 border-t border-gray-200",children:[e.jsx("button",{onClick:()=>{It(!1),Mt(null),Tt("v3.7")},disabled:Lt,className:"btn-secondary",children:"Cancel"}),e.jsx("button",{onClick:Sa,disabled:Lt,className:"btn-primary flex items-center",children:Lt?e.jsxs(e.Fragment,{children:[e.jsx(ke,{className:"w-4 h-4 mr-2 animate-spin"}),"Duplicating..."]}):e.jsxs(e.Fragment,{children:[e.jsx(Ut,{className:"w-4 h-4 mr-2"}),"Duplicate"]})})]})]})}),ca&&e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4",children:e.jsxs("div",{className:"bg-white rounded-lg shadow-xl max-w-2xl w-full max-h-[90vh] overflow-y-auto",children:[e.jsxs("div",{className:"sticky top-0 bg-white border-b border-gray-200 px-6 py-4 flex items-center justify-between",children:[e.jsx("h2",{className:"text-xl font-semibold text-gray-900",children:"Duplicate Selected Articles"}),e.jsx("button",{onClick:()=>{Et(!1),Rt("v3.7")},className:"text-gray-400 hover:text-gray-600",children:e.jsx(De,{className:"w-6 h-6"})})]}),e.jsxs("div",{className:"px-6 py-4",children:[e.jsxs("div",{className:"mb-6",children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:"Version for Duplicates *"}),e.jsxs("select",{value:Kt,onChange:t=>Rt(t.target.value),className:"w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500",disabled:st,children:[e.jsx("option",{value:"v3.7",children:"v3.7"}),e.jsx("option",{value:"v4.0",children:"v4.0"})]}),e.jsxs("p",{className:"text-xs text-gray-500 mt-1",children:["All duplicates will be created as ",e.jsx("strong",{children:"DRAFT"})," with this version tag."]})]}),e.jsxs("div",{className:"mb-4",children:[e.jsx("h3",{className:"text-sm font-medium text-gray-700 mb-2",children:"Articles to be duplicated:"}),e.jsxs("div",{className:"bg-gray-50 rounded-lg p-3 max-h-48 overflow-y-auto",children:[e.jsx("ul",{className:"space-y-1.5",children:b.filter(t=>c.has(t.id)).slice(0,5).map(t=>e.jsxs("li",{className:"text-sm text-gray-700 flex items-start",children:[e.jsx("span",{className:"text-blue-600 mr-2",children:"•"}),e.jsx("span",{className:"flex-1",children:t.title}),e.jsx("span",{className:`ml-2 inline-flex items-center px-1.5 py-0.5 text-xs rounded font-medium ${t.version==="v4.0"?"bg-teal-100 text-teal-700":"bg-gray-100 text-gray-600"}
33`,children:t.version})]},t.id))}),c.size>5&&e.jsxs("p",{className:"text-sm text-gray-500 mt-2 ml-4",children:["... and ",c.size-5," more article",c.size-5!==1?"s":""]})]})]}),e.jsx("div",{className:"bg-blue-50 border border-blue-200 rounded-lg p-3",children:e.jsxs("p",{className:"text-xs text-blue-700",children:["This will create ",e.jsx("strong",{children:c.size})," new article",c.size!==1?"s":""," as copies of the selected articles. All content, categories, and tags will be copied from the originals."]})})]}),e.jsxs("div",{className:"sticky bottom-0 bg-gray-50 px-6 py-4 flex justify-end space-x-3 border-t border-gray-200",children:[e.jsx("button",{onClick:()=>{Et(!1),Rt("v3.7")},disabled:st,className:"btn-secondary",children:"Cancel"}),e.jsx("button",{onClick:_a,disabled:st,className:"btn-primary flex items-center",children:st?e.jsxs(e.Fragment,{children:[e.jsx(ke,{className:"w-4 h-4 mr-2 animate-spin"}),"Duplicating ",Pt.current,"/",Pt.total]}):e.jsxs(e.Fragment,{children:[e.jsx(Ut,{className:"w-4 h-4 mr-2"}),"Duplicate ",c.size," Article",c.size!==1?"s":""]})})]})]})}),xa&&it&&e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4",children:e.jsxs("div",{className:"bg-white rounded-lg shadow-xl max-w-md w-full",children:[e.jsxs("div",{className:"px-6 pt-6 pb-4 text-center",children:[e.jsx("div",{className:"w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-4",children:e.jsx(Se,{className:"w-10 h-10 text-green-600"})}),e.jsxs("h2",{className:"text-xl font-semibold text-gray-900 mb-2",children:[it.count===1?"Article":"Articles"," Duplicated Successfully!"]})]}),e.jsx("div",{className:"px-6 pb-4",children:e.jsx("div",{className:"bg-gray-50 rounded-lg p-4 mb-4",children:e.jsxs("div",{className:"text-center",children:[e.jsxs("p",{className:"text-sm text-gray-600 mb-2",children:["Created ",e.jsx("strong",{children:it.count})," new ",it.count===1?"copy":"copies"," as:"]}),e.jsxs("div",{className:"flex items-center justify-center gap-2",children:[e.jsx("span",{className:`inline-flex items-center px-2 py-1 text-sm rounded font-medium ${it.version==="v4.0"?"bg-teal-100 text-teal-700":"bg-gray-100 text-gray-600"}`,children:it.version}),e.jsx("span",{className:"inline-flex items-center px-2 py-1 text-sm rounded font-medium bg-yellow-100 text-yellow-700",children:"DRAFT"})]})]})})}),e.jsx("div",{className:"px-6 pb-6",children:e.jsx("button",{onClick:Ca,className:"w-full bg-green-600 hover:bg-green-700 text-white font-medium py-2.5 px-4 rounded-lg transition-colors",children:"Done"})})]})})]})},fr=()=>e.jsx(sr,{});export{fr as default};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.