function _check_private_redeclaration(obj, privateCollection) {
    if (privateCollection.has(obj)) {
        throw new TypeError("Cannot initialize the same private elements twice on an object");
    }
}
function _class_apply_descriptor_get(receiver, descriptor) {
    if (descriptor.get) {
        return descriptor.get.call(receiver);
    }
    return descriptor.value;
}
function _class_apply_descriptor_set(receiver, descriptor, value) {
    if (descriptor.set) {
        descriptor.set.call(receiver, value);
    } else {
        if (!descriptor.writable) {
            throw new TypeError("attempted to set read only private field");
        }
        descriptor.value = value;
    }
}
function _class_extract_field_descriptor(receiver, privateMap, action) {
    if (!privateMap.has(receiver)) {
        throw new TypeError("attempted to " + action + " private field on non-instance");
    }
    return privateMap.get(receiver);
}
function _class_private_field_get(receiver, privateMap) {
    var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
    return _class_apply_descriptor_get(receiver, descriptor);
}
function _class_private_field_init(obj, privateMap, value) {
    _check_private_redeclaration(obj, privateMap);
    privateMap.set(obj, value);
}
function _class_private_field_set(receiver, privateMap, value) {
    var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
    _class_apply_descriptor_set(receiver, descriptor, value);
    return value;
}
import { addClass, outerWidth, outerHeight } from '../../helpers/dom/element.mjs';
import { mixin } from '../../helpers/object.mjs';
import localHooks from '../../mixins/localHooks.mjs';
import { EditorResizeObserver } from './editorResizeObserver.mjs';
var /**
   * @type {Document}
   */ _rootDocument = /*#__PURE__*/ new WeakMap(), /**
   * @type {HTMLElement}
   */ _rootPortalElement = /*#__PURE__*/ new WeakMap(), /**
   * @type {boolean}
   */ _isRtl = /*#__PURE__*/ new WeakMap(), /**
   * @type {HTMLElement}
   */ _container = /*#__PURE__*/ new WeakMap(), /**
   * @type {HTMLElement}
   */ _editor = /*#__PURE__*/ new WeakMap(), /**
   * @type {CSSStyleDeclaration}
   */ _editorStyle = /*#__PURE__*/ new WeakMap(), /**
   * @type {boolean}
   */ _hidden = /*#__PURE__*/ new WeakMap(), /**
   * @type {EditorResizeObserver}
   */ _resizeObserver = /*#__PURE__*/ new WeakMap();
