import{r as F,c as L,l as U}from"./index-BUdRbiwj.js";function O(){const m=F(null),l=e=>{if(m.value=null,!e.name||e.name.trim().length===0)return m.value="Schedule name is required",!1;if(!e.assetId)return m.value="Asset selection is required",!1;if(!e.frequency)return m.value="Frequency is required",!1;if(e.frequencyValue!==void 0){if(e.frequencyValue<=0)return m.value="Frequency value must be greater than 0",!1;if(!E(e.frequency,e.frequencyValue))return m.value="Frequency value exceeds maximum allowed for this frequency type",!1}return e.priority&&!["LOW","MEDIUM","HIGH","CRITICAL"].includes(e.priority)?(m.value="Priority must be LOW, MEDIUM, HIGH, or CRITICAL",!1):e.estimatedDuration!==void 0&&e.estimatedDuration<0?(m.value="Estimated duration must be a positive number",!1):!0},d=(e,n,c=1)=>{if(!e||!n)return null;const t=new Date(e);switch(n.toUpperCase()){case"DAILY":t.setDate(t.getDate()+c);break;case"WEEKLY":t.setDate(t.getDate()+c*7);break;case"MONTHLY":t.setMonth(t.getMonth()+c);break;case"QUARTERLY":t.setMonth(t.getMonth()+c*3);break;case"YEARLY":t.setFullYear(t.getFullYear()+c);break;default:return null}return t},o=e=>{if(e.status==="PAUSED")return"PAUSED";if(e.status==="INACTIVE")return"INACTIVE";if(!e.nextDue)return"SCHEDULED";const n=new Date,t=new Date(e.nextDue).getTime()-n.getTime(),i=Math.ceil(t/(1e3*60*60*24));return i<0?"OVERDUE":i===0?"DUE_TODAY":i<=3?"DUE_SOON":"SCHEDULED"},w=e=>{var c,t,i;const n=new Date().toISOString();return{...e,name:(c=e.name)==null?void 0:c.trim(),instructions:(t=e.instructions)==null?void 0:t.trim(),notes:(i=e.notes)==null?void 0:i.trim(),frequencyValue:e.frequencyValue||1,priority:e.priority||"MEDIUM",estimatedDuration:e.estimatedDuration||30,status:e.status||"ACTIVE",createdAt:e.createdAt||n,updatedAt:n}},u=e=>{const n=e.estimatedDuration||30,c=e.priority||"MEDIUM",i={LOW:15,MEDIUM:20,HIGH:30,CRITICAL:45}[c]||20;return{minDuration:Math.floor(n*.75),maxDuration:Math.ceil(n*1.5),bufferTime:i,recommendedStartTime:8*60}},I=(e,n)=>{if(e.assetId!==n.assetId)return!1;const c=new Date(e.scheduledDate),t=new Date(c.getTime()+e.estimatedDuration*6e4),i=new Date(n.scheduledDate),p=new Date(i.getTime()+n.estimatedDuration*6e4);return c
{if(n<=0)return!1;const t={DAILY:365,WEEKLY:52,MONTHLY:60,QUARTERLY:20,YEARLY:10}[e.toUpperCase()];return t?n<=t:!1},C=e=>{if(!e.nextDue)return 0;const n=new Date,c=new Date(e.nextDue),t=n.getTime()-c.getTime(),i=Math.floor(t/(1e3*60*60*24));return Math.max(0,i)},A=(e,n,c)=>{if(!e||e.length===0)return 0;const t=e.filter(y=>{const v=new Date(y.completedDate);return v>=n&&v<=c}),i=t.filter(y=>y.status==="COMPLETED").length,p=t.length;return p>0?Math.round(i/p*100):0},x=(e,n,c,t=12)=>{const i=[];let p=new Date(e);for(let y=0;ym.value),validateSchedule:l,calculateNextDueDate:d,calculateScheduleStatus:o,processScheduleData:w,calculateMaintenanceWindow:u,isScheduleConflict:I,validateFrequencySettings:E,calculateOverdueDays:C,calculateCompletionRate:A,generateRecurringDates:x}}function R(m=null){const l=m,{showSuccess:d,showError:o,showWarning:w}=U(),u=F(!1);return{isProcessing:u,submitSchedule:async s=>{const{validateForm:r,validateBusinessRules:a,getFormData:h,processScheduleData:D,isEditMode:g,scheduleId:S}=s;try{if(u.value=!0,!r())return o("Please fix validation errors"),{success:!1,error:"Form validation failed"};const f=h();if(!a(f))return o("Business rule validation failed"),{success:!1,error:"Business validation failed"};const M=D(f);let T;return g?(T=await l.updateSchedule(S,M),d("Schedule updated successfully")):(T=await l.createSchedule(M),d("Schedule created successfully")),{success:!0,data:T}}catch(f){return console.error("Schedule submission error:",f),o(`Failed to ${g?"update":"create"} schedule`),{success:!1,error:f.message}}finally{u.value=!1}},handleScheduleDeletion:async s=>{try{return u.value=!0,await l.deleteSchedule(s),d("Schedule deleted successfully"),{success:!0}}catch(r){return console.error("Schedule deletion error:",r),o("Failed to delete schedule"),{success:!1,error:r.message}}finally{u.value=!1}},handleScheduleRefresh:async(s={})=>{try{await l.fetchSchedules(s)}catch(r){console.error("Schedule refresh error:",r),o("Failed to refresh schedules")}},handleWorkOrderGeneration:async(s,r={})=>{try{u.value=!0;const a=await l.generateWorkOrders(s,r);return d(`Generated ${a.length} work order(s)`),{success:!0,workOrders:a}}catch(a){return console.error("Work order generation error:",a),o("Failed to generate work orders"),{success:!1,error:a.message}}finally{u.value=!1}},handleSchedulePause:async(s,r="")=>{try{u.value=!0;const a=await l.pauseSchedule(s,r);return d("Schedule paused successfully"),{success:!0,data:a}}catch(a){return console.error("Schedule pause error:",a),o("Failed to pause schedule"),{success:!1,error:a.message}}finally{u.value=!1}},handleScheduleResume:async s=>{try{u.value=!0;const r=await l.resumeSchedule(s);return d("Schedule resumed successfully"),{success:!0,data:r}}catch(r){return console.error("Schedule resume error:",r),o("Failed to resume schedule"),{success:!1,error:r.message}}finally{u.value=!1}},handleScheduleSearch:async(s,r={})=>{try{const a={search:s,...r};await l.fetchSchedules(a)}catch(a){console.error("Schedule search error:",a),o("Search failed")}},handleSchedulePagination:async s=>{try{await l.fetchSchedules({page:s-1})}catch(r){console.error("Schedule pagination error:",r),o("Failed to load page")}},handleScheduleSorting:async s=>{try{const{column:r,direction:a}=s;await l.fetchSchedules({sortBy:r,direction:a})}catch(r){console.error("Schedule sorting error:",r),o("Failed to apply sorting")}},handleScheduleExport:async(s={})=>{try{u.value=!0;const r=await l.exportSchedules(s);return d("Schedules exported successfully"),{success:!0,data:r}}catch(r){return console.error("Schedule export error:",r),o("Failed to export schedules"),{success:!1,error:r.message}}finally{u.value=!1}},handleBulkStatusUpdate:async(s,r,a="")=>{try{u.value=!0;const h=[];for(const S of s)try{let f;r==="PAUSED"?f=await l.pauseSchedule(S,a):r==="ACTIVE"?f=await l.resumeSchedule(S):f=await l.updateSchedule(S,{status:r,reason:a}),h.push({scheduleId:S,success:!0,result:f})}catch(f){h.push({scheduleId:S,success:!1,error:f.message})}const D=h.filter(S=>S.success).length,g=h.filter(S=>!S.success).length;return g===0?(d(`Successfully updated ${s.length} schedules`),{success:!0,results:h,successCount:D,failureCount:g}):D>0?(w(`Some operations failed. ${D} of ${s.length} schedules updated successfully`),{success:!0,results:h,successCount:D,failureCount:g}):(o("All bulk operations failed"),{success:!1,results:h,successCount:D,failureCount:g})}catch(h){return console.error("Bulk update error:",h),o("Bulk update failed"),{success:!1,error:h.message}}finally{u.value=!1}},handleScheduleLoad:async s=>{try{const r=await l.fetchScheduleById(s);if(!r)throw new Error("Schedule not found");return r}catch(r){throw console.error("Schedule load error:",r),o("Failed to load schedule"),r}},handleScheduleDuplication:async(s,r={})=>{try{u.value=!0;const a=await l.duplicateSchedule(s,r);return d("Schedule duplicated successfully"),{success:!0,data:a}}catch(a){return console.error("Schedule duplication error:",a),o("Failed to duplicate schedule"),{success:!1,error:a.message}}finally{u.value=!1}},handleTemplateCreation:async s=>{try{u.value=!0;const r=await l.createTemplate(s);return d("Template created successfully"),{success:!0,data:r}}catch(r){return console.error("Template creation error:",r),o("Failed to create template"),{success:!1,error:r.message}}finally{u.value=!1}}}}export{R as a,O as u};