import{Plugin as n,PendingActions as u}from"@ckeditor/ckeditor5-core";import{ObservableMixin as h,Collection as m,logWarning as p,uid as w,CKEditorError as c}from"@ckeditor/ckeditor5-utils";/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/class g extends h(){total;_reader;_data;constructor(){super();const e=new window.FileReader;this._reader=e,this._data=void 0,this.set("loaded",0),e.onprogress=t=>{this.loaded=t.loaded}}get error(){return this._reader.error}get data(){return this._data}read(e){const t=this._reader;return this.total=e.size,new Promise((s,r)=>{t.onload=()=>{const a=t.result;this._data=a,s(a)},t.onerror=()=>{r("error")},t.onabort=()=>{r("aborted")},this._reader.readAsDataURL(e)})}abort(){this._reader.abort()}}/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/class d extends n{loaders=new m;_loadersMap=new Map;_pendingAction=null;static get pluginName(){return"FileRepository"}static get isOfficialPlugin(){return!0}static get requires(){return[u]}init(){this.loaders.on("change",()=>this._updatePendingAction()),this.set("uploaded",0),this.set("uploadTotal",null),this.bind("uploadedPercent").to(this,"uploaded",this,"uploadTotal",(e,t)=>t?e/t*100:0)}getLoader(e){return this._loadersMap.get(e)||null}createLoader(e){if(!this.createUploadAdapter)return p("filerepository-no-upload-adapter"),null;const t=new f(Promise.resolve(e),this.createUploadAdapter);return this.loaders.add(t),this._loadersMap.set(e,t),e instanceof Promise&&t.file.then(s=>{this._loadersMap.set(s,t)}).catch(()=>{}),t.on("change:uploaded",()=>{let s=0;for(const r of this.loaders)s+=r.uploaded;this.uploaded=s}),t.on("change:uploadTotal",()=>{let s=0;for(const r of this.loaders)r.uploadTotal&&(s+=r.uploadTotal);this.uploadTotal=s}),t}destroyLoader(e){const t=e instanceof f?e:this.getLoader(e);t._destroy(),this.loaders.remove(t),this._loadersMap.forEach((s,r)=>{s===t&&this._loadersMap.delete(r)})}_updatePendingAction(){const e=this.editor.plugins.get(u);if(this.loaders.length){if(!this._pendingAction){const t=this.editor.t,s=r=>`${t("Upload in progress")} ${parseInt(r)}%.`;this._pendingAction=e.add(s(this.uploadedPercent)),this._pendingAction.bind("message").to(this,"uploadedPercent",s)}}else e.remove(this._pendingAction),this._pendingAction=null}}class f extends h(){id;_filePromiseWrapper;_adapter;_reader;constructor(e,t){super(),this.id=w(),this._filePromiseWrapper=this._createFilePromiseWrapper(e),this._adapter=t(this),this._reader=new g,this.set("status","idle"),this.set("uploaded",0),this.set("uploadTotal",null),this.bind("uploadedPercent").to(this,"uploaded",this,"uploadTotal",(s,r)=>r?s/r*100:0),this.set("uploadResponse",null)}get file(){return this._filePromiseWrapper?this._filePromiseWrapper.promise.then(e=>this._filePromiseWrapper?e:null):Promise.resolve(null)}get data(){return this._reader.data}read(){if(this.status!="idle")throw new c("filerepository-read-wrong-status",this);return this.status="reading",this.file.then(e=>this._reader.read(e)).then(e=>{if(this.status!=="reading")throw this.status;return this.status="idle",e}).catch(e=>{throw e==="aborted"?(this.status="aborted","aborted"):(this.status="error",this._reader.error?this._reader.error:e)})}upload(){if(this.status!="idle")throw new c("filerepository-upload-wrong-status",this);return this.status="uploading",this.file.then(()=>this._adapter.upload()).then(e=>(this.uploadResponse=e,this.status="idle",e)).catch(e=>{throw this.status==="aborted"?"aborted":(this.status="error",e)})}abort(){const e=this.status;this.status="aborted",this._filePromiseWrapper.isFulfilled?e=="reading"?this._reader.abort():e=="uploading"&&this._adapter.abort&&this._adapter.abort():(this._filePromiseWrapper.promise.catch(()=>{}),this._filePromiseWrapper.rejecter("aborted")),this._destroy()}_destroy(){this._filePromiseWrapper=void 0,this._reader=void 0,this._adapter=void 0,this.uploadResponse=void 0}_createFilePromiseWrapper(e){const t={};return t.promise=new Promise((s,r)=>{t.rejecter=r,t.isFulfilled=!1,e.then(a=>{t.isFulfilled=!0,s(a)}).catch(a=>{t.isFulfilled=!0,r(a)})}),t}}/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/class P extends n{static get requires(){return[d]}static get pluginName(){return"Base64UploadAdapter"}static get licenseFeatureCode(){return"B64A"}static get isOfficialPlugin(){return!0}static get isPremiumPlugin(){return!0}init(){this.editor.plugins.get(d).createUploadAdapter=e=>new b(e)}}let b=class{loader;reader;constructor(e){this.loader=e}upload(){return new Promise((e,t)=>{const s=this.reader=new window.FileReader;s.addEventListener("load",()=>{e({default:s.result})}),s.addEventListener("error",r=>{t(r)}),s.addEventListener("abort",()=>{t()}),this.loader.file.then(r=>{s.readAsDataURL(r)})})}abort(){this.reader.abort()}};/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/class A extends n{static get requires(){return[d]}static get pluginName(){return"SimpleUploadAdapter"}static get licenseFeatureCode(){return"SUA"}static get isOfficialPlugin(){return!0}static get isPremiumPlugin(){return!0}init(){const e=this.editor.config.get("simpleUpload");if(e){if(!e.uploadUrl){p("simple-upload-adapter-missing-uploadurl");return}this.editor.plugins.get(d).createUploadAdapter=t=>new x(t,e)}}}class x{loader;options;xhr;constructor(e,t){this.loader=e,this.options=t}upload(){return this.loader.file.then(e=>new Promise((t,s)=>{this._initRequest(),this._initListeners(t,s,e),this._sendRequest(e)}))}abort(){this.xhr&&this.xhr.abort()}_initRequest(){const e=this.xhr=new XMLHttpRequest;e.open("POST",this.options.uploadUrl,!0),e.responseType="json"}_initListeners(e,t,s){const r=this.xhr,a=this.loader,l=`Couldn't upload file: ${s.name}.`;r.addEventListener("error",()=>t(l)),r.addEventListener("abort",()=>t()),r.addEventListener("load",()=>{const i=r.response;if(!i||i.error)return t(i&&i.error&&i.error.message?i.error.message:l);const _=i.url?{default:i.url}:i.urls;e({...i,urls:_})});/* istanbul ignore else -- @preserve */r.upload&&r.upload.addEventListener("progress",i=>{i.lengthComputable&&(a.uploadTotal=i.total,a.uploaded=i.loaded)})}_sendRequest(e){let t=this.options.headers||{};typeof t=="function"&&(t=t(e));const s=this.options.withCredentials||!1;for(const a of Object.keys(t))this.xhr.setRequestHeader(a,t[a]);this.xhr.withCredentials=s;const r=new FormData;r.append("upload",e),this.xhr.send(r)}}export{P as Base64UploadAdapter,g as FileReader,d as FileRepository,A as SimpleUploadAdapter};
