/**
 * DevExtreme (esm/ui/text_box/ui.text_editor.label.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import $ from "../../core/renderer";
import Guid from "../../core/guid";
import {
    name as click
} from "../../events/click";
import eventsEngine from "../../events/core/events_engine";
import {
    addNamespace
} from "../../events/utils/index";
import {
    start as hoverStart
} from "../../events/hover";
import {
    active
} from "../../events/core/emitter.feedback";
import {
    getWindow
} from "../../core/utils/window";
import {
    getWidth
} from "../../core/utils/size";
var TEXTEDITOR_LABEL_CLASS = "dx-texteditor-label";
var TEXTEDITOR_WITH_LABEL_CLASS = "dx-texteditor-with-label";
var TEXTEDITOR_LABEL_OUTSIDE_CLASS = "dx-texteditor-label-outside";
var TEXTEDITOR_WITH_FLOATING_LABEL_CLASS = "dx-texteditor-with-floating-label";
var TEXTEDITOR_WITH_BEFORE_BUTTONS_CLASS = "dx-texteditor-with-before-buttons";
var LABEL_BEFORE_CLASS = "dx-label-before";
var LABEL_CLASS = "dx-label";
var LABEL_AFTER_CLASS = "dx-label-after";
class TextEditorLabel {
    constructor(props) {
        this.NAME = "dxLabel";
        this._props = props;
        this._id = "".concat(TEXTEDITOR_LABEL_CLASS, "-").concat(new Guid);
        this._render();
        this._toggleMarkupVisibility()
    }
    _isVisible() {
        return !!this._props.text && "hidden" !== this._props.mode
    }
    _render() {
        this._$before = $("<div>").addClass(LABEL_BEFORE_CLASS);
        this._$labelSpan = $("<span>");
        this._$label = $("<div>").addClass(LABEL_CLASS).append(this._$labelSpan);
        this._$after = $("<div>").addClass(LABEL_AFTER_CLASS);
        this._$root = $("<div>").addClass(TEXTEDITOR_LABEL_CLASS).attr("id", this._id).append(this._$before).append(this._$label).append(this._$after);
        this._updateMark();
        this._updateText();
        this._updateBeforeWidth();
        this._updateMaxWidth()
    }
    _toggleMarkupVisibility() {
        var visible = this._isVisible();
        this._updateEditorBeforeButtonsClass(visible);
        this._updateEditorLabelClass(visible);
        visible ? this._$root.appendTo(this._props.$editor) : this._$root.detach();
        this._attachEvents()
    }
    _attachEvents() {
        var clickEventName = addNamespace(click, this.NAME);
        var hoverStartEventName = addNamespace(hoverStart, this.NAME);
        var activeEventName = addNamespace(active, this.NAME);
        eventsEngine.off(this._$labelSpan, clickEventName);
        eventsEngine.off(this._$labelSpan, hoverStartEventName);
        eventsEngine.off(this._$labelSpan, activeEventName);
        if (this._isVisible() && this._isOutsideMode()) {
            eventsEngine.on(this._$labelSpan, clickEventName, e => {
                var selectedText = getWindow().getSelection().toString();
                if ("" === selectedText) {
                    this._props.onClickHandler();
                    e.preventDefault()
                }
            });
            eventsEngine.on(this._$labelSpan, hoverStartEventName, e => {
                this._props.onHoverHandler(e)
            });
            eventsEngine.on(this._$labelSpan, activeEventName, e => {
                this._props.onActiveHandler(e)
            })
        }
    }
    _updateEditorLabelClass(visible) {
        this._props.$editor.removeClass(TEXTEDITOR_WITH_FLOATING_LABEL_CLASS).removeClass(TEXTEDITOR_LABEL_OUTSIDE_CLASS).removeClass(TEXTEDITOR_WITH_LABEL_CLASS);
        if (visible) {
            var labelClass = "floating" === this._props.mode ? TEXTEDITOR_WITH_FLOATING_LABEL_CLASS : TEXTEDITOR_WITH_LABEL_CLASS;
            this._props.$editor.addClass(labelClass);
            if (this._isOutsideMode()) {
                this._props.$editor.addClass(TEXTEDITOR_LABEL_OUTSIDE_CLASS)
            }
        }
    }
    _isOutsideMode() {
        return "outside" === this._props.mode
    }
    _updateEditorBeforeButtonsClass() {
        var visible = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : this._isVisible();
        this._props.$editor.removeClass(TEXTEDITOR_WITH_BEFORE_BUTTONS_CLASS);
        if (visible) {
            var beforeButtonsClass = this._props.containsButtonsBefore ? TEXTEDITOR_WITH_BEFORE_BUTTONS_CLASS : "";
            this._props.$editor.addClass(beforeButtonsClass)
        }
    }
    _updateMark() {
        this._$labelSpan.attr("data-mark", this._props.mark)
    }
    _updateText() {
        this._$labelSpan.text(this._props.text)
    }
    _updateBeforeWidth() {
        if (this._isVisible()) {
            var _this$_props$beforeWi;
            var width = null !== (_this$_props$beforeWi = this._props.beforeWidth) && void 0 !== _this$_props$beforeWi ? _this$_props$beforeWi : this._props.getBeforeWidth();
            this._$before.css({
                width: width
            });
            this._updateLabelTransform()
        }
    }
    _updateLabelTransform() {
        var offset = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : 0;
        this._$labelSpan.css("transform", "");
        if (this._isVisible() && this._isOutsideMode()) {
            var sign = this._props.rtlEnabled ? 1 : -1;
            var labelTranslateX = sign * (getWidth(this._$before) + offset);
            this._$labelSpan.css("transform", "translateX(".concat(labelTranslateX, "px)"))
        }
    }
    _updateMaxWidth() {
        if (this._isVisible() && !this._isOutsideMode()) {
            var _this$_props$containe;
            var maxWidth = null !== (_this$_props$containe = this._props.containerWidth) && void 0 !== _this$_props$containe ? _this$_props$containe : this._props.getContainerWidth();
            this._$label.css({
                maxWidth: maxWidth
            })
        }
    }
    $element() {
        return this._$root
    }
    isVisible() {
        return this._isVisible()
    }
    getId() {
        if (this._isVisible()) {
            return this._id
        }
    }
    updateMode(mode) {
        this._props.mode = mode;
        this._toggleMarkupVisibility();
        this._updateBeforeWidth();
        this._updateMaxWidth()
    }
    updateText(text) {
        this._props.text = text;
        this._updateText();
        this._toggleMarkupVisibility();
        this._updateBeforeWidth();
        this._updateMaxWidth()
    }
    updateMark(mark) {
        this._props.mark = mark;
        this._updateMark()
    }
    updateContainsButtonsBefore(containsButtonsBefore) {
        this._props.containsButtonsBefore = containsButtonsBefore;
        this._updateEditorBeforeButtonsClass()
    }
    updateBeforeWidth(beforeWidth) {
        this._props.beforeWidth = beforeWidth;
        this._updateBeforeWidth()
    }
    updateMaxWidth(containerWidth) {
        this._props.containerWidth = containerWidth;
        this._updateMaxWidth()
    }
}
export {
    TextEditorLabel
};
