1import{r as x,v as L,h as J,j as e,l as H,w as ie,x as E,y as Z,u as ce,z as de,A as oe}from"./index-CcaPaFKR.js";import{u as V}from"./useQuery-mApUf7_D.js";import{t as z,c as me,n as U,e as xe,g as X,m as Y,a as B,b as ue}from"./en-US-8upN5vI8.js";import{u as he}from"./useMutation-Dh6FSrqR.js";import{F as K}from"./ArrowPathIcon-BMdAPMn-.js";import{F as Q}from"./ExclamationTriangleIcon-aOJaj_aS.js";function ge({title:a,titleId:t,...s},o){return x.createElement("svg",Object.assign({xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",strokeWidth:1.5,stroke:"currentColor","aria-hidden":"true","data-slot":"icon",ref:o,"aria-labelledby":t},s),a?x.createElement("title",{id:t},a):null,x.createElement("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M5.25 7.5A2.25 2.25 0 0 1 7.5 5.25h9a2.25 2.25 0 0 1 2.25 2.25v9a2.25 2.25 0 0 1-2.25 2.25h-9a2.25 2.25 0 0 1-2.25-2.25v-9Z"}))}const pe=x.forwardRef(ge);function O(a,t){const s=+z(a)-+z(t);return s<0?-1:s>0?1:s}function fe(a){return me(a,Date.now())}function ye(a,t,s){const[o,l]=U(s==null?void 0:s.in,a,t),u=o.getFullYear()-l.getFullYear(),g=o.getMonth()-l.getMonth();return u*12+g}function je(a){return t=>{const o=(a?Math[a]:Math.trunc)(t);return o===0?0:o}}function ve(a,t){return+z(a)-+z(t)}function be(a,t){const s=z(a,t==null?void 0:t.in);return s.setHours(23,59,59,999),s}function Ne(a,t){const s=z(a,t==null?void 0:t.in),o=s.getMonth();return s.setFullYear(s.getFullYear(),o+1,0),s.setHours(23,59,59,999),s}function we(a,t){const s=z(a,t==null?void 0:t.in);return+be(s,t)==+Ne(s,t)}function _e(a,t,s){const[o,l,u]=U(s==null?void 0:s.in,a,a,t),g=O(l,u),r=Math.abs(ye(l,u));if(r<1)return 0;l.getMonth()===1&&l.getDate()>27&&l.setDate(30),l.setMonth(l.getMonth()-g*r);let m=O(l,u)===-g;we(o)&&r===1&&O(o,u)===1&&(m=!1);const p=g*(r-+m);return p===0?0:p}function Ce(a,t,s){const o=ve(a,t)/1e3;return je(s==null?void 0:s.roundingMethod)(o)}function Se(a,t,s){const o=ue(),l=(s==null?void 0:s.locale)??o.locale??xe,u=2520,g=O(a,t);if(isNaN(g))throw new RangeError("Invalid time value");const r=Object.assign({},s,{addSuffix:s==null?void 0:s.addSuffix,comparison:g}),[m,p]=U(s==null?void 0:s.in,...g>0?[t,a]:[a,t]),y=Ce(p,m),j=(X(p)-X(m))/1e3,n=Math.round((y-j)/60);let d;if(n<2)return s!=null&&s.includeSeconds?y<5?l.formatDistance("lessThanXSeconds",5,r):y<10?l.formatDistance("lessThanXSeconds",10,r):y<20?l.formatDistance("lessThanXSeconds",20,r):y<40?l.formatDistance("halfAMinute",0,r):y<60?l.formatDistance("lessThanXMinutes",1,r):l.formatDistance("xMinutes",1,r):n===0?l.formatDistance("lessThanXMinutes",1,r):l.formatDistance("xMinutes",n,r);if(n<45)return l.formatDistance("xMinutes",n,r);if(n<90)return l.formatDistance("aboutXHours",1,r);if(n<Y){const c=Math.round(n/60);return l.formatDistance("aboutXHours",c,r)}else{if(n<u)return l.formatDistance("xDays",1,r);if(n<B){const c=Math.round(n/Y);return l.formatDistance("xDays",c,r)}else if(n<B*2)return d=Math.round(n/B),l.formatDistance("aboutXMonths",d,r)}if(d=_e(p,m),d<12){const c=Math.round(n/B);return l.formatDistance("xMonths",c,r)}else{const c=d%12,_=Math.trunc(d/12);return c<3?l.formatDistance("aboutXYears",_,r):c<9?l.formatDistance("overXYears",_,r):l.formatDistance("almostXYears",_+1,r)}}function ee(a,t){return Se(a,fe(a),t)}const ke=({contextId:a,enabled:t=!0,pollInterval:s=3e3,onComplete:o,onError:l})=>{const[u,g]=x.useState(null),[r,m]=x.useState(!1),[p,y]=x.useState(null),j=x.useRef(null),n=x.useRef(!1),d=x.useCallback(async()=>{var M,C;if(!(!a||!t||n.current))try{m(!0),y(null);const v=await L.getProcessingStatus(a);g(v),["processed","failed","cancelled"].includes(v.context.status)&&(n.current=!0,j.current&&(clearInterval(j.current),j.current=null),v.context.status==="processed"&&o?o(v):v.context.status==="failed"&&l&&l(v.processing.error||"Processing failed"))}catch(v){const b=((C=(M=v.response)==null?void 0:M.data)==null?void 0:C.error)||v.message||"Failed to fetch task status";y(b),l&&l(b)}finally{m(!1)}},[a,t,o,l]);x.useEffect(()=>{if(!(!t||!a||n.current))return d(),j.current=setInterval(d,s),()=>{j.current&&(clearInterval(j.current),j.current=null)}},[t,a,s,d]);const c=x.useCallback(()=>{n.current=!1,d()},[d]),_=x.useCallback(()=>{n.current=!0,j.current&&(clearInterval(j.current),j.current=null)},[]);return{taskStatus:u,isLoading:r,error:p,refresh:c,stop:_,isCompleted:n.current}},Me=({enabled:a=!0,pollInterval:t=1e4,onlyWhenVisible:s=!0}={})=>
1{const[o,l]=x.useState(new Set),[u,g]=x.useState(!0),r=x.useRef(null),m=x.useRef(0);x.useEffect(()=>{if(!s)return;const y=()=>{g(!document.hidden)};return document.addEventListener("visibilitychange",y),()=>document.removeEventListener("visibilitychange",y)},[s]);const p=x.useCallback(async()=>{if(!(!a||s&&!u))try{const y=await L.listContexts(),j=new Set(y.filter(_=>["processing","uploading"].includes(_.status)).map(_=>_.id));l(j);const n=j.size,d=m.current;m.current=n,r.current&&(clearInterval(r.current),r.current=null);let c=t;n===0?c=t*3:n<d&&(c=t/2),r.current=setInterval(p,c)}catch(y){console.error("Failed to check processing contexts:",y),r.current&&(clearInterval(r.current),r.current=setInterval(p,t*2))}},[a,t,u,s]);return x.useEffect(()=>{if(!a){r.current&&(clearInterval(r.current),r.current=null);return}return p(),()=>{r.current&&(clearInterval(r.current),r.current=null)}},[a,p]),x.useEffect(()=>{a&&(u&&!r.current?p():!u&&r.current&&(clearInterval(r.current),r.current=null))},[u,a,p]),{processingContexts:o,refresh:p,isPolling:!!r.current}},De=()=>{const[a,t]=x.useState(!1),[s,o]=x.useState(null);return{retryProcessing:x.useCallback(async u=>{var g,r;try{return t(!0),o(null),await L.retryProcessing(u),!0}catch(m){const p=((r=(g=m.response)==null?void 0:g.data)==null?void 0:r.error)||m.message||"Failed to retry processing";return o(p),!1}finally{t(!1)}},[]),isRetrying:a,error:s}},Fe=()=>{const[a,t]=x.useState(!1),[s,o]=x.useState(null);return{cancelProcessing:x.useCallback(async u=>{var g,r;try{return t(!0),o(null),await L.cancelProcessing(u),!0}catch(m){const p=((r=(g=m.response)==null?void 0:g.data)==null?void 0:r.error)||m.message||"Failed to cancel processing";return o(p),!1}finally{t(!1)}},[]),isCancelling:a,error:s}},W={uploading:"bg-blue-100 text-blue-800",processing:"bg-yellow-100 text-yellow-800",processed:"bg-green-100 text-green-800",failed:"bg-red-100 text-red-800",active:"bg-green-100 text-green-800",archived:"bg-gray-100 text-gray-800"},$={client_preferences:"Client Preferences",booking_history:"Booking History",travel_policies:"Travel Policies",expense_data:"Expense Data",vendor_data:"Vendor Data",custom:"Custom Context"},Te=({onContextSelect:a,onUploadClick:t,canUpload:s=!0})=>{const[o,l]=x.useState("all"),[u,g]=x.useState(""),{currentOrganization:r}=J(),{data:m=[],isLoading:p,error:y}=V({queryKey:["contexts",r==null?void 0:r.id],queryFn:L.listContexts,enabled:!!r&&!!localStorage.getItem("access_token")}),j=m.filter(d=>{const c=d.name.toLowerCase().includes(u.toLowerCase())||d.description.toLowerCase().includes(u.toLowerCase()),_=o==="all"||d.context_type===o;return c&&_}),n=d=>{a&&a(d)};return r?p?e.jsx("div",{className:"bg-white shadow rounded-lg p-6",children:e.jsxs("div",{className:"animate-pulse space-y-4",children:[e.jsx("div",{className:"h-4 bg-gray-200 rounded w-1/4"}),e.jsx("div",{className:"space-y-3",children:[1,2,3].map(d=>e.jsx("div",{className:"h-16 bg-gray-200 rounded"},d))})]})}):y?e.jsx("div",{className:"bg-white shadow rounded-lg p-6",children:e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"text-red-600 mb-2",children:e.jsx("svg",{className:"mx-auto h-8 w-8",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 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z",clipRule:"evenodd"})})}),e.jsx("h3",{className:"text-sm font-medium text-gray-900",children:"Failed to load contexts"}),e.jsx("p",{className:"text-sm text-gray-500 mt-1",children:"There was an error loading your contexts. Please try again."})]})}):e.jsxs("div",{className:"bg-white shadow rounded-lg",children:[e.jsxs("div",{className:"px-6 py-4 border-b border-gray-200",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-medium text-gray-900",children:"Data Contexts"}),e.jsx("p",{className:"text-sm text-gray-500",children:"Manage your organization's uploaded data contexts"})]}),t&&s&&e.jsxs("button",{onClick:t,className:"inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-primary-500 bg-accent-300 hover:bg-accent-400 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-accent-200",children:[e.jsx("svg",{className:"w-4 h-4 mr-2",fill:"
1currentColor",viewBox:"0 0 20 20",children:e.jsx("path",{fillRule:"evenodd",d:"M10 3a1 1 0 011 1v5h5a1 1 0 110 2h-5v5a1 1 0 11-2 0v-5H4a1 1 0 110-2h5V4a1 1 0 011-1z",clipRule:"evenodd"})}),"Upload Context"]})]}),e.jsxs("div",{className:"mt-4 flex flex-col sm:flex-row gap-4",children:[e.jsx("div",{className:"flex-1",children:e.jsx("input",{type:"text",placeholder:"Search contexts...",value:u,onChange:d=>g(d.target.value),className:"block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"})}),e.jsx("div",{children:e.jsxs("select",{value:o,onChange:d=>l(d.target.value),className:"block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm",children:[e.jsx("option",{value:"all",children:"All Types"}),Object.entries($).map(([d,c])=>e.jsx("option",{value:d,children:c},d))]})})]})]}),e.jsx("div",{className:"divide-y divide-gray-200",children:j.length===0?e.jsxs("div",{className:"p-6 text-center",children:[e.jsx("div",{className:"text-gray-400 mb-4",children:e.jsx("svg",{className:"mx-auto h-12 w-12",stroke:"currentColor",fill:"none",viewBox:"0 0 48 48",children:e.jsx("path",{d:"M34 40h10v-4a6 6 0 00-10.712-3.714M34 40H14m20 0v-4a9.971 9.971 0 00-.712-3.714M14 40H4v-4a6 6 0 0110.713-3.714M14 40v-4c0-1.313.253-2.566.713-3.714m0 0A10.003 10.003 0 0124 26c4.21 0 7.813 2.602 9.288 6.286",strokeWidth:2,strokeLinecap:"round",strokeLinejoin:"round"})})}),e.jsx("h3",{className:"text-sm font-medium text-gray-900",children:"No contexts found"}),e.jsx("p",{className:"text-sm text-gray-500 mt-1",children:u||o!=="all"?"Try adjusting your search or filter criteria.":"Get started by uploading your first data context."}),t&&s&&!u&&o==="all"&&e.jsx("button",{onClick:t,className:"mt-4 inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-indigo-700 bg-indigo-100 hover:bg-indigo-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500",children:"Upload your first context"})]}):j.map(d=>{var c;return e.jsx("div",{onClick:()=>n(d),className:"p-6 hover:bg-gray-50 cursor-pointer transition-colors",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center space-x-3",children:[e.jsx("h3",{className:"text-sm font-medium text-gray-900 truncate",children:d.name}),e.jsx("span",{className:`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${W[d.status]||W.active}`,children:d.status}),e.jsx("span",{className:"inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-800",children:$[d.context_type]||d.context_type})]}),d.description&&e.jsx("p",{className:"text-sm text-gray-500 mt-1 truncate",children:d.description}),e.jsxs("div",{className:"flex items-center space-x-4 mt-2 text-xs text-gray-500",children:[e.jsxs("span",{children:["ð ",d.original_filename]}),e.jsxs("span",{children:["ð ",((c=d.total_rows)==null?void 0:c.toLocaleString())||0," rows"]}),e.jsxs("span",{children:["ð¾ ",d.file_size_display]}),e.jsxs("span",{children:["ð¤ ",d.uploaded_by_name]}),e.jsxs("span",{children:["ð ",ee(new Date(d.uploaded_at),{addSuffix:!0})]})]})]}),e.jsx("div",{className:"flex-shrink-0",children:e.jsx("svg",{className:"w-5 h-5 text-gray-400",fill:"currentColor",viewBox:"0 0 20 20",children:e.jsx("path",{fillRule:"evenodd",d:"M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z",clipRule:"evenodd"})})})]})},d.id)})}),j.length>0&&e.jsx("div",{className:"px-6 py-3 bg-gray-50 border-t border-gray-200",children:e.jsxs("p",{className:"text-xs text-gray-500",children:["Showing ",j.length," of ",m.length," contexts",u&&` matching "${u}"`,o!=="all"&&` of type "${$[o]}"`]})})]}):e.jsx("div",{className:"bg-white shadow rounded-lg p-6",children:e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"text-gray-400 mb-4",children:e.jsx("svg",{className:"mx-auto h-12 w-12",stroke:"currentColor",fill:"none",viewBox:"0 0 48 48",children:e.jsx("path",{d:"M34 40h10v-4a6 6 0 00-10.712-3.714M34 40H14m20 0v-4a9.971 9.971 0 00-.712-3.714M14 40H4v-4a6 6 0 0110.713-3.714M14 40v-4c0-1.313.253-2.566.713-3.714m0 0A10.003 10.003 0 0124 26c4.21 0 7.813 2.602 9.288 6.286",strokeWidth:2,strokeLinecap:"round",strokeLinejoin:"round"})})}),e.jsx("h3",{className:"text-sm font-medium text-gray-900",children:"No organization selected"}),e.jsx("p",{className:"text-sm text-gray-500 mt-1",children:"Please select an organization to view contexts."})]})})},se=({contextId:a,contextName:t,onComplete:s,onError:o,showControls:l=!0})=>{const{taskStatus:u,isLoading:g,error:r,refresh:m}=ke({contextId:a,onComplete:s,onError:o}),{retryProcessing:p,isRetrying:y}
1=De(),{cancelProcessing:j,isCancelling:n}=Fe();if(!u&&g)return e.jsx("div",{className:"bg-white rounded-lg border p-4",children:e.jsxs("div",{className:"animate-pulse",children:[e.jsx("div",{className:"h-4 bg-gray-200 rounded w-1/4 mb-2"}),e.jsx("div",{className:"h-2 bg-gray-200 rounded w-full"})]})});if(!u)return null;const{context:d,processing:c}=u,_=d.status==="processing",M=d.status==="failed",C=()=>{switch(d.status){case"processed":return"text-green-600";case"failed":return"text-red-600";case"processing":return"text-primary-600";default:return"text-gray-600"}},v=()=>{switch(d.status){case"processed":return e.jsx(H,{className:"h-5 w-5 text-green-600"});case"failed":return e.jsx(Q,{className:"h-5 w-5 text-red-600"});case"processing":return e.jsx("div",{className:"h-5 w-5 border-2 border-primary-500 border-t-transparent rounded-full animate-spin"});default:return null}},b=async()=>{await p(a)&&m()},N=async()=>{await j(a)&&m()},h=w=>{if(w===0)return"0 Bytes";const D=1024,R=["Bytes","KB","MB","GB"],P=Math.floor(Math.log(w)/Math.log(D));return parseFloat((w/Math.pow(D,P)).toFixed(2))+" "+R[P]},S=(w,D)=>{if(!w)return null;const R=new Date(w),P=D?new Date(D):new Date,F=Math.round((P.getTime()-R.getTime())/1e3);return F<60?`${F}s`:F<3600?`${Math.floor(F/60)}m ${F%60}s`:`${Math.floor(F/3600)}h ${Math.floor(F%3600/60)}m`};return e.jsxs("div",{className:"bg-white rounded-lg border p-4 space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center space-x-3",children:[v(),e.jsxs("div",{children:[e.jsx("h3",{className:"font-medium text-gray-900",children:t}),e.jsxs("p",{className:"text-sm text-gray-500",children:[d.file_format.toUpperCase()," ⢠",h(d.file_size)]})]})]}),l&&e.jsxs("div",{className:"flex items-center space-x-2",children:[M&&e.jsxs("button",{onClick:b,disabled:y,className:"inline-flex items-center px-3 py-1 border border-gray-300 shadow-sm text-sm leading-4 font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:opacity-50",children:[e.jsx(K,{className:"h-4 w-4 mr-1"}),y?"Retrying...":"Retry"]}),_&&e.jsxs("button",{onClick:N,disabled:n,className:"inline-flex items-center px-3 py-1 border border-red-300 shadow-sm text-sm leading-4 font-medium rounded-md text-red-700 bg-white hover:bg-red-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 disabled:opacity-50",children:[e.jsx(pe,{className:"h-4 w-4 mr-1"}),n?"Cancelling...":"Cancel"]}),e.jsx("button",{onClick:m,disabled:g,className:"inline-flex items-center px-3 py-1 border border-gray-300 shadow-sm text-sm leading-4 font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:opacity-50",children:e.jsx(K,{className:`h-4 w-4 ${g?"animate-spin":""}`})})]})]}),e.jsxs("div",{className:"flex items-center justify-between text-sm",children:[e.jsx("span",{className:`font-medium capitalize ${C()}`,children:d.status.replace("_"," ")}),e.jsx("span",{className:"text-gray-500",children:c.started_at&&S(c.started_at,c.completed_at)})]}),_&&e.jsxs("div",{className:"space-y-2",children:[e.jsx("div",{className:"w-full bg-gray-200 rounded-full h-2",children:e.jsx("div",{className:"bg-primary-500 h-2 rounded-full transition-all duration-300",style:{width:`${Math.max(c.progress,2)}%`}})}),e.jsxs("div",{className:"flex items-center justify-between text-xs text-gray-500",children:[e.jsx("span",{children:c.message}),e.jsxs("span",{children:[c.progress,"%"]})]}),c.current_chunk&&c.total_chunks&&e.jsxs("div",{className:"text-xs text-gray-500",children:["Chunk ",c.current_chunk," of ",c.total_chunks,c.rows_processed&&c.total_rows&&e.jsxs("span",{className:"ml-2",children:["(",c.rows_processed.toLocaleString()," / ",c.total_rows.toLocaleString()," rows)"]})]})]}),M&&c.error&&e.jsx("div",{className:"bg-red-50 border border-red-200 rounded-md p-3",children:e.jsxs("div",{className:"flex",children:[e.jsx(Q,{className:"h-5 w-5 text-red-400"}),e.jsxs("div",{className:"ml-3",children:[e.jsx("h3",{className:"text-sm font-medium text-red-800",children:"Processing Failed"}),e.jsx("div",{className:"mt-1 text-sm text-red-700",children:c.error})]})]})}),d.status==="processed"&&e.jsx("div",{className:"bg-green-50 border border-green-200 rounded-md p-3",children:e.jsxs("div",{className:"flex",children:[e.jsx(H,{className:"h-5 w-5 text-green-400"}),e.jsxs("div",{className:"ml-3",children:[e.jsx("h3",{className:"text-sm font-medium text-green-800",children:"Processing Complete"}),e.jsx("div",{className:"mt-1 text-sm text-green-700",children:c.total_rows&&e.jsxs("span",{children:[c.total_rows.toLocaleStr
1ing()," rows processed successfully"]})})]})]})}),r&&e.jsx("div",{className:"bg-red-50 border border-red-200 rounded-md p-3",children:e.jsxs("div",{className:"flex",children:[e.jsx(ie,{className:"h-5 w-5 text-red-400"}),e.jsxs("div",{className:"ml-3",children:[e.jsx("h3",{className:"text-sm font-medium text-red-800",children:"Connection Error"}),e.jsx("div",{className:"mt-1 text-sm text-red-700",children:r})]})]})})]})},Le=()=>{const[a,t]=x.useState([]),[s,o]=x.useState(!0),[l,u]=x.useState(null),g=async()=>{try{o(!0),u(null);const r=await E.listTemplates();t(r)}catch(r){u(r instanceof Error?r.message:"Failed to fetch templates")}finally{o(!1)}};return x.useEffect(()=>{g()},[]),{templates:a,loading:s,error:l,refetch:g}},Pe=a=>{const[t,s]=x.useState(null),[o,l]=x.useState(!1),[u,g]=x.useState(null);return x.useEffect(()=>{if(!a){s(null);return}(async()=>{try{l(!0),g(null);const m=await E.getTemplate(a);s(m)}catch(m){g(m instanceof Error?m.message:"Failed to fetch template")}finally{l(!1)}})()},[a]),{template:t,loading:o,error:u}},ze=()=>{const[a,t]=x.useState(!1),[s,o]=x.useState(null);return{validateFile:async(g,r)=>{try{t(!0);
1const m=await E.validateFile(g,r);return o(m),m.is_valid&&setTimeout(()=>{console.log("Validation successful, result:",m)},100),m}catch(m){const p=m instanceof Error?m.message:"Validation failed";throw o({is_valid:!1,errors:[p],warnings:[],preview_data:[]}),m}finally{t(!1)}},validating:a,validationResult:s,clearValidation:()=>{o(null)}}},Re=[{value:"client_preferences",label:"Client Preferences"},{value:"booking_history",label:"Booking History"},{value:"travel_policies",label:"Travel Policies"},{value:"expense_data",label:"Expense Data"},{value:"vendor_data",label:"Vendor Data"},{value:"custom",label:"Custom Context"}],Ee=({onUploadSuccess:a,onCancel:t})=>{var I,q;const[s,o]=x.useState({name:"",description:"",context_type:"custom",is_public_in_org:!1,template_id:""}),[l,u]=x.useState(null),[g,r]=x.useState(!1),[m,p]=x.useState(null),[y,j]=x.useState(!1),[n,d]=x.useState(!0),c=Z(),{templates:_,loading:M}=Le(),{template:C}=Pe(n?s.template_id:null),{validateFile:v,validating:b,validationResult:N,clearValidation:h}=ze(),S=he({mutationFn:async i=>n&&s.template_id?E.uploadWithTemplate(i):L.uploadContext(i),onSuccess:i=>{c.invalidateQueries({queryKey:["contexts"]}),c.invalidateQueries({queryKey:["context-summary"]}),i.status==="processing"?(p(i),j(!0)):a&&a(i),o({name:"",description:"",context_type:"custom",is_public_in_org:!1,template_id:""}),u(null),d(!1),h()},onError:i=>{console.error("Upload failed:",i)}}),w=i=>{const{name:f,value:k,type:T}=i.target;o(le=>({...le,[f]:T==="checkbox"?i.target.checked:k})),f==="template_id"&&k&&l&&(h(),v(k,l).catch(console.error))},D=async i=>{if(u(i),h(),!s.name){const f=i.name.replace(/\.[^/.]+$/,"");o(k=>({...k,name:f}))}if(n&&s.template_id)try{await v(s.template_id,i)}catch(f){console.error("File validation failed:",f)}},R=i=>{var k;const f=(k=i.target.files)==null?void 0:k[0];f&&D(f)},P=i=>{i.preventDefault(),i.stopPropagation(),i.type==="dragenter"||i.type==="dragover"?r(!0):i.type==="dragleave"&&r(!1)},F=i=>{var k;i.preventDefault(),i.stopPropagation(),r(!1);const f=(k=i.dataTransfer.files)==null?void 0:k[0];f&&D(f)},te=i=>{if(i.preventDefault(),!l){alert("Please select a file to upload");return}const f=new FormData;f.append("name",s.name),f.append("description",s.description),n||f.append("context_type",s.context_type),f.append("is_public_in_org",s.is_public_in_org.toString()),f.append("uploaded_file",l),n&&s.template_id&&f.append("template_id",s.template_id),S.mutate(f)},A=i=>{var T;const f=[".csv",".xlsx",".xls",".json"],k="."+((T=i.name.split(".").pop())==null?void 0:T.toLowerCase());return f.includes(k)},ae=i=>{if(i===0)return"0 Bytes";const f=1024,k=["Bytes","KB","MB","GB"],T=Math.floor(Math.log(i)/Math.log(f));return parseFloat((i/Math.pow(f,T)).toFixed(2))+" "+k[T]},re=()=>{j(!1),p(null),c.invalidateQueries({queryKey:["contexts"]}),c.invalidateQueries({queryKey:["context-summary"]}),a&&m&&a(m)},ne=i=>{console.error("Processing failed:",i)};return y&&m?e.jsx("div",{className:"space-y-6",children:e.jsxs("div",{className:"bg-white shadow rounded-lg p-6",children:[e.jsxs("div",{className:"mb-4",children:[e.jsx("h2",{className:"text-lg font-medium text-gray-900",children:"File Processing"}),e.jsx("p",{className:"mt-1 text-sm text-gray-500",children:"Your file has been uploaded and is being processed. This may take a few minutes for large files."})]}),e.jsx(se,{contextId:m.id,contextName:m.name,onComplete:re,onError:ne}),e.jsx("div",{className:"mt-4 flex justify-end",children:e.jsx("button",{onClick:()=>{j(!1),p(null)},className:"px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500",children:"Upload Another File"})})]})}):e.jsxs("div",{className:"bg-white shadow rounded-lg p-6",children:[e.jsxs("div",{className:"mb-6",children:[e.jsx("h2",{className:"text-lg font-medium text-gray-900",children:"Upload Context Data"}),e.jsx("p",{className:"mt-1 text-sm text-gray-500",children:"Upload CSV, Excel, or JSON files to create data contexts for your organization."})]}),e.jsx("div",{className:"mb-6 bg-blue-50 border border-blue-200 rounded-lg p-4",children:e.jsxs("div",{className:"flex items-start space-x-3",children:[e.jsx("input",{id:"use_template",type:"checkbox",checked:n,onChange:i=>{d(i.target.checked),i.target.checked||(o(f=>({...f,template_id:""})),h())},className:"mt-1 h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded"}),e.jsxs("div",{className:"flex-1",children:[e.jsx("label",{htmlFor:"use_template",className:"block text-sm font-medium text-blue-900",children:"Use a predefined template (Recommended)"}),e.jsxs("p",{className:"mt-1 text-sm text-blue-700",children:["Templates provide data validation, ensure consistent formatting, and help prevent upload errors.",!n&&" You can still upload without a template, but your data won't be val
1idated."]}),n&&e.jsxs("div",{className:"mt-4 space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"template_id",className:"block text-sm font-medium text-blue-900",children:"Select Template"}),e.jsxs("select",{id:"template_id",name:"template_id",value:s.template_id,onChange:w,className:"mt-1 block w-full px-3 py-2 border border-blue-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm bg-white",disabled:M,children:[e.jsx("option",{value:"",children:"Select a template..."}),_.map(i=>e.jsx("option",{value:i.id,children:i.display_name||i.name},i.id))]}),M&&e.jsx("p",{className:"mt-1 text-xs text-primary-600",children:"Loading templates..."})]}),C&&e.jsxs("div",{className:"bg-white border border-blue-300 rounded-md p-4",children:[e.jsx("h4",{className:"text-sm font-medium text-blue-900 mb-2",children:C.name}),e.jsx("p",{className:"text-sm text-blue-700 mb-3",children:C.description}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{children:[e.jsx("h5",{className:"text-xs font-medium text-blue-900",children:"Required Fields:"}),e.jsx("div",{className:"flex flex-wrap gap-1 mt-1",children:(I=C.schema.required_fields)==null?void 0:I.map(i=>e.jsxs("span",{className:"inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-blue-100 text-blue-800",children:[i.name," (",i.type,")"]},i.name))})]}),C.schema.optional_fields&&C.schema.optional_fields.length>0&&e.jsxs("div",{children:[e.jsx("h5",{className:"text-xs font-medium text-blue-900",children:"Optional Fields:"}),e.jsx("div",{className:"flex flex-wrap gap-1 mt-1",children:C.schema.optional_fields.map(i=>e.jsxs("span",{className:"inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-gray-100 text-gray-700",children:[i.name," (",i.type,")"]},i.name))})]})]}),e.jsx("div",{className:"mt-3 flex items-center space-x-2",children:e.jsx("button",{type:"button",onClick:async()=>{try{await E.downloadSample(C.id)}catch(i){console.error("Failed to download sample file:",i),alert("Failed to download sample file. Please try again.")}},className:"inline-flex items-center px-3 py-1 border border-blue-300 shadow-sm text-xs font-medium rounded text-blue-700 bg-white hover:bg-blue-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500",children:"ð¥ Download Sample File"})})]}),N&&e.jsxs("div",{className:`border rounded-md p-4 ${N.is_valid?"bg-green-50 border-green-200":"bg-red-50 border-red-200"}`,children:[e.jsxs("div",{className:"flex items-center mb-2",children:[N.is_valid?e.jsx("svg",{className:"w-4 h-4 text-green-500 mr-2",fill:"currentColor",viewBox:"0 0 20 20",children:e.jsx("path",{fillRule:"evenodd",d:"M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z",clipRule:"evenodd"})}):e.jsx("svg",{className:"w-4 h-4 text-red-500 mr-2",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 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z",clipRule:"evenodd"})}),e.jsx("h4",{className:`text-sm font-medium ${N.is_valid?"text-green-900":"text-red-900"}`,children:N.is_valid?"File Validation Passed â
":"File Validation Failed â"})]}),N.errors&&N.errors.length>0&&e.jsxs("div",{className:"mb-2",children:[e.jsx("h5",{className:"text-xs font-medium text-red-900 mb-1",children:"Errors:"}),e.jsx("ul",{className:"text-xs text-red-700 space-y-1",children:N.errors.map((i,f)=>e.jsxs("li",{children:["⢠",i]},f))})]}),N.warnings&&N.warnings.length>0&&e.jsxs("div",{className:"mb-2",children:[e.jsx("h5",{className:"text-xs font-medium text-yellow-900 mb-1",children:"Warnings:"}),e.jsx("ul",{className:"text-xs text-yellow-700 space-y-1",children:N.warnings.map((i,f)=>e.jsxs("li",{children:["⢠",i]},f))})]}),N.preview_data&&N.preview_data.length>0&&e.jsxs("div",{children:[e.jsx("h5",{className:"text-xs font-medium text-gray-900 mb-1",children:"Preview (first 3 rows):"}),e.jsx("div",{className:"bg-white border border-gray-200 rounded text-xs overflow-x-auto",children:e.jsxs("table",{className:"min-w-full",children:[e.jsx("thead",{className:"bg-gray-50",children:e.jsx("tr",{children:Object.keys(N.preview_data[0]||{}).map(i=>e.jsx("th",{className:"px-2 py-1 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:i},i))})}),e.jsx("tbody",{className:"bg-white divide-y divide-gray-200",children:N.preview_data.slice(0,3).map((i,f)=>e.jsx("tr",{children:Object.values(i).map((k,T)=>
1e.jsx("td",{className:"px-2 py-1 whitespace-nowrap text-xs text-gray-900",children:String(k)},T))},f))})]})})]})]}),b&&e.jsxs("div",{className:"flex items-center space-x-2 text-sm text-primary-600",children:[e.jsxs("svg",{className:"animate-spin h-4 w-4",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"})]}),e.jsx("span",{children:"Validating file against template..."})]})]}),!n&&e.jsx("div",{className:"mt-3 p-3 bg-yellow-50 border border-yellow-200 rounded-md",children:e.jsxs("div",{className:"flex items-start space-x-2",children:[e.jsx("svg",{className:"w-4 h-4 text-yellow-600 mt-0.5 flex-shrink-0",fill:"currentColor",viewBox:"0 0 20 20",children:e.jsx("path",{fillRule:"evenodd",d:"M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z",clipRule:"evenodd"})}),e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-medium text-yellow-800",children:"Uploading without template"}),e.jsx("p",{className:"text-sm text-yellow-700 mt-1",children:"Your file will be uploaded as-is without validation. Make sure your data is properly formatted. Consider using a template for better data quality and validation."})]})]})})]})]})}),e.jsxs("form",{onSubmit:te,className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:"File Upload"}),e.jsxs("div",{className:`relative border-2 border-dashed rounded-lg p-6 text-center transition-colors ${g?"border-indigo-500 bg-indigo-50":l?"border-green-300 bg-green-50":"border-gray-300 hover:border-gray-400"}`,onDragEnter:P,onDragLeave:P,onDragOver:P,onDrop:F,children:[e.jsx("input",{type:"file",accept:".csv,.xlsx,.xls,.json",onChange:R,className:"absolute inset-0 w-full h-full opacity-0 cursor-pointer"}),l?e.jsxs("div",{className:"space-y-2",children:[e.jsx("div",{className:"flex items-center justify-center",children:e.jsx("svg",{className:"w-8 h-8 text-green-500",fill:"currentColor",viewBox:"0 0 20 20",children:e.jsx("path",{fillRule:"evenodd",d:"M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z",clipRule:"evenodd"})})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-gray-900",children:l.name}),e.jsxs("p",{className:"text-xs text-gray-500",children:[ae(l.size)," ⢠",l.type||"Unknown type"]}),!A(l)&&e.jsx("p",{className:"text-xs text-red-600 mt-1",children:"Invalid file type. Please select CSV, Excel, or JSON files."})]}),e.jsx("button",{type:"button",onClick:()=>u(null),className:"text-sm text-indigo-600 hover:text-indigo-500",children:"Choose different file"})]}):e.jsxs("div",{className:"space-y-2",children:[e.jsx("div",{className:"flex items-center justify-center",children:e.jsx("svg",{className:"w-8 h-8 text-gray-400",stroke:"currentColor",fill:"none",viewBox:"0 0 48 48",children:e.jsx("path",{d:"M28 8H12a4 4 0 00-4 4v20m32-12v8m0 0v8a4 4 0 01-4 4H12a4 4 0 01-4-4v-4m32-4l-3.172-3.172a4 4 0 00-5.656 0L28 28M8 32l9.172-9.172a4 4 0 015.656 0L28 28m0 0l4 4m4-24h8m-4-4v8m-12 4h.02",strokeWidth:2,strokeLinecap:"round",strokeLinejoin:"round"})})}),e.jsxs("div",{children:[e.jsxs("p",{className:"text-sm text-gray-600",children:[e.jsx("span",{className:"font-medium text-indigo-600",children:"Click to upload"})," or drag and drop"]}),e.jsx("p",{className:"text-xs text-gray-500",children:"CSV, Excel (XLSX/XLS), or JSON files up to 50MB"})]})]})]})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-6",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"name",className:"block text-sm font-medium text-gray-700",children:"Context Name *"}),e.jsx("input",{type:"text",id:"name",name:"name",required:!0,value:s.name,onChange:w,className:"mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm",placeholder:"e.g., client_preferences_2024"})]}),!n&&e.jsxs("div",{children:[e.jsx("label",{htmlFor:"context_type",className:"block text-sm font-medium text-gray-700",children:"Context Type"}),e.jsx("select",{id:"context_type",name:"context_type",value:s.context_type,onCh
1ange:w,className:"mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm",children:Re.map(i=>e.jsx("option",{value:i.value,children:i.label},i.value))}),e.jsx("p",{className:"mt-1 text-xs text-gray-500",children:"Categorize your data context for better organization"})]}),n&&C&&e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700",children:"Template Type"}),e.jsx("div",{className:"mt-1 px-3 py-2 border border-gray-300 rounded-md bg-gray-50 text-sm text-gray-700",children:C.display_name||C.name}),e.jsx("p",{className:"mt-1 text-xs text-gray-500",children:"Data structure and validation rules defined by template"})]})]}),e.jsxs("div",{children:[e.jsx("label",{htmlFor:"description",className:"block text-sm font-medium text-gray-700",children:"Description"}),e.jsx("textarea",{id:"description",name:"description",rows:3,value:s.description,onChange:w,className:"mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm",placeholder:"Describe what this context contains..."})]}),e.jsxs("div",{className:"flex items-center",children:[e.jsx("input",{id:"is_public_in_org",name:"is_public_in_org",type:"checkbox",checked:s.is_public_in_org,onChange:w,className:"h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded"}),e.jsx("label",{htmlFor:"is_public_in_org",className:"ml-2 block text-sm text-gray-900",children:"Make this context accessible to all organization members"})]}),e.jsxs("div",{className:"flex justify-end space-x-3",children:[t&&e.jsx("button",{type:"button",onClick:t,className:"px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500",children:"Cancel"}),e.jsx("button",{type:"submit",disabled:S.isPending||!l||!A(l||new File([],""))||n&&(!s.template_id||N!==null&&!N.is_valid),className:"px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-primary-500 bg-accent-300 hover:bg-accent-400 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-accent-200 disabled:opacity-50 disabled:cursor-not-allowed shadow-md",children:S.isPending?"Uploading...":"Upload Context"})]}),S.isError&&e.jsx("div",{className:"rounded-md bg-red-50 p-4",children:e.jsx("div",{className:"flex",children:e.jsxs("div",{className:"ml-3",children:[e.jsx("h3",{className:"text-sm font-medium text-red-800",children:"Upload failed"}),e.jsx("div",{className:"mt-2 text-sm text-red-700",children:((q=S.error)==null?void 0:q.message)||"An error occurred while uploading the file."})]})})})]})]})},G={uploading:"bg-blue-100 text-blue-800",processing:"bg-yellow-100 text-yellow-800",processed:"bg-green-100 text-green-800",failed:"bg-red-100 text-red-800",active:"bg-green-100 text-green-800",archived:"bg-gray-100 text-gray-800"},Be=({contextId:a,onBack:t})=>{const[s,o]=x.useState(1),[l,u]=x.useState(25),[g,r]=x.useState(""),[m,p]=x.useState(!1),[y,j]=x.useState("overview"),{data:n,isLoading:d}=V({queryKey:["context",a],queryFn:()=>L.getContext(a),enabled:!!localStorage.getItem("access_token")}),{data:c,isLoading:_}=V({queryKey:["context-data",a,s,l,g,m],queryFn:()=>L.getContextData(a,s,l,g,m),enabled:y==="data"&&!!localStorage.getItem("access_token")}),M=h=>{o(h)},C=h=>{r(h),o(1)};if(d)return e.jsx("div",{className:"bg-white shadow rounded-lg p-6",children:e.jsxs("div",{className:"animate-pulse space-y-4",children:[e.jsx("div",{className:"h-8 bg-gray-200 rounded w-1/3"}),e.jsx("div",{className:"h-4 bg-gray-200 rounded w-1/2"}),e.jsx("div",{className:"space-y-3",children:[1,2,3].map(h=>e.jsx("div",{className:"h-16 bg-gray-200 rounded"},h))})]})});if(!n)return e.jsx("div",{className:"bg-white shadow rounded-lg p-6",children:e.jsxs("div",{className:"text-center",children:[e.jsx("h3",{className:"text-lg font-medium text-gray-900",children:"Context not found"}),e.jsx("p",{className:"text-sm text-gray-500 mt-1",children:"The requested context could not be found."}),t&&e.jsx("button",{onClick:t,className:"mt-4 inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-indigo-700 bg-indigo-100 hover:bg-indigo-200",children:"Go back"})]})});const v=()=>{var h;return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-6",children:[e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-medium text-gray-900 mb-3",children:"Context Information"}),e.jsxs("dl",{className:"space-y-2",children:[e.jsxs("div",{children:[e.jsx("dt",{className:"text-xs font-medium text-gray-500",children:"Name"}),e.jsx("dd",{className:"text-sm text-gray-900",children:n.name})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-xs font-medium text-gray-500",children:"Type"}),e.jsx("dd",{className:"text-sm text-gray-900",children:n.context_type})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-xs font-medium text-gray-500",children:"Description"}),e.jsx("dd",{className:"text-sm text-gray-900",children:n.description||"No description"})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-xs font-medium text-gray-500",children:"Status"}),e.jsx("dd",{children:e.jsx("span",{className:`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${G[n.status]}`,children:n.status})})]})]})]}),e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-medium text-gray-900 mb-3",children:"File Information"}),e.jsxs("dl",{className:"space-y-2",children:[e.jsxs("div",{children:[e.jsx("dt",{className:"text-xs font-medium text-gray-500",children:"Original Filename"}),e.jsx("dd",{className:"text-sm text-gray-900",children:n.original_filename})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-xs font-medium text-gray-500",children:"File Format"}),e.jsx("dd",{className:"text-sm text-gray-900",children:n.file_format.toUpperCase()})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-xs font-medium text-gray-500",children:"File Size"}),e.jsx("dd",{className:"text-sm text-gray-900",children:n.file_size_display})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-xs font-medium text-gray-500",children:"Uploaded By"}),e.jsx("dd",{className:"text-sm text-gray-900",children:n.uploaded_by_name})]}),e.jsxs("div",{children:[e.jsx("dt",{className:"text-xs font-medium text-gray-500",children:"Uploaded"}),e.jsx("dd",{className:"text-sm text-gray-900",children:ee(new Date(n.uploaded_at),{addSuffix:!0})})]})]})]})]}),n.total_rows!==void 0&&e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-medium text-gray-900 mb-3",children:"Data Statistics"}),e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[e.jsxs("div",{className:"bg-blue-50 p-4 rounded-lg",children:[e.jsx("dt",{className:"text-xs font-medium text-blue-700",children:"Total Rows"}),e.jsx("dd",{className:"text-2xl font-semibold text-blue-900",children:(h=n.total_rows)==null?void 0:h.toLocaleStr
1ing()})]}),e.jsxs("div",{className:"bg-green-50 p-4 rounded-lg",children:[e.jsx("dt",{className:"text-xs font-medium text-green-700",children:"Columns"}),e.jsx("dd",{className:"text-2xl font-semibold text-green-900",children:n.total_columns})]}),e.jsxs("div",{className:"bg-purple-50 p-4 rounded-lg",children:[e.jsx("dt",{className:"text-xs font-medium text-purple-700",children:"Accessibility"}),e.jsx("dd",{className:"text-sm font-semibold text-purple-900",children:n.is_public_in_org?"Public":"Private"})]}),e.jsxs("div",{className:"bg-gray-50 p-4 rounded-lg",children:[e.jsx("dt",{className:"text-xs font-medium text-gray-700",children:"Format"}),e.jsx("dd",{className:"text-sm font-semibold text-gray-900",children:n.file_format.toUpperCase()})]})]})]}),n.column_names&&n.column_names.length>0&&e.jsxs("div",{children:[e.jsxs("h4",{className:"text-sm font-medium text-gray-900 mb-3",children:["Columns (",n.column_names.length,")"]}),e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-2",children:n.column_names.map((S,w)=>e.jsx("div",{className:"bg-gray-50 px-3 py-2 rounded text-sm text-gray-900",children:S},w))})]})]})},b=()=>!n.sample_data||!n.sample_data.rows||n.sample_data.rows.length===0?e.jsx("div",{className:"text-center py-8",children:e.jsx("p",{className:"text-gray-500",children:"No sample data available"})}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs("p",{className:"text-sm text-gray-600",children:["Showing first ",n.sample_data.rows.length," rows as sample data"]}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"min-w-full divide-y divide-gray-200",children:[e.jsx("thead",{className:"bg-gray-50",children:e.jsx("tr",{children:n.sample_data.headers.map((h,S)=>e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:h},S))})}),e.jsx("tbody",{className:"bg-white divide-y divide-gray-200",children:n.sample_data.rows.map((h,S)=>e.jsx("tr",{children:n.sample_data.headers.map((w,D)=>e.jsx("td",{className:"px-6 py-4 whitespace-nowrap text-sm text-gray-900",children:h[w]!==null&&h[w]!==void 0?String(h[w]):"-"},D))},S))})]})})]}),N=()=>_?e.jsx("div",{className:"space-y-4",children:e.jsxs("div",{className:"animate-pulse",children:[e.jsx("div",{className:"h-4 bg-gray-200 rounded w-1/4 mb-4"}),e.jsx("div",{className:"space-y-3",children:[1,2,3,4,5].map(h=>e.jsx("div",{className:"h-8 bg-gray-200 rounded"},h))})]})}):c?e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row gap-4 items-start sm:items-center justify-between",children:[e.jsxs("div",{className:"flex items-center space-x-4",children:[e.jsx("input",{type:"text",placeholder:"Search data...",value:g,onChange:h=>C(h.target.value),className:"block w-64 px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"}),e.jsxs("label",{className:"flex items-center",children:[e.jsx("input",{type:"checkbox",checked:m,onChange:h=>p(h.target.checked),className:"h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded"}),e.jsx("span",{className:"ml-2 text-sm text-gray-900",children:"Valid only"})]})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx("label",{className:"text-sm text-gray-700",children:"Show:"}),e.jsxs("select",{value:l,onChange:h=>u(Number(h.target.value)),className:"block px-3 py-1 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm",children:[e.jsx("option",{value:10,children:"10"}),e.jsx("option",{value:25,children:"25"}),e.jsx("option",{value:50,children:"50"}),e.jsx("option",{value:100,children:"100"})]}),e.jsx("span",{className:"text-sm text-gray-700",children:"per page"})]})]}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"min-w-full divide-y divide-gray-200",children:[e.jsx("thead",{className:"bg-gray-50",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Row #"}),e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:"Status"}),c.context.column_names.map((h,S)=>e.jsx("th",{className:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",children:h},S))]})}),e.jsx("tbody",{className:"bg-white divide-y divide-gray-200",children:c.data.map(h=>e.jsxs("tr",{className:h.is_valid?"":"bg-red-50",children:[e.jsx("td",{className:"px-6 py-4 whitespace-nowrap text-sm text-gray-500",children:h.row_number}),e.jsx("td",{className:"px-6 py-4 whitespace-nowrap",children:h.is_valid?e.jsx("span",{className:"inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800",children:"Valid"}):e.jsx("span",{className:"inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-red-100 text-red-800",children:"Invalid"})}),c.context.column_names.map((S,w)=>e.jsx("td",{className:"px-6 py-4 whitespace-nowrap text-sm text-gray-900",children:h.row_data[S]!==null&&h.row_data[S]!==void 0?String(h.row_data[S]):"-"},w))]}
1,h.id))})]})}),c.pagination.total_pages>1&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"text-sm text-gray-700",children:["Showing ",(c.pagination.current_page-1)*c.pagination.page_size+1," to"," ",Math.min(c.pagination.current_page*c.pagination.page_size,c.pagination.total_count)," of"," ",c.pagination.total_count," results"]}),e.jsxs("div",{className:"flex space-x-2",children:[e.jsx("button",{onClick:()=>M(c.pagination.current_page-1),disabled:!c.pagination.has_previous,className:"px-3 py-1 border border-gray-300 rounded-md text-sm disabled:opacity-50 disabled:cursor-not-allowed hover:bg-gray-50",children:"Previous"}),e.jsxs("span",{className:"px-3 py-1 text-sm text-gray-700",children:["Page ",c.pagination.current_page," of ",c.pagination.total_pages]}),e.jsx("button",{onClick:()=>M(c.pagination.current_page+1),disabled:!c.pagination.has_next,className:"px-3 py-1 border border-gray-300 rounded-md text-sm disabled:opacity-50 disabled:cursor-not-allowed hover:bg-gray-50",children:"Next"})]})]})]}):e.jsx("div",{className:"text-center py-8",children:e.jsx("p",{className:"text-gray-500",children:"No data available"})});return e.jsxs("div",{className:"bg-white shadow rounded-lg",children:[e.jsxs("div",{className:"px-6 py-4 border-b border-gray-200",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center space-x-4",children:[t&&e.jsx("button",{onClick:t,className:"text-gray-400 hover:text-gray-600",children:e.jsx("svg",{className:"w-6 h-6",fill:"currentColor",viewBox:"0 0 20 20",children:e.jsx("path",{fillRule:"evenodd",d:"M7.707 14.707a1 1 0 01-1.414 0L3.586 12l2.707-2.707a1 1 0 011.414 1.414L6.414 12l1.293 1.293a1 1 0 010 1.414z",clipRule:"evenodd"})})}),e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-medium text-gray-900",children:n.name}),e.jsx("p",{className:"text-sm text-gray-500",children:n.description})]})]}),e.jsx("span",{className:`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${G[n.status]}`,children:n.status})]}),e.jsx("div",{className:"mt-4",children:e.jsx("nav",{className:"flex space-x-8",children:[{key:"overview",label:"Overview"},{key:"sample",label:"Sample Data"},{key:"data",label:"Full Data"}].map(h=>e.jsx("button",{onClick:()=>j(h.key),className:`py-2 px-1 border-b-2 font-medium text-sm ${y===h.key?"border-indigo-500 text-indigo-600":"border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300"}`,children:h.label},h.key))})})]}),e.jsxs("div",{className:"p-6",children:[y==="overview"&&v(),y==="sample"&&b(),y==="data"&&N()]})]})},Oe=()=>{const[a,t]=x.useState("list"),[s,o]=x.useState(null),{currentOrganization:l}=J(),{user:u}=ce(),g=Z(),r=de(u==null?void 0:u.user_type),{processingContexts:m}=Me({enabled:!!l&&!!localStorage.getItem("access_token")&&a==="list",pollInterval:8e3,onlyWhenVisible:!0}),{data:p,isLoading:y}=V({queryKey:["context-summary",l==null?void 0:l.id],queryFn:L.getContextSummary,enabled:!!l&&!!localStorage.getItem("access_token")}),j=v=>{o(v),t("detail")},n=v=>{o(v),t("detail")},d=()=>{r&&t("upload")},c=()=>{o(null),t("list")},_=()=>{if(y||!p)return e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-6 mb-8",children:[1,2,3,4].map(b=>e.jsx("div",{className:"bg-white overflow-hidden shadow rounded-lg",children:e.jsx("div",{className:"p-5",children:e.jsxs("div",{className:"animate-pulse",children:[e.jsx("div",{className:"h-4 bg-gray-200 rounded w-1/2 mb-2"}),e.jsx("div",{className:"h-8 bg-gray-200 rounded w-3/4"})]})})},b))});const v=[{name:"Total Contexts",value:p.summary.total_contexts,icon:"ð",color:"blue"},{name:"Total Data Rows",value:p.summary.total_data_rows.toLocaleString(),icon:"ð",color:"green"},{name:"Processing",value:m.size,icon:m.size>0?"â³":"â
",color:m.size>0?"amber":"emerald",animated:m.size>0},{name:"Context Types",value:Object.keys(p.summary.by_type).length,icon:"ð·ï¸",color:"purple"}];return e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-6 mb-8",children:v.map(b=>e.jsx("div",{className:"bg-white overflow-hidden shadow rounded-lg",children:e.jsx("div",{className:"p-5",children:e.jsxs("div",{className:"flex items-center",children:[e.jsx("div",{className:"flex-shrink-0",children:e.jsx("span",{className:`text-2xl ${"animated"in b&&b.animated?"animate-pulse":""}`,children:b.icon})}),e.jsx("div",{className:"ml-5 w-0 flex-1",children:e.jsxs("dl",{children:[e.jsx("dt",{className:"text-sm font-medium text-gray-500 truncate",children:b.name}),e.jsx("dd",{className:`text-lg font-medium text-${b.color}-600`,children:b.value})]})})]})})},b.name))})},M=()=>m.size===0?null:e.jsxs("div",{className:"bg-white shadow rounded-lg mb-8",children:[e.jsx("div",{className:"px-6 py-4 border-b border-gray-200",children:e.jsxs("div",{className:"flex items-center",children:[e.jsx("div",{className:"flex-shrink-0",children:e.jsx("div",{className:"h-3 w-3 border-2 border-primary-500 border-t-transparent roun
1ded-full animate-spin mr-3"})}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-medium text-gray-900",children:"Processing Files"}),e.jsxs("p",{className:"text-sm text-gray-500",children:[m.size," file",m.size===1?"":"s"," currently being processed"]})]})]})}),e.jsx("div",{className:"p-6 space-y-4",children:Array.from(m).map(v=>{const b=p==null?void 0:p.recent_contexts.find(N=>N.id===v);return e.jsx(se,{contextId:v,contextName:(b==null?void 0:b.name)||"Processing File...",showControls:!1,onComplete:()=>{g.invalidateQueries({queryKey:["context-summary"]}),g.invalidateQueries({queryKey:["contexts"]})}},v)})})]}),C=()=>{switch(a){case"upload":return r?e.jsx(Ee,{onUploadSuccess:n,onCancel:c}):(t("list"),null);case"detail":return s?e.jsx(Be,{contextId:s.id,onBack:c}):null;case"list":default:return e.jsxs("div",{className:"space-y-8",children:[_(),M(),e.jsx(Te,{onContextSelect:j,onUploadClick:d,canUpload:r})]})}};return l?e.jsxs("div",{className:"max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8",children:[a!=="list"&&e.jsx("nav",{className:"flex mb-8","aria-label":"Breadcrumb",children:e.jsxs("ol",{className:"flex items-center space-x-4",children:[e.jsx("li",{children:e.jsx("button",{onClick:()=>{c(),typeof window<"u"&&window.innerWidth<1024&&oe.getState().setCollapsed(!1)},className:"text-gray-500 hover:text-gray-700 text-sm",children:"Contexts"})}),e.jsx("li",{children:e.jsx("svg",{className:"flex-shrink-0 h-4 w-4 text-gray-400",fill:"currentColor",viewBox:"0 0 20 20",children:e.jsx("path",{fillRule:"evenodd",d:"M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z",clipRule:"evenodd"})})}),e.jsx("li",{children:e.jsx("span",{className:"text-sm text-gray-900",children:a==="upload"?"Upload Context":s==null?void 0:s.name})})]})}),C()]}):e.jsx("div",{className:"max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8",children:e.jsx("div",{className:"bg-white shadow rounded-lg p-6",children:e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"text-gray-400 mb-4",children:e.jsx("svg",{className:"mx-auto h-12 w-12",stroke:"currentColor",fill:"none",viewBox:"0 0 48 48",children:e.jsx("path",{d:"M34 40h10v-4a6 6 0 00-10.712-3.714M34 40H14m20 0v-4a9.971 9.971 0 00-.712-3.714M14 40H4v-4a6 6 0 0110.713-3.714M14 40v-4c0-1.313.253-2.566.713-3.714m0 0A10.003 10.003 0 0124 26c4.21 0 7.813 2.602 9.288 6.286",strokeWidth:2,strokeLinecap:"round",strokeLinejoin:"round"})})}),e.jsx("h3",{className:"text-sm font-medium text-gray-900",children:"No organization selected"}),e.jsx("p",{className:"text-sm text-gray-500 mt-1",children:"Please select an organization to access context management."})]})})})},He=()=>e.jsxs("div",{className:"space-y-6 px-8 md:px-20 pt-8",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-gray-900",children:"Context Management"}),e.jsx("p",{className:"mt-1 text-sm text-gray-500",children:"Upload, manage, and analyze your organization's contextual data"})]}),e.jsx(Oe,{})]});export{He as ContextsPage};
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.