// #region Imports
import { noChange } from 'lit';
import { PartType, directive } from 'lit/directive.js';
import { DirectiveBase } from './Abstracts/DirectiveBase';
import { isReactiveControllerHost } from '../Controllers/Utils';
// #endregion
class AttributeReflectionController {
    static _subscriberCache = new WeakMap();
    _watchedAttributes = new Set();
    _element;
    _host;
    _observer;
    constructor(host, element, attributes) {
        this._host = host;
        this._element = element;
        this._watchedAttributes.add(attributes);
        this._observer = new MutationObserver((mutations) => {
            for (const mutation of mutations) {
                this.notify(mutation.attributeName);
            }
        });
        this.observe();
        host.addController(this);
        AttributeReflectionController._subscriberCache.set(element, this);
    }
    hostConnected() {
        // Implement the hostConnected method from the ReactiveController interface
    }
    hostDisconnected() {
        // Implement the hostDisconnected method from the ReactiveController interface
    }
    observe() {
        const attributeFilter = this._watchedAttributes.values().next().value;
        this._observer.observe(this._element, { attributeFilter });
    }
    notify(attr) {
        if (attr) {
            this._host.requestUpdate();
        }
    }
    static getOrCreateFor(host, element, attributes) {
        return AttributeReflectionController._subscriberCache.get(element) ?? new AttributeReflectionController(host, element, attributes);
    }
}
class ReflectAttributesDirective extends DirectiveBase {
    _attributes;
    _controller;
    constructor(args, attributes) {
        super(args);
        if (args.type !== PartType.ELEMENT) {
            throw new Error('The `reflectAttributes` directive can only be used on elements.');
        }
        if (!isReactiveControllerHost(args.element)) {
            throw new Error('The `reflectAttributes` directive can only be used on reactive controller host elements.');
        }
        this._attributes = attributes;
    }
    render(attributes) {
        return noChange;
    }
    update(part, [attributes]) {
        if (part.options?.host === undefined) {
            throw new Error('The reflectAttributes directive has no host defined');
        }
        this._controller = AttributeReflectionController.getOrCreateFor(part.options.host, part.element, attributes);
        return this.render(attributes);
    }
}
/**
 * @public
 */
export const reflectAttributes = directive(ReflectAttributesDirective);
//# sourceMappingURL=ReflectAttributesDirective.js.map