/*!
 * Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see https://ckeditor.com/legal/ckeditor-oss-license
 */
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.CKEditor5=e():(t.CKEditor5=t.CKEditor5||{},t.CKEditor5.importWordAdapter=e())}(self,(()=>(()=>{"use strict";var t={d:(e,i)=>{for(var r in i)t.o(i,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:i[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{default:()=>i});const i={importWordAdapter:class{constructor(t){this.editor=t}init(){if(this.editor.plugins.has("ImageUploadEditing")){this.editor.plugins.get("ImageUploadEditing").on("uploadComplete",((t,{imageElement:e})=>{this.editor.model.change((t=>{t.removeAttribute("htmlAttributes",e)}))}))}}afterInit(){const t=this.editor.config._config.importWord.uploadMedia;t&&t.enabled&&this.handleDataInsert()}replace(t,e){let i=t.getElementsByTagName("img"),r=[];for(let t of i)r.push(t);for(let t of r){if(!this.isBase64ImageData(t.src))continue;const i=this.uploadMediaFromBase64(t.src,e);if(i){let e=document.createElement("drupal-media");e.setAttribute("data-entity-type","media"),e.setAttribute("data-entity-uuid",i),t.replaceWith(e)}}return t.innerHTML}isBase64ImageData(t){return/^data:image\/[^;]+;base64,/.test(t)}handleDataInsert(){this.editor.commands.get("importWord").on("dataInsert",((t,e)=>{const i=this.editor.sourceElement.dataset.editorActiveTextFormat,r=(new DOMParser).parseFromString(e.html,"text/html").body;e.html=this.replace(r,i)}),{priority:"highest"})}uploadMediaFromBase64(t,e){const i=new XMLHttpRequest,r={image:t};if(i.open("POST","/ckeditor5-premium-features/import-word/upload-media/"+e+"/",!1),i.setRequestHeader("Content-Type","application/json;charset=UTF-8"),i.send(JSON.stringify(r)),200===i.status){const t=JSON.parse(i.responseText);if(t.mediaUuid)return t.mediaUuid}}static get pluginName(){return"importWordAdapter"}}};return e=e.default})()));