/*!
 * Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see https://ckeditor.com/legal/ckeditor-oss-license
 */
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.CKEditor5=t():(e.CKEditor5=e.CKEditor5||{},e.CKEditor5.trackChangesAdapter=t())}(self,(()=>(()=>{"use strict";var e={d:(t,s)=>{for(var i in s)e.o(s,i)&&!e.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:s[i]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{default:()=>i});const s=class{constructor(e){this.editor=e,this.elementId=this.editor.sourceElement.dataset.ckeditor5PremiumElementId}processCollaborationCommandDisable(e){if(!this.isCollaborationDisabled())return!1;const t=this.editor.commands._commands.get(e);return void 0===t||t.forceDisabled("premium-features-module"),!0}processRevisionDisable(){return!!this.isCollaborationDisabled()&&(this.editor.plugins.has("RevisionTracker")&&(this.editor.plugins.get("RevisionTracker").isEnabled=!1),!0)}isCollaborationDisabled(){return void 0!==drupalSettings.ckeditor5Premium&&void 0!==drupalSettings.ckeditor5Premium.disableCollaboration&&!0===drupalSettings.ckeditor5Premium.disableCollaboration}getEditorParentContainer(e){let t=document.getElementById(e);for(;t&&void 0!==t&&void 0!==t.classList&&!t.classList.contains("ck-editor-container");)t=t.parentElement;return t&&void 0!==t?t.parentElement:null}getSourceDataSelector(e){return{trackChanges:".track-changes",comments:".comments",revisionHistory:".revision-history",revisionHistoryContainer:".revision-history-container",resolvedSuggestionsComments:".resolved-suggestions-comments"}[e]+"-data"+`[data-ckeditor5-premium-element-id="${this.elementId}"]`}};const i={TrackChangesAdapter:class{trackedSuggestion;constructor(e){this.editor=e,this.storage=new s(e)}static get pluginName(){return"TrackChangesAdapter"}static get requires(){return["TrackChanges","Comments","TrackChangesAdapter"]}afterInit(){if(!this.editor.plugins.has("Comments")||!this.editor.plugins.has("TrackChanges"))return;if(this.storage.processCollaborationCommandDisable("trackChanges"))return;const e=this.editor.plugins.get("TrackChanges"),t=document.querySelector(this.storage.getSourceDataSelector("trackChanges"));if(!t||""==t.value)return;this.editor.on("ready",(()=>{let e=this.editor.sourceElement.dataset.editorActiveTextFormat,t=drupalSettings.ckeditor5Premium.tracking_changes.default_state;void 0!==t[e]&&t[e]&&this.editor.execute("trackChanges")})),this.trackedSuggestion=new Map;const s=JSON.parse(t.value);for(const t of s)e.addSuggestion(t),this.attachSuggestionEvents(e.getSuggestion(t.id));this.editor.model.document.on("change:data",(()=>{this.updateStorage(e,t)})),this.editor.model.document.on("comments:change:data",(()=>{this.updateStorage(e,t)}));this.editor.sourceElement.closest("form").addEventListener("submit",(()=>{this.updateStorage(e,t)}))}updateStorage(e,t){var s=e.getSuggestions({skipNotAttached:!1});for(let e in s)null==s[e].head||null==s[e].next&&null==s[e].previous||s[e].setAttribute("head",s[e].head.id),this.trackedSuggestion.has(s[e].id)?1==s[e].isInContent?s[e].removeAttribute("status"):void 0===s[e].attributes.status&&this.trackedSuggestion.delete(s[e].id):0!=s[e].isInContent?this.attachSuggestionEvents(s[e]):this.editor.model.document.fire("trackchanges:change:data");t.value=JSON.stringify(Array.from(this.trackedSuggestion.values()))}attachSuggestionEvents(e){this.trackedSuggestion.set(e.id,e);var t=this,s=function(e){let s=t.trackedSuggestion.get(e.source.id);"accept"!==e.name&&"discard"!==e.name||t.updateSuggestionCommentsData(e),void 0!==s&&(void 0!==s.attributes&&void 0!==s.attributes.key?s.setAttribute("status",e.name):t.trackedSuggestion.delete(e.source.id))};e.on("accept",s),e.on("discard",s)}updateSuggestionCommentsData(e){const t=this.editor.plugins.get("CommentsRepository"),s=document.querySelector(this.storage.getSourceDataSelector("resolvedSuggestionsComments"));let i=e.source.id,o=s.value;if(void 0===i||!i)return;o||(o=JSON.stringify([]));let r=Array.from(JSON.parse(o)),a=t.getCommentThread(i).toJSON();!r.some((e=>e.threadId===a.threadId))&&a.comments.length&&(r.push(a),s.value=JSON.stringify(r))}}};return t=t.default})()));