import { __decorate } from "tslib";
import { attr, observable } from '@ni/fast-element';
import { DesignSystem, FoundationElement } from '@ni/fast-foundation';
import { styles } from './styles';
import { template } from './template';
import { FvSplitButtonAnchorAppearance, FvSplitButtonAnchorAppearanceVariant } from './types';
/**
 * A split button with an anchor-backed primary action and a secondary expandable menu surface.
 */
export class FvSplitButtonAnchor extends FoundationElement {
    constructor() {
        super(...arguments);
        this.label = 'Primary function';
        this.href = '';
        this.target = '';
        this.rel = '';
        this.download = '';
        this.disabled = false;
        this.open = false;
        this.appearance = FvSplitButtonAnchorAppearance.outline;
        this.appearanceVariant = FvSplitButtonAnchorAppearanceVariant.default;
        this.documentClickHandler = (event) => {
            if (!this.open) {
                return;
            }
            if (!event.composedPath().includes(this)) {
                this.setOpen(false);
            }
        };
        this.keydownHandler = (event) => {
            if (event.key === 'Escape') {
                this.setOpen(false);
            }
        };
        this.menuChangeHandler = () => {
            this.handleMenuChange();
        };
    }
    /** @internal */
    disabledChanged() {
        if (this.disabled) {
            this.setOpen(false);
        }
    }
    /** @internal */
    connectedCallback() {
        super.connectedCallback();
        document.addEventListener('click', this.documentClickHandler);
        document.addEventListener('keydown', this.keydownHandler);
    }
    /** @internal */
    disconnectedCallback() {
        document.removeEventListener('click', this.documentClickHandler);
        document.removeEventListener('keydown', this.keydownHandler);
        super.disconnectedCallback();
    }
    /** @internal */
    handlePrimaryClick() {
        if (this.disabled) {
            return;
        }
        this.dispatchEvent(new CustomEvent('trigger', {
            bubbles: true,
            composed: true
        }));
    }
    /** @internal */
    handleToggleClick() {
        if (this.disabled) {
            return;
        }
        this.setOpen(!this.open);
    }
    /** @internal */
    handleMenuChange() {
        this.setOpen(false);
    }
    /** @internal */
    regionChanged(prev, _next) {
        if (prev) {
            prev.removeEventListener('change', this.menuChangeHandler);
        }
        if (this.region) {
            this.region.anchorElement = this.splitButtonContainer ?? this;
            this.region.addEventListener('change', this.menuChangeHandler, { capture: true });
        }
    }
    /** @internal */
    splitButtonContainerChanged() {
        if (this.region) {
            this.region.anchorElement = this.splitButtonContainer ?? this;
        }
    }
    setOpen(nextOpen) {
        if (this.open === nextOpen) {
            return;
        }
        this.open = nextOpen;
        this.dispatchEvent(new CustomEvent('toggle', {
            bubbles: true,
            composed: true,
            detail: { open: this.open }
        }));
    }
}
__decorate([
    attr
], FvSplitButtonAnchor.prototype, "label", void 0);
__decorate([
    attr
], FvSplitButtonAnchor.prototype, "href", void 0);
__decorate([
    attr
], FvSplitButtonAnchor.prototype, "target", void 0);
__decorate([
    attr
], FvSplitButtonAnchor.prototype, "rel", void 0);
__decorate([
    attr
], FvSplitButtonAnchor.prototype, "download", void 0);
__decorate([
    attr({ mode: 'boolean' })
], FvSplitButtonAnchor.prototype, "disabled", void 0);
__decorate([
    attr({ mode: 'boolean' })
], FvSplitButtonAnchor.prototype, "open", void 0);
__decorate([
    attr()
], FvSplitButtonAnchor.prototype, "appearance", void 0);
__decorate([
    attr({ attribute: 'appearance-variant' })
], FvSplitButtonAnchor.prototype, "appearanceVariant", void 0);
__decorate([
    observable
], FvSplitButtonAnchor.prototype, "splitButtonContainer", void 0);
__decorate([
    observable
], FvSplitButtonAnchor.prototype, "region", void 0);
const okFvSplitButtonAnchor = FvSplitButtonAnchor.compose({
    baseName: 'fv-split-button-anchor',
    template,
    styles
});
DesignSystem.getOrCreate().withPrefix('ok').register(okFvSplitButtonAnchor());
export const fvSplitButtonAnchorTag = 'ok-fv-split-button-anchor';
//# sourceMappingURL=index.js.map