2 lines
24 KiB
JavaScript
2 lines
24 KiB
JavaScript
import{bc as W,aP as O,r as j,c as f}from"./index-QAlcqU00.js";class M extends W{constructor(){super("/documents")}async search(e={}){const t=new URLSearchParams;e.search&&t.append("search",e.search),e.type&&t.append("type",e.type),e.status&&t.append("status",e.status),e.accessLevel&&t.append("accessLevel",e.accessLevel),e.authorId&&t.append("authorId",e.authorId.toString()),e.departmentId&&t.append("departmentId",e.departmentId.toString()),e.page!==void 0&&t.append("page",e.page.toString()),e.size!==void 0&&t.append("size",e.size.toString()),e.sortBy&&t.append("sortBy",e.sortBy),e.direction&&t.append("direction",e.direction),e.startDate&&t.append("startDate",e.startDate),e.endDate&&t.append("endDate",e.endDate),e.expirationStart&&t.append("expirationStart",e.expirationStart),e.expirationEnd&&t.append("expirationEnd",e.expirationEnd);const s=t.toString(),o=s?`/documents?${s}`:"/documents";return this.get(o)}async getById(e){return this.get(`/documents/${e}`)}async create(e){return this.post("/documents",e)}async update(e,t){return this.put(`/documents/${e}`,t)}async deleteDocument(e){return this.delete(e)}async getVersions(e){return this.get(`/documents/${e}/versions`)}async createVersion(e,t){return this.post(`/documents/${e}/versions`,t)}async getVersion(e,t){return this.get(`/documents/${e}/versions/${t}`)}async setActiveVersion(e,t){return this.put(`/documents/${e}/versions/${t}/activate`)}async uploadFile(e,t){const s=new FormData;return s.append("file",t),this.post(`/documents/${e}/upload`,s)}async downloadFile(e){return this.get(`/documents/${e}/download`)}async getFileMetadata(e){return this.get(`/documents/${e}/file-info`)}async getApprovalWorkflow(e){return this.get(`/documents/${e}/approval-workflow`)}async createApprovalWorkflow(e,t){return this.post(`/documents/${e}/approval-workflow`,t)}async submitForApproval(e){return this.post(`/documents/${e}/submit-approval`)}async approveDocument(e,t){return this.post(`/documents/${e}/approve`,t)}async rejectDocument(e,t){return this.post(`/documents/${e}/reject`,t)}async getTemplates(){return this.get("/documents/templates")}async getTemplatesByType(e){return this.get(`/documents/templates?type=${e}`)}async createFromTemplate(e){return this.post("/documents/from-template",e)}async getPermissions(e,t){return this.get(`/documents/${e}/permissions?userId=${t}`)}async updatePermissions(e,t){return this.put(`/documents/${e}/permissions`,t)}async archiveDocument(e,t){return this.post(`/documents/${e}/archive`,t)}async getRetentionPolicy(e){return this.get(`/documents/${e}/retention-policy`)}async setRetentionPolicy(e,t){return this.put(`/documents/${e}/retention-policy`,t)}async fullTextSearch(e,t={}){const s={query:e,includeHighlights:t.includeHighlights??!0};return this.post("/documents/search",s)}async getSearchSuggestions(e){return this.get(`/documents/search/suggestions?q=${encodeURIComponent(e)}`)}async reindexDocument(e){return this.post(`/documents/${e}/reindex`)}async getDocumentPermissions(e){return this.get(`/documents/${e}/permissions`)}async updateDocumentPermissions(e,t){return this.put(`/documents/${e}/permissions`,t)}async checkUserAccess(e,t){return this.get(`/documents/${e}/access/${t}`)}async getUserAccessLevel(e,t){return this.get(`/documents/${e}/access/${t}/level`)}async shareDocument(e,t){return this.post(`/documents/${e}/share`,t)}async revokeAccess(e,t){return this.delete(`/documents/${e}/access/${t}`)}async getRetentionPolicies(){return this.get("/documents/retention/policies")}async createRetentionPolicy(e){return this.post("/documents/retention/policies",e)}async updateRetentionPolicy(e,t){return this.put(`/documents/retention/policies/${e}`,t)}async deleteRetentionPolicy(e){return this.delete(`/documents/retention/policies/${e}`)}async checkRetentionStatus(e){return this.get(`/documents/${e}/retention/status`)}async applyRetentionPolicy(e,t){return this.post(`/documents/${e}/retention/apply`,{policyId:t})}async executeRetentionActions(e={}){return this.post("/documents/retention/execute",e)}async getAnalytics(e){return this.get(`/documents/${e}/analytics`)}async trackView(e,t){return this.post(`/documents/${e}/track-view`,t)}}const p={POLICY:"policy",PROCEDURE:"procedure",MANUAL:"manual",REPORT:"report",CONTRACT:"contract",CERTIFICATE:"certificate",DRAWING:"drawing",SPECIFICATION:"specification",TEMPLATE:"template",OTHER:"other"},u={DRAFT:"draft",PENDING_REVIEW:"pending_review",UNDER_REVIEW:"under_review",APPROVED:"approved",PUBLISHED:"published",ARCHIVED:"archived",EXPIRED:"expired",REJECTED:"rejected"},m={PUBLIC:"public",INTERNAL:"internal",RESTRICTED:"restricted",CONFIDENTIAL:"confidential"},z={LOW:"low",MEDIUM:"medium",HIGH:"high",CRITICAL:"critical"},H={title:{type:"string",required:!0,minLength:3,maxLength:255},description:{type:"string",required:!1,maxLength:1e3},type:{type:"enum",required:!0,values:Object.values(p)},status:{type:"enum",required:!0,values:Object.values(u)},accessLevel:{type:"enum",required:!0,values:Object.values(m)},authorId:{type:"number",required:!0,min:1},departmentId:{type:"number",required:!1,min:1},tags:{type:"array",required:!1,itemType:"string"},expirationDate:{type:"date",required:!1},metadata:{type:"object",required:!1},priority:{type:"enum",required:!1,values:Object.values(z)}},_={documentId:{type:"number",required:!0,min:1},versionNumber:{type:"string",required:!0,pattern:/^\d+\.\d+(\.\d+)?$/},changeDescription:{type:"string",required:!0,minLength:1,maxLength:500},authorId:{type:"number",required:!0,min:1},filePath:{type:"string",required:!1},fileSize:{type:"number",required:!1,min:0},checksum:{type:"string",required:!1},uploadDate:{type:"date",required:!1}},G={documentId:{type:"number",required:!0,min:1},approvers:{type:"array",required:!0,minLength:1},requiredApprovals:{type:"number",required:!1,min:1},autoAdvance:{type:"boolean",required:!1},dueDateDays:{type:"number",required:!1,min:1,max:365}};function v(r,e=H){const t=[];return Object.keys(e).forEach(s=>{const o=e[s],a=r[s];if(o.required&&(a==null||a==="")){const y=s==="documentId"?"Document ID":s==="authorId"?"Author ID":s==="accessLevel"?"Access level":s==="type"?"Document type":s==="versionNumber"?"Version number":s==="changeDescription"?"Change description":s==="approvers"?"Approvers list":s.charAt(0).toUpperCase()+s.slice(1).replace(/([A-Z])/g," $1");t.push(`${y} is required`);return}if(a!=null)switch(o.type){case"string":typeof a!="string"?t.push(`${s} must be a string`):(o.minLength&&a.length<o.minLength&&t.push(`${s.charAt(0).toUpperCase()+s.slice(1)} must be at least ${o.minLength} characters long`),o.maxLength&&a.length>o.maxLength&&t.push(`${s.charAt(0).toUpperCase()+s.slice(1)} must be less than ${o.maxLength} characters`),o.pattern&&!o.pattern.test(a)&&(s==="versionNumber"?t.push("Version number must follow semantic versioning (e.g., 1.0, 1.2.3)"):t.push(`${s} format is invalid`)));break;case"number":typeof a!="number"||isNaN(a)?t.push(`${s} must be a number`):(o.min!==void 0&&a<o.min&&(s==="authorId"?t.push("Author ID must be a positive number"):s==="fileSize"?t.push("File size must be a positive number"):t.push(`${s} must be at least ${o.min}`)),o.max!==void 0&&a>o.max&&t.push(`${s} must be at most ${o.max}`));break;case"enum":o.values.includes(a)||(s==="type"?t.push("Invalid document type"):s==="status"?t.push("Invalid document status"):s==="accessLevel"?t.push("Invalid access level"):t.push(`Invalid ${s}`));break;case"array":Array.isArray(a)?(o.minLength&&a.length<o.minLength&&t.push(`${s} must have at least ${o.minLength} items`),o.itemType==="string"&&s==="tags"&&a.some(d=>typeof d!="string"||d.trim()==="")&&t.push("All tags must be non-empty strings")):t.push(`${s} must be an array`);break;case"date":if(a&&s==="expirationDate"){const y=new Date(a),d=new Date;d.setHours(0,0,0,0),y<d&&t.push("Expiration date cannot be in the past")}break}}),{isValid:t.length===0,errors:t}}function X(r){return v(r,_)}function J(r){const t=[...v(r,G).errors];return r.approvers&&Array.isArray(r.approvers)&&(r.approvers.some(o=>!o.userId||!o.role||!o.order)&&t.push("Each approver must have userId, role, and order"),r.requiredApprovals&&r.requiredApprovals>r.approvers.length&&t.push("Required approvals cannot exceed number of approvers")),{isValid:t.length===0,errors:t}}function Y(r){if(r===0)return"0 B";const e=["B","KB","MB","GB","TB"],t=Math.floor(Math.log(r)/Math.log(1024));return`${(r/Math.pow(1024,t)).toFixed(1)} ${e[t]}`}function te(r){return{[p.POLICY]:"Policy",[p.PROCEDURE]:"Procedure",[p.MANUAL]:"Manual",[p.REPORT]:"Report",[p.CONTRACT]:"Contract",[p.CERTIFICATE]:"Certificate",[p.DRAWING]:"Drawing",[p.SPECIFICATION]:"Specification",[p.TEMPLATE]:"Template",[p.OTHER]:"Other"}[r]||r}function re(r){const e={[u.DRAFT]:{label:"Draft",color:"gray",bgColor:"bg-gray-100",textColor:"text-gray-800"},[u.PENDING_REVIEW]:{label:"Pending Review",color:"yellow",bgColor:"bg-yellow-100",textColor:"text-yellow-800"},[u.UNDER_REVIEW]:{label:"Under Review",color:"blue",bgColor:"bg-blue-100",textColor:"text-blue-800"},[u.APPROVED]:{label:"Approved",color:"green",bgColor:"bg-green-100",textColor:"text-green-800"},[u.PUBLISHED]:{label:"Published",color:"green",bgColor:"bg-green-100",textColor:"text-green-800"},[u.ARCHIVED]:{label:"Archived",color:"gray",bgColor:"bg-gray-100",textColor:"text-gray-800"},[u.EXPIRED]:{label:"Expired",color:"red",bgColor:"bg-red-100",textColor:"text-red-800"},[u.REJECTED]:{label:"Rejected",color:"red",bgColor:"bg-red-100",textColor:"text-red-800"}};return e[r]||e[u.DRAFT]}function se(r){const e={[m.PUBLIC]:{label:"Public",color:"green",icon:"globe",description:"Accessible to everyone"},[m.INTERNAL]:{label:"Internal",color:"blue",icon:"building",description:"Accessible to organization members"},[m.RESTRICTED]:{label:"Restricted",color:"orange",icon:"shield",description:"Accessible to specific roles/departments"},[m.CONFIDENTIAL]:{label:"Confidential",color:"red",icon:"lock",description:"Accessible to authorized personnel only"}};return e[r]||e[m.INTERNAL]}function K(r){if(!r.expirationDate)return!1;const e=new Date(r.expirationDate),t=new Date;return t.setHours(0,0,0,0),e<t}function Z(r,e){if(!r.accessLevel)return!1;switch(r.accessLevel){case m.PUBLIC:return!0;case m.INTERNAL:return!!e;case m.RESTRICTED:return e&&(e.role==="admin"||e.role==="manager");case m.CONFIDENTIAL:return e&&e.role==="admin";default:return!1}}let h=new M;const Q=O("documents",{state:()=>({documents:[],currentDocument:null,loading:!1,error:null,initialized:!1,totalElements:0,totalPages:0,searchCriteria:{page:0,size:10,sortBy:"title",direction:"ASC",search:"",filters:{}},documentVersions:{},documentTemplates:[],approvalWorkflows:{},documentPermissions:{},documentAnalytics:{},searchSuggestions:[]}),getters:{getDocumentById:r=>e=>r.documents.find(t=>t.id===e),publishedDocuments:r=>r.documents.filter(e=>e.status===u.PUBLISHED),draftDocuments:r=>r.documents.filter(e=>e.status===u.DRAFT),pendingReviewDocuments:r=>r.documents.filter(e=>e.status===u.PENDING_REVIEW||e.status===u.UNDER_REVIEW),expiredDocuments:r=>r.documents.filter(e=>e.status===u.EXPIRED),archivedDocuments:r=>r.documents.filter(e=>e.status===u.ARCHIVED),getDocumentsByType:r=>e=>r.documents.filter(t=>t.type===e),getDocumentsByAuthor:r=>e=>r.documents.filter(t=>t.authorId===e),getDocumentsByAccessLevel:r=>e=>r.documents.filter(t=>t.accessLevel===e),expiringSoonDocuments:r=>{const e=new Date;return e.setDate(e.getDate()+30),r.documents.filter(t=>{if(!t.expirationDate||t.status===u.EXPIRED)return!1;const s=new Date(t.expirationDate);return s<=e&&s>new Date})},filteredDocuments:r=>{let e=[...r.documents];if(r.searchCriteria.search){const t=r.searchCriteria.search.toLowerCase();e=e.filter(s=>{var o,a,y,d;return((o=s.title)==null?void 0:o.toLowerCase().includes(t))||((a=s.description)==null?void 0:a.toLowerCase().includes(t))||((y=s.tags)==null?void 0:y.some(D=>D.toLowerCase().includes(t)))||((d=s.authorName)==null?void 0:d.toLowerCase().includes(t))})}return r.searchCriteria.filters.status&&(e=e.filter(t=>t.status===r.searchCriteria.filters.status)),r.searchCriteria.filters.type&&(e=e.filter(t=>t.type===r.searchCriteria.filters.type)),r.searchCriteria.filters.accessLevel&&(e=e.filter(t=>t.accessLevel===r.searchCriteria.filters.accessLevel)),r.searchCriteria.filters.authorId&&(e=e.filter(t=>t.authorId===r.searchCriteria.filters.authorId)),r.searchCriteria.filters.departmentId&&(e=e.filter(t=>t.departmentId===r.searchCriteria.filters.departmentId)),e},documentStats:r=>{const e={total:r.documents.length,published:0,draft:0,pendingReview:0,underReview:0,approved:0,expired:0,archived:0,rejected:0,byType:{},byAccessLevel:{public:0,internal:0,restricted:0,confidential:0},expiringSoon:0},t=new Date;return t.setDate(t.getDate()+30),r.documents.forEach(s=>{switch(s.status){case u.PUBLISHED:e.published++;break;case u.DRAFT:e.draft++;break;case u.PENDING_REVIEW:e.pendingReview++;break;case u.UNDER_REVIEW:e.underReview++;break;case u.APPROVED:e.approved++;break;case u.EXPIRED:e.expired++;break;case u.ARCHIVED:e.archived++;break;case u.REJECTED:e.rejected++;break}const o=s.type||"UNKNOWN";if(e.byType[o]=(e.byType[o]||0)+1,e.byAccessLevel[s.accessLevel]!==void 0&&e.byAccessLevel[s.accessLevel]++,s.expirationDate&&s.status!==u.EXPIRED){const a=new Date(s.expirationDate);a<=t&&a>new Date&&e.expiringSoon++}}),e},documentsRequiringAttention:(r,e)=>[...e.expiringSoonDocuments,...e.pendingReviewDocuments].filter((t,s,o)=>o.findIndex(a=>a.id===t.id)===s),documentVersionsById:r=>e=>r.documentVersions[e]||[],approvalWorkflowById:r=>e=>r.approvalWorkflows[e]||null,documentPermissionsById:r=>e=>r.documentPermissions[e]||null,templatesByType:r=>e=>r.documentTemplates.filter(t=>t.type===e)},actions:{async fetchDocuments(r=null){this.loading=!0,this.error=null;try{const e=r||this.searchCriteria,t=await h.search(e);this.documents=t.content||t.data||t,this.totalElements=t.totalElements||this.documents.length,this.totalPages=t.totalPages||1}catch(e){throw this.error=e.message,e}finally{this.loading=!1}},async fetchDocumentById(r){this.loading=!0,this.error=null;try{const e=await h.getById(r);return this.currentDocument=e,e}catch(e){throw this.error=e.message,e}finally{this.loading=!1}},async createDocument(r){this.loading=!0,this.error=null;try{const e=await h.create(r);return this.documents.push(e),this.currentDocument=e,e}catch(e){throw this.error=e.message,e}finally{this.loading=!1}},async updateDocument(r,e){var t;this.loading=!0,this.error=null;try{const s=await h.update(r,e),o=this.documents.findIndex(a=>a.id===r);return o!==-1&&(this.documents[o]=s),((t=this.currentDocument)==null?void 0:t.id)===r&&(this.currentDocument=s),s}catch(s){throw this.error=s.message,s}finally{this.loading=!1}},async deleteDocument(r){var e;this.loading=!0,this.error=null;try{await h.deleteDocument(r);const t=this.documents.findIndex(s=>s.id===r);t!==-1&&this.documents.splice(t,1),((e=this.currentDocument)==null?void 0:e.id)===r&&(this.currentDocument=null)}catch(t){throw this.error=t.message,t}finally{this.loading=!1}},async getDocumentVersions(r){this.loading=!0,this.error=null;try{const e=await h.getVersions(r);return this.documentVersions[r]=e,e}catch(e){throw this.error=e.message,e}finally{this.loading=!1}},async createDocumentVersion(r,e){this.loading=!0,this.error=null;try{const t=await h.createVersion(r,e);return this.documentVersions[r]||(this.documentVersions[r]=[]),this.documentVersions[r].push(t),t}catch(t){throw this.error=t.message,t}finally{this.loading=!1}},async setActiveDocumentVersion(r,e){var t;this.loading=!0,this.error=null;try{const s=await h.setActiveVersion(r,e);return((t=this.currentDocument)==null?void 0:t.id)===r&&await this.fetchDocumentById(r),s}catch(s){throw this.error=s.message,s}finally{this.loading=!1}},async uploadDocumentFile(r,e){var t;this.loading=!0,this.error=null;try{const s=await h.uploadFile(r,e);return((t=this.currentDocument)==null?void 0:t.id)===r&&await this.fetchDocumentById(r),s}catch(s){throw this.error=s.message,s}finally{this.loading=!1}},async downloadDocumentFile(r){this.loading=!0,this.error=null;try{return await h.downloadFile(r)}catch(e){throw this.error=e.message,e}finally{this.loading=!1}},async getApprovalWorkflow(r){this.loading=!0,this.error=null;try{const e=await h.getApprovalWorkflow(r);return this.approvalWorkflows[r]=e,e}catch(e){throw this.error=e.message,e}finally{this.loading=!1}},async createApprovalWorkflow(r,e){this.loading=!0,this.error=null;try{const t=await h.createApprovalWorkflow(r,e);return this.approvalWorkflows[r]=t,t}catch(t){throw this.error=t.message,t}finally{this.loading=!1}},async submitDocumentForApproval(r){var e;this.loading=!0,this.error=null;try{const t=await h.submitForApproval(r),s=this.documents.find(o=>o.id===r);return s&&(s.status=u.PENDING_REVIEW),((e=this.currentDocument)==null?void 0:e.id)===r&&(this.currentDocument.status=u.PENDING_REVIEW),t}catch(t){throw this.error=t.message,t}finally{this.loading=!1}},async approveDocument(r,e){this.loading=!0,this.error=null;try{const t=await h.approveDocument(r,e);return await this.fetchDocumentById(r),t}catch(t){throw this.error=t.message,t}finally{this.loading=!1}},async rejectDocument(r,e){var t;this.loading=!0,this.error=null;try{const s=await h.rejectDocument(r,e),o=this.documents.find(a=>a.id===r);return o&&(o.status=u.REJECTED),((t=this.currentDocument)==null?void 0:t.id)===r&&(this.currentDocument.status=u.REJECTED),s}catch(s){throw this.error=s.message,s}finally{this.loading=!1}},async fetchDocumentTemplates(){this.loading=!0,this.error=null;try{const r=await h.getTemplates();return this.documentTemplates=r,r}catch(r){throw this.error=r.message,r}finally{this.loading=!1}},async createDocumentFromTemplate(r){this.loading=!0,this.error=null;try{const e=await h.createFromTemplate(r);return this.documents.push(e),this.currentDocument=e,e}catch(e){throw this.error=e.message,e}finally{this.loading=!1}},async getDocumentPermissions(r,e){this.loading=!0,this.error=null;try{const t=await h.getPermissions(r,e);return this.documentPermissions[r]=t,t}catch(t){throw this.error=t.message,t}finally{this.loading=!1}},async updateDocumentPermissions(r,e){this.loading=!0,this.error=null;try{const t=await h.updatePermissions(r,e);return this.documentPermissions[r]=e,t}catch(t){throw this.error=t.message,t}finally{this.loading=!1}},async performFullTextSearch(r,e={}){this.loading=!0,this.error=null;try{return await h.fullTextSearch(r,e)}catch(t){throw this.error=t.message,t}finally{this.loading=!1}},async getSearchSuggestions(r){try{const e=await h.getSearchSuggestions(r);return this.searchSuggestions=e,e}catch(e){return console.warn("Failed to get search suggestions:",e),[]}},async archiveDocument(r,e){var t;this.loading=!0,this.error=null;try{const s=await h.archiveDocument(r,e),o=this.documents.find(a=>a.id===r);return o&&(o.status=u.ARCHIVED),((t=this.currentDocument)==null?void 0:t.id)===r&&(this.currentDocument.status=u.ARCHIVED),s}catch(s){throw this.error=s.message,s}finally{this.loading=!1}},async getDocumentAnalytics(r){this.loading=!0,this.error=null;try{const e=await h.getAnalytics(r);return this.documentAnalytics[r]=e,e}catch(e){throw this.error=e.message,e}finally{this.loading=!1}},async trackDocumentView(r,e){try{return await h.trackView(r,e)}catch(t){console.warn("Failed to track document view:",t)}},async updateSearchTerm(r){this.searchCriteria.search=r,this.searchCriteria.page=0,await this.fetchDocuments()},async updateFilters(r){this.searchCriteria.filters={...this.searchCriteria.filters,...r},this.searchCriteria.page=0,await this.fetchDocuments()},async changePage(r){this.searchCriteria.page=r,await this.fetchDocuments()},async changeSorting(r,e){this.searchCriteria.sortBy=r,this.searchCriteria.direction=e,this.searchCriteria.page=0,await this.fetchDocuments()},async refreshDocuments(){await this.fetchDocuments()},async initializeDocuments(){this.initialized||(await this.fetchDocuments(),await this.fetchDocumentTemplates(),this.initialized=!0)},clearCurrentDocument(){this.currentDocument=null},clearError(){this.error=null},setRepository(r){h=r}}});function ne(){const r=Q(),e=j(null),t=f(()=>r.documents),s=f(()=>r.currentDocument),o=f(()=>r.loading),a=f(()=>r.error),y=f(()=>r.documentStats),d=n=>{e.value=null;const i=v(n),c=i.errors.map(l=>l.includes("Title is required")?"Document title is required":l.includes("Document type is required")?"Document type is required":l.includes("Status is required")?"Document status is required":l.includes("Access level is required")?"Access level is required":l.includes("Author ID is required")?"Author ID is required":l);return{isValid:i.isValid,errors:c}},D=async(n=null)=>{try{await r.fetchDocuments(n)}catch(i){throw console.error("Failed to load documents:",i),i}},C=async n=>{try{return await r.fetchDocumentById(n)}catch(i){throw console.error(`Failed to load document ${n}:`,i),i}},R=async(n,i=null)=>{const c=d(n);if(!c.isValid)return e.value=c.errors.join(", "),null;try{return i?await r.updateDocument(i,n):await r.createDocument(n)}catch(l){throw console.error("Failed to save document:",l),l}},I=async n=>{try{await r.deleteDocument(n)}catch(i){throw console.error(`Failed to remove document ${n}:`,i),i}},w=n=>{e.value=null;const i=X(n),c=i.errors.map(l=>l.includes("Document ID is required")?"Document ID is required":l.includes("Version number is required")?"Version number is required":l.includes("Change description is required")?"Change description is required":l.includes("Author ID is required")?"Author ID is required":l);return{isValid:i.isValid,errors:c}},T=async(n,i)=>{const c={...i,documentId:n},l=w(c);if(!l.isValid)return e.value=l.errors.join(", "),null;try{return await r.createDocumentVersion(n,i)}catch(g){throw console.error(`Failed to create version for document ${n}:`,g),g}},E=n=>{const i=[];if(!n)return i.push("File is required"),{isValid:!1,errors:i};const c=100*1024*1024;return n.size>c&&i.push("File size must be less than 100MB"),{isValid:i.length===0,errors:i}},x=async(n,i)=>{const c=E(i);if(!c.isValid)return e.value=c.errors.join(", "),null;try{return await r.uploadDocumentFile(n,i)}catch(l){throw console.error(`Failed to upload file for document ${n}:`,l),l}},b=n=>{e.value=null;const c=[...J(n).errors];return!n.approvers||!Array.isArray(n.approvers)||n.approvers.length===0?c.push("At least one approver is required"):n.approvers.some(g=>!g.userId||!g.role||!g.order)&&c.push("Each approver must have userId, role, and order"),{isValid:c.length===0,errors:c}},$=async(n,i)=>{const c={...i,documentId:n},l=b(c);if(!l.isValid)return e.value=l.errors.join(", "),null;try{return await r.createApprovalWorkflow(n,i)}catch(g){throw console.error(`Failed to create approval workflow for document ${n}:`,g),g}},P=async n=>{try{return await r.submitDocumentForApproval(n)}catch(i){throw console.error(`Failed to submit document ${n} for approval:`,i),i}},L=async(n,i)=>{try{return await r.approveDocument(n,i)}catch(c){throw console.error(`Failed to approve document ${n}:`,c),c}},V=async(n,i)=>{try{return await r.rejectDocument(n,i)}catch(c){throw console.error(`Failed to reject document ${n}:`,c),c}},S=async n=>{try{return await r.createDocumentFromTemplate(n)}catch(i){throw console.error("Failed to create document from template:",i),i}},A=n=>{const i=[];return(!n.reason||n.reason.trim().length===0)&&i.push("Archive reason is required"),{isValid:i.length===0,errors:i}},F=async(n,i)=>{const c=A(i);if(!c.isValid)return e.value=c.errors.join(", "),null;try{return await r.archiveDocument(n,i)}catch(l){throw console.error(`Failed to archive document ${n}:`,l),l}},q=n=>Y(n),N=(n,i)=>{if(!n||!i)return null;const c=new Date(n);return c.setDate(c.getDate()+i),c},k=n=>K(n),B=(n,i)=>Z(n,i),U=()=>{e.value=null,r.clearError()};return{documents:t,currentDocument:s,loading:o,error:f(()=>a.value||e.value),documentStats:y,loadDocuments:D,loadDocumentById:C,saveDocument:R,removeDocument:I,validateDocument:d,createDocumentVersion:T,validateVersionData:w,uploadDocumentFile:x,validateUploadData:E,validateApprovalWorkflow:b,createApprovalWorkflow:$,submitForApproval:P,approveDocument:L,rejectDocument:V,createFromTemplate:S,archiveDocument:F,validateArchiveData:A,formatFileSize:q,calculateDocumentExpiration:N,isDocumentExpired:k,checkUserAccess:B,clearError:U}}export{M as D,ne as a,p as b,u as c,re as d,m as e,se as f,te as g,Y as h,K as i,Q as u};
|