import { __decorate } from "tslib";
import { Observable, observable } from '@ni/fast-element';
import { FoundationElement } from '@ni/fast-foundation';
import { RichTextMention } from '../../rich-text-mention/base';
import { MentionInternals } from '../../rich-text-mention/base/models/mention-internals';
import { Configuration } from '../models/configuration';
import { RichTextUpdateTracker } from '../models/rich-text-tracker';
import { RichTextValidator } from '../models/rich-text-validator';
import { waitUntilCustomElementsDefinedAsync } from '../../utilities/wait-until-custom-elements-defined-async';
/**
 * Base class for rich text components
 */
export class RichText extends FoundationElement {
    constructor() {
        super(...arguments);
        /**
         * @internal
         */
        this.mentionInternalsNotifiers = [];
        /**
         * @internal
         */
        this.childItems = [];
        this.richTextUpdateTracker = new RichTextUpdateTracker(this);
        this.richTextValidator = new RichTextValidator();
    }
    /**
     * @public
     */
    get validity() {
        return this.richTextValidator.getValidity();
    }
    /**
     * @public
     */
    checkValidity() {
        return this.richTextValidator.isValid();
    }
    /**
     * @internal
     */
    handleChange(source, args) {
        if (source instanceof MentionInternals && typeof args === 'string') {
            this.richTextUpdateTracker.trackMentionInternalsPropertyChanged(args);
        }
    }
    /**
     * @internal
     */
    createConfig() {
        this.validate();
        if (this.richTextValidator.isValid()) {
            if (this.richTextUpdateTracker.updateMappingConfigs
                || this.richTextUpdateTracker.updatePattern) {
                this.configuration = new Configuration(this.mentionElements);
            }
        }
        else {
            this.configuration = undefined;
        }
    }
    validate() {
        this.richTextValidator.validate(this.mentionElements);
    }
    childItemsChanged(prev, _next) {
        if (prev !== undefined) {
            void this.updateMentionElementsFromChildItems();
        }
    }
    async updateMentionElementsFromChildItems() {
        await waitUntilCustomElementsDefinedAsync(this.childItems);
        this.mentionElements = this.childItems.filter((x) => x instanceof RichTextMention);
        this.observeMentionInternals();
        this.richTextUpdateTracker.trackMentionElementsInstancesChanged();
    }
    observeMentionInternals() {
        this.removeMentionInternalsObservers();
        for (const mention of this.mentionElements) {
            const notifierInternals = Observable.getNotifier(mention.mentionInternals);
            notifierInternals.subscribe(this);
            this.mentionInternalsNotifiers.push(notifierInternals);
        }
    }
    removeMentionInternalsObservers() {
        this.mentionInternalsNotifiers.forEach(notifier => {
            notifier.unsubscribe(this);
        });
        this.mentionInternalsNotifiers = [];
    }
}
__decorate([
    observable
], RichText.prototype, "childItems", void 0);
__decorate([
    observable
], RichText.prototype, "configuration", void 0);
//# sourceMappingURL=index.js.map