var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { css, customElement, html, styleMap } from '../../../../external/lit/index.js';
import { UmbPropertyEditorUiRteElementBase } from '../../../rte/index.js';
import '../../components/input-tiptap/input-tiptap.element.js';
/**
 * @element umb-property-editor-ui-tiptap
 */
let UmbPropertyEditorUiTiptapElement = class UmbPropertyEditorUiTiptapElement extends UmbPropertyEditorUiRteElementBase {
    firstUpdated(_changedProperties) {
        super.firstUpdated(_changedProperties);
        this.addFormControlElement(this.shadowRoot.querySelector('umb-input-tiptap'));
    }
    #onChange(event) {
        const tipTapElement = event.target;
        const markup = tipTapElement.value;
        // Remove unused Blocks of Blocks Layout. Leaving only the Blocks that are present in Markup.
        const usedContentKeys = [];
        // Regex matching all block elements in the markup, and extracting the content key. It's the same as the one used on the backend.
        const regex = new RegExp(/<umb-rte-block(?:-inline)?(?: class="(?:.[^"]*)")? data-content-key="(?<key>.[^"]*)">(?:<!--Umbraco-Block-->)?<\/umb-rte-block(?:-inline)?>/gi);
        let blockElement;
        while ((blockElement = regex.exec(markup)) !== null) {
            if (blockElement.groups?.key) {
                usedContentKeys.push(blockElement.groups.key);
            }
        }
        if (this.value) {
            this.value = {
                ...this.value,
                markup: markup,
            };
        }
        else {
            this.value = {
                markup: markup,
                blocks: {
                    layout: {},
                    contentData: [],
                    settingsData: [],
                    expose: [],
                },
            };
        }
        // lets run this one after we set the value, to make sure we don't reset the value.
        this._filterUnusedBlocks(usedContentKeys);
        this._fireChangeEvent();
    }
    render() {
        return html `
			<umb-input-tiptap
				style=${styleMap(this._css)}
				.configuration=${this._config}
				.label=${this.name}
				.requiredMessage=${this.mandatoryMessage}
				.value=${this._markup}
				?readonly=${this.readonly}
				?required=${this.mandatory}
				@change=${this.#onChange}></umb-input-tiptap>
		`;
    }
    static { this.styles = css `
		:host(:invalid:not([pristine])) umb-input-tiptap {
			--umb-tiptap-edge-border-color: var(--uui-color-invalid);
		}
	`; }
};
UmbPropertyEditorUiTiptapElement = __decorate([
    customElement('umb-property-editor-ui-tiptap')
], UmbPropertyEditorUiTiptapElement);
export { UmbPropertyEditorUiTiptapElement };
export { UmbPropertyEditorUiTiptapElement as element };
