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;
};
var IgcExpansionPanelComponent_1;
import { LitElement, html, nothing } from 'lit';
import { property, queryAssignedElements } 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 { themes } from '../../theming/theming-decorator.js';
import { addKeybindings, altKey, arrowDown, arrowUp, } from '../common/controllers/key-bindings.js';
import { registerComponent } from '../common/definitions/register.js';
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
import { createCounter, isEmpty } from '../common/util.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 IgcExpansionPanelComponent = IgcExpansionPanelComponent_1 = class IgcExpansionPanelComponent extends EventEmitterMixin(LitElement) {
    static register() {
        registerComponent(IgcExpansionPanelComponent_1, IgcIconComponent);
    }
    constructor() {
        super();
        this.headerRef = createRef();
        this.contentRef = createRef();
        this.animationPlayer = addAnimationController(this, this.contentRef);
        this.open = false;
        this.disabled = false;
        this.indicatorPosition = 'start';
        addKeybindings(this, {
            ref: this.headerRef,
            skip: () => this.disabled,
            bindingDefaults: { preventDefault: true },
        })
            .setActivateHandler(this.toggleWithEvent)
            .set([altKey, arrowDown], this.openWithEvent)
            .set([altKey, arrowUp], this.closeWithEvent);
    }
    createRenderRoot() {
        const root = super.createRenderRoot();
        root.addEventListener('slotchange', () => this.requestUpdate());
        return root;
    }
    connectedCallback() {
        super.connectedCallback();
        this._panelId =
            this.id ||
                `igc-expansion-panel-${IgcExpansionPanelComponent_1.increment()}`;
    }
    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.animationPlayer.stopAll(),
            this.animationPlayer.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;
    }
    renderIndicatorTemplate() {
        const indicatorHidden = this.open && this._indicatorExpandedElements.length > 0;
        const indicatorExpandedHidden = isEmpty(this._indicatorExpandedElements) || !this.open;
        return html `
      <div part="indicator" aria-hidden="true">
        <slot name="indicator" ?hidden=${indicatorHidden}>
          <igc-icon
            name=${this.open ? 'collapse' : 'expand'}
            collection="default"
          >
          </igc-icon>
        </slot>
        <slot
          name="indicator-expanded"
          ?hidden=${indicatorExpandedHidden}
        ></slot>
      </div>
    `;
    }
    renderHeaderTemplate() {
        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.renderIndicatorTemplate()}
        <div>
          <slot name="title" part="title"></slot>
          <slot name="subtitle" part="subtitle"></slot>
        </div>
      </div>
    `;
    }
    renderContentTemplate() {
        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.renderHeaderTemplate()}${this.renderContentTemplate()}`;
    }
};
IgcExpansionPanelComponent.tagName = 'igc-expansion-panel';
IgcExpansionPanelComponent.styles = [styles, shared];
IgcExpansionPanelComponent.increment = createCounter();
__decorate([
    queryAssignedElements({ slot: 'indicator-expanded' })
], IgcExpansionPanelComponent.prototype, "_indicatorExpandedElements", void 0);
__decorate([
    property({ reflect: true, type: Boolean })
], IgcExpansionPanelComponent.prototype, "open", void 0);
__decorate([
    property({ reflect: true, type: Boolean })
], IgcExpansionPanelComponent.prototype, "disabled", void 0);
__decorate([
    property({ reflect: true, attribute: 'indicator-position' })
], IgcExpansionPanelComponent.prototype, "indicatorPosition", void 0);
IgcExpansionPanelComponent = IgcExpansionPanelComponent_1 = __decorate([
    themes(all)
], IgcExpansionPanelComponent);
export default IgcExpansionPanelComponent;
//# sourceMappingURL=expansion-panel.js.map