import { LitElement, html } from "lit";
import { customElement, state } from "lit/decorators.js";
import { styleMap } from "lit/directives/style-map.js";
import { LightDomMixin } from "../../../mixins/light-dom.js";
import { buildDebouncedQueue } from "../../../utils/debounce-utils.js";
import { randomID } from "../../../utils/utils.js";
function _ts_decorate(decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) 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;
}
function _ts_metadata(k, v) {
    if ("object" == typeof Reflect && "function" == typeof Reflect.metadata) return Reflect.metadata(k, v);
}
class AtomicAriaLive extends LightDomMixin(LitElement) {
    connectedCallback() {
        super.connectedCallback();
        this.ariaLiveId = randomID('atomic-aria-live-');
        document.addEventListener('atomic/accessibility/findAriaLive', this.onFindAriaLive);
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.messagesQueue.clear();
        document.removeEventListener('atomic/accessibility/findAriaLive', this.onFindAriaLive);
    }
    updateMessage(region, message, assertive) {
        const updateRegion = ()=>{
            this.regions = {
                ...this.regions,
                [region]: {
                    assertive,
                    message
                }
            };
        };
        if (message) this.messagesQueue.enqueue(updateRegion, region);
        else {
            this.messagesQueue.cancelActionIfQueued(region);
            updateRegion();
        }
    }
    registerRegion(region, assertive) {
        if (region in this.regions) return;
        this.regions = {
            ...this.regions,
            [region]: {
                assertive,
                message: ''
            }
        };
    }
    render() {
        const hostStyles = {
            position: 'absolute',
            display: 'block',
            height: '0',
            overflow: 'hidden',
            margin: '0'
        };
        return html`
      <div style=${styleMap(hostStyles)}>
        ${Object.entries(this.regions).map(([regionName, { assertive, message }])=>html`
            <div
              id="${this.ariaLiveId}-${regionName}"
              aria-live="${assertive ? 'assertive' : 'polite'}"
              role="status"
            >
              ${message}
            </div>
          `)}
      </div>
    `;
    }
    constructor(...args){
        super(...args), this.regions = {}, this.messagesQueue = buildDebouncedQueue({
            delay: 500
        }), this.onFindAriaLive = (event)=>{
            const customEvent = event;
            customEvent.detail.element = this;
        };
    }
}
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Readonly ? Object : Readonly)
], AtomicAriaLive.prototype, "regions", void 0);
AtomicAriaLive = _ts_decorate([
    customElement('atomic-aria-live')
], AtomicAriaLive);
export { AtomicAriaLive };
