var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") 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;
};
import { html, LitElement, nothing } from 'lit';
import { property } from 'lit/decorators.js';
import { createRef, ref } from 'lit/directives/ref.js';
import { addAnimationController } from '../../animations/player.js';
import { growVerIn, growVerOut } from '../../animations/presets/grow/index.js';
import { addThemingController } from '../../theming/theming-controller.js';
import { addKeybindings, altKey, arrowDown, arrowUp, } from '../common/controllers/key-bindings.js';
import { addSlotController, setSlots } from '../common/controllers/slot.js';
import { registerComponent } from '../common/definitions/register.js';
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
import IgcIconComponent from '../icon/icon.js';
import { styles } from './themes/expansion-panel.base.css.js';
import { styles as shared } from './themes/shared/expansion-panel.common.css.js';
import { all } from './themes/themes.js';
let nextId = 1;
class IgcExpansionPanelComponent extends EventEmitterMixin(LitElement) {
    static register() {
        registerComponent(IgcExpansionPanelComponent, IgcIconComponent);
    }
    constructor() {
        super();
        this._panelId = `${IgcExpansionPanelComponent.tagName}-${nextId++}`;
        this._headerRef = createRef();
        this._contentRef = createRef();
        this._player = addAnimationController(this, this._contentRef);
        this._slots = addSlotController(this, {
            slots: setSlots('title', 'subtitle', 'indicator', 'indicator-expanded'),
        });
        this.open = false;
        this.disabled = false;
        this.indicatorPosition = 'start';
        addThemingController(this, all);
        addKeybindings(this, {
            ref: this._headerRef,
            skip: () => this.disabled,
        })
            .setActivateHandler(this._toggleWithEvent)
            .set([altKey, arrowDown], this._openWithEvent)
            .set([altKey, arrowUp], this._closeWithEvent);
    }
    connectedCallback() {
        super.connectedCallback();
        this._panelId = this.id || this._panelId;
    }
    _handleClick() {
        this._headerRef.value.focus();
        this._toggleWithEvent();
    }
    _toggleWithEvent() {
        this.open ? this._closeWithEvent() : this._openWithEvent();
    }
    async _toggleAnimation(dir) {
        const animation = dir === 'open' ? growVerIn : growVerOut;
        const [_, event] = await Promise.all([
            this._player.stopAll(),
            this._player.play(animation()),
        ]);
        return event.type === 'finish';
    }
    async _openWithEvent() {
        if (this.open ||
            !this.emitEvent('igcOpening', { cancelable: true, detail: this })) {
            return;
        }
        this.open = true;
        if (await this._toggleAnimation('open')) {
            this.emitEvent('igcOpened', { detail: this });
        }
    }
    async _closeWithEvent() {
        if (!(this.open &&
            this.emitEvent('igcClosing', { cancelable: true, detail: this }))) {
            return;
        }
        this.open = false;
        if (await this._toggleAnimation('close')) {
            this.emitEvent('igcClosed', { detail: this });
        }
    }
    async toggle() {
        return this.open ? this.hide() : this.show();
    }
    async hide() {
        if (!this.open) {
            return false;
        }
        this.open = false;
        await this._toggleAnimation('close');
        return true;
    }
    async show() {
        if (this.open) {
            return false;
        }
        this.open = true;
        await this._toggleAnimation('open');
        return true;
    }
    _renderIndicator() {
        const iconName = this.open ? 'collapse' : 'expand';
        const indicatorHidden = this.open && this._slots.hasAssignedElements('indicator-expanded');
        return html `
      <div part="indicator" aria-hidden="true">
        <slot name="indicator" ?hidden=${indicatorHidden}>
          <igc-icon name=${iconName} collection="default"></igc-icon>
        </slot>
        <slot name="indicator-expanded" ?hidden=${!indicatorHidden}></slot>
      </div>
    `;
    }
    _renderHeader() {
        return html `
      <div
        ${ref(this._headerRef)}
        part="header"
        id="${this._panelId}-header"
        role="button"
        aria-expanded=${this.open}
        aria-disabled=${this.disabled}
        aria-controls="${this._panelId}-content"
        tabindex=${this.disabled ? '-1' : '0'}
        @click=${this.disabled ? nothing : this._handleClick}
      >
        ${this._renderIndicator()}
        <div>
          <slot name="title" part="title"></slot>
          <slot name="subtitle" part="subtitle"></slot>
        </div>
      </div>
    `;
    }
    _renderPanel() {
        return html `
      <div
        ${ref(this._contentRef)}
        part="content"
        role="region"
        id="${this._panelId}-content"
        aria-labelledby="${this._panelId}-header"
        .inert=${!this.open}
        aria-hidden=${!this.open}
      >
        <slot></slot>
      </div>
    `;
    }
    render() {
        return html `${this._renderHeader()}${this._renderPanel()}`;
    }
}
IgcExpansionPanelComponent.tagName = 'igc-expansion-panel';
IgcExpansionPanelComponent.styles = [styles, shared];
export default IgcExpansionPanelComponent;
__decorate([
    property({ type: Boolean, reflect: true })
], IgcExpansionPanelComponent.prototype, "open", void 0);
__decorate([
    property({ type: Boolean, reflect: true })
], IgcExpansionPanelComponent.prototype, "disabled", void 0);
__decorate([
    property({ reflect: true, attribute: 'indicator-position' })
], IgcExpansionPanelComponent.prototype, "indicatorPosition", void 0);
//# sourceMappingURL=expansion-panel.js.map