import { LitElement } from 'lit';
import { A11yState } from './accessibility/a11y.state.enum';
import { A11yProperty } from './accessibility/a11y.property.enum';
import { Role } from './accessibility/role.enum';
export default class NileElement extends LitElement {
    constructor() {
        super(...arguments);
        this.BUBBLES = true;
        this.COMPOSED = true;
        this.CANCELABLE = false;
    }
    /** Emits a custom event with more convenient defaults. */
    emit(name, detail, bubbles, composed, cancelable) {
        const event = new CustomEvent(name, {
            bubbles: bubbles ?? this.BUBBLES,
            cancelable: cancelable ?? this.CANCELABLE,
            composed: composed ?? this.COMPOSED,
            detail
        });
        this.dispatchEvent(event);
        return event;
    }
}
export function setupA11yMetadata(el, options = {}) {
    const role = options[Role.Role];
    if (role && !el.hasAttribute(Role.Role)) {
        el.setAttribute(Role.Role, role);
    }
    const missingLabel = !el.hasAttribute(A11yProperty.AriaLabelledby) && !el.hasAttribute(A11yProperty.AriaLabel);
    if (missingLabel) {
        const fallbackLabel = options[A11yProperty.AriaLabel] ?? options[A11yProperty.AriaLabelledby] ?? (options[A11yProperty.AriaLabelledby] ? el.value : null);
        if (fallbackLabel?.trim()) {
            el.setAttribute(A11yProperty.AriaLabel, fallbackLabel.trim());
        }
    }
    const disabled = options[A11yState.AriaDisabled];
    if (disabled === true) {
        el.setAttribute(A11yState.AriaDisabled, 'true');
    }
    else if (disabled === false) {
        el.removeAttribute(A11yState.AriaDisabled);
    }
    for (const property of Object.values(A11yProperty)) {
        const value = options[property];
        if (value !== undefined && !el.hasAttribute(property)) {
            el.setAttribute(property, String(value));
        }
    }
    for (const state of Object.values(A11yState)) {
        if (state === A11yState.AriaDisabled)
            continue;
        const value = options[state];
        if (value === undefined)
            continue;
        el.setAttribute(state, String(value));
    }
}
function getLabelFromSlots(el) {
    const slot = el.querySelector('[slot="label"]');
    return slot?.textContent?.trim() || null;
}
function getLabelFromText(el) {
    const text = el.textContent?.trim();
    return text || null;
}
//# sourceMappingURL=nile-element.js.map