/**
 * Comment editor for the Comments plugin.
 *
 * @private
 * @class CommentEditor
 */ class CommentEditor {
    static get CLASS_EDITOR_CONTAINER() {
        return 'htCommentsContainer';
    }
    static get CLASS_EDITOR() {
        return 'htComments';
    }
    static get CLASS_INPUT() {
        return 'htCommentTextArea';
    }
    static get CLASS_CELL() {
        return 'htCommentCell';
    }
    /**
   * Set position of the comments editor according to the  provided x and y coordinates.
   *
   * @param {number} x X position (in pixels).
   * @param {number} y Y position (in pixels).
   */ setPosition(x, y) {
        _class_private_field_get(this, _editorStyle).left = `${x}px`;
        _class_private_field_get(this, _editorStyle).top = `${y}px`;
    }
    /**
   * Set the editor size according to the provided arguments.
   *
   * @param {number} width Width in pixels.
   * @param {number} height Height in pixels.
   */ setSize(width, height) {
        if (width && height) {
            const input = this.getInputElement();
            input.style.width = `${width}px`;
            input.style.height = `${height}px`;
        }
    }
    /**
   * Returns the size of the comments editor.
   *
   * @returns {{ width: number, height: number }}
   */ getSize() {
        return {
            width: outerWidth(this.getInputElement()),
            height: outerHeight(this.getInputElement())
        };
    }
    /**
   * Starts observing the editor size.
   */ observeSize() {
        _class_private_field_get(this, _resizeObserver).observe();
    }
    /**
   * Reset the editor size to its initial state.
   */ resetSize() {
        const input = this.getInputElement();
        input.style.width = '';
        input.style.height = '';
    }
    /**
   * Set the read-only state for the comments editor.
   *
   * @param {boolean} state The new read only state.
   */ setReadOnlyState(state) {
        const input = this.getInputElement();
        input.readOnly = state;
    }
    /**
   * Show the comments editor.
   */ show() {
        _class_private_field_get(this, _editorStyle).display = 'block';
        _class_private_field_set(this, _hidden, false);
    }
    /**
   * Hide the comments editor.
   */ hide() {
        _class_private_field_get(this, _resizeObserver).unobserve();
        if (!_class_private_field_get(this, _hidden)) {
            _class_private_field_get(this, _editorStyle).display = 'none';
        }
        _class_private_field_set(this, _hidden, true);
    }
    /**
   * Checks if the editor is visible.
   *
   * @returns {boolean}
   */ isVisible() {
        return _class_private_field_get(this, _editorStyle).display === 'block';
    }
    /**
   * Set the comment value.
   *
   * @param {string} [value] The value to use.
   */ setValue(value = '') {
        const comment = value || '';
        this.getInputElement().value = comment;
    }
    /**
   * Get the comment value.
   *
   * @returns {string}
   */ getValue() {
        return this.getInputElement().value;
    }
    /**
   * Checks if the comment input element is focused.
   *
   * @returns {boolean}
   */ isFocused() {
        return _class_private_field_get(this, _rootDocument).activeElement === this.getInputElement();
    }
    /**
   * Focus the comments input element.
   */ focus() {
        this.getInputElement().focus();
    }
    /**
   * Create the `textarea` to be used as a comments editor.
   *
   * @returns {HTMLElement}
   */ createEditor() {
        const editor = _class_private_field_get(this, _rootDocument).createElement('div');
        const textarea = _class_private_field_get(this, _rootDocument).createElement('textarea');
        editor.style.display = 'none';
        _class_private_field_set(this, _container, _class_private_field_get(this, _rootDocument).createElement('div'));
        _class_private_field_get(this, _container).setAttribute('dir', _class_private_field_get(this, _isRtl) ? 'rtl' : 'ltr');
        addClass(_class_private_field_get(this, _container), CommentEditor.CLASS_EDITOR_CONTAINER);
        _class_private_field_get(this, _rootPortalElement).appendChild(_class_private_field_get(this, _container));
        addClass(editor, CommentEditor.CLASS_EDITOR);
        addClass(textarea, CommentEditor.CLASS_INPUT);
        textarea.setAttribute('data-hot-input', true);
        editor.appendChild(textarea);
        _class_private_field_get(this, _container).appendChild(editor);
        return editor;
    }
    /**
   * Get the input element.
   *
   * @returns {HTMLElement}
   */ getInputElement() {
        return _class_private_field_get(this, _editor).querySelector(`.${CommentEditor.CLASS_INPUT}`);
    }
    /**
   * Get the editor element.
   *
   * @returns {HTMLElement} The editor element.
   */ getEditorElement() {
        return _class_private_field_get(this, _editor);
    }
    /**
   * Destroy the comments editor.
   */ destroy() {
        const containerParentElement = _class_private_field_get(this, _container) ? _class_private_field_get(this, _container).parentNode : null;
        _class_private_field_get(this, _editor).parentNode.removeChild(_class_private_field_get(this, _editor));
        _class_private_field_set(this, _editor, null);
        _class_private_field_set(this, _editorStyle, null);
        _class_private_field_get(this, _resizeObserver).destroy();
        if (containerParentElement) {
            containerParentElement.removeChild(_class_private_field_get(this, _container));
        }
    }
    constructor(rootDocument, isRtl, rootPortalElement){
        _class_private_field_init(this, _rootDocument, {
            writable: true,
            value: void 0
        });
        _class_private_field_init(this, _rootPortalElement, {
            writable: true,
            value: void 0
        });
        _class_private_field_init(this, _isRtl, {
            writable: true,
            value: void 0
        });
        _class_private_field_init(this, _container, {
            writable: true,
            value: void 0
        });
        _class_private_field_init(this, _editor, {
            writable: true,
            value: void 0
        });
        _class_private_field_init(this, _editorStyle, {
            writable: true,
            value: void 0
        });
        _class_private_field_init(this, _hidden, {
            writable: true,
            value: void 0
        });
        _class_private_field_init(this, _resizeObserver, {
            writable: true,
            value: void 0
        });
        _class_private_field_set(this, _isRtl, false);
        _class_private_field_set(this, _container, null);
        _class_private_field_set(this, _hidden, true);
        _class_private_field_set(this, _resizeObserver, new EditorResizeObserver());
        _class_private_field_set(this, _rootDocument, rootDocument);
        _class_private_field_set(this, _rootPortalElement, rootPortalElement);
        _class_private_field_set(this, _isRtl, isRtl);
        _class_private_field_set(this, _editor, this.createEditor());
        _class_private_field_set(this, _editorStyle, _class_private_field_get(this, _editor).style);
        _class_private_field_get(this, _resizeObserver).setObservedElement(this.getInputElement());
        _class_private_field_get(this, _resizeObserver).addLocalHook('resize', (...args)=>this.runLocalHooks('resize', ...args));
        this.hide();
    }
}
mixin(CommentEditor, localHooks);
export default CommentEditor;
