"use strict";(self["webpackChunktutor"]=self["webpackChunktutor"]||[]).push([["756"],{43786:function(e,i,t){t.d(i,{fileUploaderMeta:()=>d});/* import */var r=t(12470);/* import */var s=/*#__PURE__*/t.n(r);/* import */var a=t(16131);/* import */var l=t(74412);var n={multiple:false,maxFiles:undefined,accept:".pdf,.doc,.docx,.jpg,.jpeg,.png",maxSize:Number(l/* .tutorConfig.max_upload_size */.P.max_upload_size),variant:"file-uploader",value:[],name:"",required:false,imagePreviewPlaceholder:"",useWPMedia:false,wpMediaTitle:"",wpMediaButtonText:"",wpMediaLibraryType:undefined};var u=e=>{if(!e)return[];if(Array.isArray(e))return e;return[e]};function o(e,i,t){if(e!=="image-uploader"){return t||""}if(typeof i==="string"&&i){return i}if(i&&typeof i==="object"&&!Array.isArray(i)&&"url"in i){return i.url}if(Array.isArray(i)&&i.length>0){var r=i[0];if(typeof r==="string"){return r}if(r&&typeof r==="object"&&"url"in r){return r.url}}return t||""}var h=function(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:n;return{isDragOver:false,isDisabled:e.disabled,multiple:!!e.multiple,maxFiles:e.maxFiles,accept:e.accept,maxSize:e.maxSize||0x3200000,variant:e.variant,imagePreview:o(e.variant,e.value,e.imagePreviewPlaceholder||""),selectedFiles:u(e.value),name:e.name||"",required:e.required||false,useWPMedia:e.useWPMedia||false,wpMediaTitle:e.wpMediaTitle||(0,r.__)("Select File","tutor"),wpMediaButtonText:e.wpMediaButtonText||(0,r.__)("Use this file","tutor"),wpMediaLibraryType:e.wpMediaLibraryType,formatBytes:a/* .formatBytes */.z,$refs:{},init(){this.$refs.fileInput.addEventListener("change",e=>this.handleFileSelect(e));this.setupFormIntegration()},destroy(){this.$refs.fileInput.removeEventListener("change",e=>this.handleFileSelect(e))},handleDragOver(e){e.preventDefault();if(!this.isDisabled){this.isDragOver=true}},handleDragLeave(e){e.preventDefault();this.isDragOver=false},handleDrop(e){var i;e.preventDefault();this.isDragOver=false;if(this.isDisabled)return;var t=Array.from(((i=e.dataTransfer)===null||i===void 0?void 0:i.files)||[]);this.processFiles(t)},handleFileSelect(e){e.preventDefault();var i=e.target;var t=Array.from(i.files||[]);this.processFiles(t);try{i.value=""}catch(e){// InvalidStateError can occur in certain browser states, safe to ignore
}},openFileDialog(){if(this.isDisabled){return}if(this.useWPMedia){this.openWPMediaLibrary()}else{this.$refs.fileInput.click()}},openWPMediaLibrary(){var e;var i=(e=window.TutorCore)===null||e===void 0?void 0:e.wpMedia;if(!(i===null||i===void 0?void 0:i.isAvailable())){this.showError((0,r.__)("WordPress media library is not available","tutor"));return}var t=this.selectedFiles.filter(e=>typeof e==="object"&&"id"in e&&typeof e.id==="number").map(e=>e.id);i.open({title:this.wpMediaTitle,button:{text:this.wpMediaButtonText},multiple:this.multiple,library:this.wpMediaLibraryType?{type:this.wpMediaLibraryType}:undefined,maxFileSize:this.maxSize,maxFiles:this.maxFiles},e=>{this.processFiles(e)},t)},processFiles(i){if(!i.length){return}var t=[];for(var s of i){if(typeof s==="string"){t.push(s);continue}if(this.isWPMedia(s)){t.push(s);continue}if(s.size>this.maxSize){this.showError((0,r.sprintf)((0,r.__)("File %1$s is too large. Maximum allowed size is %2$s.","tutor"),s.name,(0,a/* .formatBytes */.z)(this.maxSize)));continue}if(this.accept&&!this.isFileTypeAccepted(s)){this.showError((0,r.sprintf)((0,r.__)("File type %s is not allowed","tutor"),s.type));continue}t.push(s)}if(t.length>0){if(!this.isFileListChanged(this.selectedFiles,t)){return}if(this.multiple){var l=this.mergeFileLists(this.selectedFiles,t);var n=l.reduce((e,i)=>{if(typeof i==="string"){return 0}return e+Math.round(Number(i===null||i===void 0?void 0:i.size)||0)},0);if(this.maxFiles&&l.length>this.maxFiles){this.showError((0,r.sprintf)((0,r.__)("Cannot select more than %d files","tutor"),this.maxFiles));return}if(n>this.maxSize){this.showError((0,r.sprintf)((0,r.__)("Maximum allowed size is %1$s.","tutor"),(0,a/* .formatBytes */.z)(this.maxSize)));return}this.selectedFiles=l}else{this.selectedFiles=[t[0]]}this.updateFormValue();this.syncFileInput();if(this.variant==="image-uploader"&&this.selectedFiles.length>0){var u=this.selectedFiles[0];if(this.isWPMedia(u)&&u.url){this.imagePreview=u.url}else if(u instanceof File&&u.type.startsWith("image/")){var o=new FileReader;o.onload=e=>{var i;this.imagePreview=(i=e.target)===null||i===void 0?void 0:i.result};o.readAsDataURL(u)}}if(e.onFileSelect){e.onFileSelect(this.selectedFiles)}}},removeFile(i){if(e.onFileRemove&&typeof i==="number"){e.onFileRemove(this.selectedFiles[i])}if(this.multiple&&typeof i==="number"){this.selectedFiles=this.selectedFiles.filter((e,t)=>t!==i)}else{this.selectedFiles=[]}if(this.selectedFiles.length===0){this.imagePreview=""}if(this.$refs.fileInput){try{this.$refs.fileInput.value=""}catch(e){// Safe to ignore
}this.$refs.fileInput.dispatchEvent(new Event("change",{bubbles:true}));this.$refs.fileInput.dispatchEvent(new Event("input",{bubbles:true}))}this.updateFormValue();this.syncFileInput();if(e.onFileSelect){e.onFileSelect(this.selectedFiles)}},mergeFileLists(e,i){var t=new Set;var r=[];var s=e=>{if(!e)return;for(var i of Array.from(e)){var s=this.getFileKey(i);if(t.has(s))continue;t.add(s);r.push(i)}};s(e);s(i);return r},isFileListChanged(e,i){if(e===i){return false}if(!e||!i){return true}if(e.length!==i.length){return true}var t=new Set(Array.from(e,e=>this.getFileKey(e)));return i.some(e=>!t.has(this.getFileKey(e)))},getFileKey(e){if(typeof e==="string"){return e}if(this.isWPMedia(e)){return"wp:".concat(e.id)}return"".concat(e.name,"|").concat(e.size,"|").concat(e.lastModified,"|").concat(e.type)},isWPMedia(e){return typeof e==="object"&&"id"in e&&typeof e.id==="number"},isFileTypeAccepted(e){var i;if(!this.accept)return true;var t=this.accept.split(",").map(e=>e.trim().toLowerCase());var r="."+((i=e.name.split(".").pop())===null||i===void 0?void 0:i.toLowerCase());var s=e.type.toLowerCase();return t.some(e=>{if(e.startsWith(".")){return e===r}return e===s||e===s.split("/")[0]+"/*"})},getFormElement(){var e=this.$el;return e.closest('form[x-data*="tutorForm"], form[x-data*="form("]')},setupFormIntegration(){if(!this.name){return}var e=this.getFormElement();if(!e){return}try{var i=window.Alpine;var t=i===null||i===void 0?void 0:i.$data(e);if(!t||typeof t.register!=="function"){return}this.registerFormField(t);this.syncInitialValue(t);this.watchFormChanges()}catch(e){// eslint-disable-next-line no-console
console.warn((0,r.sprintf)((0,r.__)("Failed to integrate with form: %s","tutor"),e))}},registerFormField(e){var i={numberOnly:false};if(this.required){i.required=this.required}e.register(this.name,i)},syncInitialValue(e){var i;var t=(i=e.values)===null||i===void 0?void 0:i[this.name];var r=t!==undefined&&t!==null&&t!=="";if(r){this.selectedFiles=Array.isArray(t)?t:[t];this.syncFileInput();return}this.updateFormValue({shouldValidate:false,shouldTouch:false,shouldDirty:false})},watchFormChanges(){var e=this;if(!e.$watch){return}e.$watch("values.".concat(this.name),e=>{var i=Array.isArray(e)?e:e?[e]:[];if(!this.isFileListChanged(this.selectedFiles,i)){return}this.selectedFiles=i;this.syncFileInput();if(this.variant==="image-uploader"){this.updateImagePreview()}})},updateImagePreview(){if(this.selectedFiles.length===0){this.imagePreview="";return}var e=this.selectedFiles[0];if(typeof e==="string"){this.imagePreview=e;return}// Handle WPMedia objects
if(this.isWPMedia(e)&&e.url){this.imagePreview=e.url;return}if(e instanceof File&&e.type.startsWith("image/")){var i=new FileReader;i.onload=e=>{var i;this.imagePreview=(i=e.target)===null||i===void 0?void 0:i.result};i.readAsDataURL(e)}},updateFormValue(e){e=e!==null&&e!==void 0?e:{shouldValidate:true,shouldTouch:true,shouldDirty:true};if(!this.name){return}var i=this.getFormElement();if(!i){return}try{var t;var s=(t=window.Alpine)===null||t===void 0?void 0:t.$data(i);if(s&&typeof s.setValue==="function"){var a=this.multiple?this.selectedFiles:this.selectedFiles[0]||null;s.setValue(this.name,a,e)}}catch(e){// eslint-disable-next-line no-console
console.warn((0,r.sprintf)((0,r.__)("Failed to update form value: %s","tutor"),e))}},syncFileInput(){if(this.$refs.fileInput){var e=new DataTransfer;this.selectedFiles.forEach(i=>{if(i instanceof File){e.items.add(i)}});try{this.$refs.fileInput.files=e.files}catch(e){// Safe to ignore
}// Dispatch events to notify listeners (including Alpine's x-model if any)
this.$refs.fileInput.dispatchEvent(new Event("change",{bubbles:true}));this.$refs.fileInput.dispatchEvent(new Event("input",{bubbles:true}))}},showError(i){if(e.onError){e.onError(i)}else{window.TutorCore.toast.error(i)}}}};var d={name:"fileUploader",component:h}}}]);