import { __decorate } from "tslib";
import { attr, Observable, observable } from '@ni/fast-element';
import { FoundationElement } from '@ni/fast-foundation';
import { MentionInternals } from './models/mention-internals';
import { Mapping } from '../../mapping/base';
/**
 * The base class for Mention configuration
 */
export class RichTextMention extends FoundationElement {
    constructor() {
        super(...arguments);
        /**
         * @internal
         */
        this.mentionInternals = new MentionInternals(this.getMentionInternalsOptions(), (filter) => {
            this.emitMentionUpdate(filter);
        });
        /** @internal */
        this.mappingNotifiers = [];
        /** @internal */
        this.mappingElements = [];
    }
    /**
     * @public
     */
    checkValidity() {
        return this.mentionInternals.validator.isValid();
    }
    /**
     * @public
     */
    get validity() {
        return this.mentionInternals.validator.getValidity();
    }
    /**
     * @internal
     */
    emitMentionUpdate(filter) {
        const mentionUpdateEventDetails = {
            filter
        };
        this.$emit('mention-update', mentionUpdateEventDetails);
    }
    /**
     * @internal
     */
    handleChange(source, args) {
        if (source instanceof Mapping
            && typeof args === 'string'
            && this.getObservedMappingProperty().includes(args)) {
            this.updateMappingConfigs();
        }
    }
    getMappingConfigs() {
        const mappingConfigs = new Map();
        this.mappingElements.forEach(mapping => {
            const href = mapping.key ?? undefined;
            if (href === undefined || typeof href !== 'string') {
                throw Error('mentionHref was invalid for type. Validation should have prevented this.');
            }
            const mappingConfig = this.createMappingConfig(mapping);
            mappingConfigs.set(href, mappingConfig);
        });
        return mappingConfigs;
    }
    updateMappingConfigs() {
        this.mentionInternals.validator.validate(this.mappingElements, this.pattern);
        this.mentionInternals.mappingConfigs = this.mentionInternals.validator.isValid()
            ? this.getMappingConfigs()
            : undefined;
    }
    mappingElementsChanged() {
        this.updateMappingConfigs();
        this.observeMappingElements();
    }
    patternChanged() {
        this.mentionInternals.validator.validate(this.mappingElements, this.pattern);
        this.mentionInternals.pattern = this.pattern;
    }
    buttonLabelChanged() {
        this.mentionInternals.buttonLabel = this.buttonLabel;
    }
    removeMappingElementObservers() {
        this.mappingNotifiers.forEach(notifier => {
            notifier.unsubscribe(this);
        });
        this.mappingNotifiers = [];
    }
    observeMappingElements() {
        this.removeMappingElementObservers();
        for (const mapping of this.mappingElements) {
            const notifier = Observable.getNotifier(mapping);
            notifier.subscribe(this);
            this.mappingNotifiers.push(notifier);
        }
    }
}
__decorate([
    attr
], RichTextMention.prototype, "pattern", void 0);
__decorate([
    attr({ attribute: 'button-label' })
], RichTextMention.prototype, "buttonLabel", void 0);
__decorate([
    observable
], RichTextMention.prototype, "mappingElements", void 0);
//# sourceMappingURL=index.js.map