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 { h } from '@stencil/core';
import { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import { createEnterKeyHandlerToTriggerClick } from '../../../../../utils/accessibility';
export class VegaTabGroupCloseButtonRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /*
     * The below method is e2e-test covered in
     * @see{module:vega-tab-group-call-closeTab-only-one-tab-e2e}
     */
    /* istanbul ignore next */
    this.onCloseTab = (e) => {
      var _a;
      const closeIcon = e.currentTarget;
      const vegaTab = closeIcon.closest('.vega-tab');
      const shouldRestoreTabFocus = ((_a = this.host.shadowRoot) === null || _a === void 0 ? void 0 : _a.activeElement) === closeIcon;
      const closeTabIndex = this.tabItems.findIndex((item) => item.dataTarget === vegaTab.getAttribute('data-target'));
      let nextSelectedDataTarget;
      if (vegaTab.classList.contains('selected')) {
        const searchIndex = closeTabIndex === this.tabItems.length - 1 ? closeTabIndex - 1 : closeTabIndex + 1;
        nextSelectedDataTarget = this.tabItems[searchIndex].dataTarget;
        this.selectedTargetController.setSelectedDataTarget(nextSelectedDataTarget);
      }
      const newTabItems = this.tabItems
        .slice(0, closeTabIndex)
        .concat(this.tabItems.slice(closeTabIndex + 1));
      this.tabPagesController.setTabItems(newTabItems);
      const newTabWidthArray = this.tabWidthArray
        .slice(0, closeTabIndex)
        .concat(this.tabWidthArray.slice(closeTabIndex + 1));
      this.tabPagesController.setTabWidthArray(newTabWidthArray);
      if (shouldRestoreTabFocus && nextSelectedDataTarget && this.host.shadowRoot) {
        const shadowRoot = this.host.shadowRoot;
        requestAnimationFrame(() => {
          const selectedTabLabel = shadowRoot.querySelector(`.vega-tab[data-target='${nextSelectedDataTarget}'] .label`);
          selectedTabLabel === null || selectedTabLabel === void 0 ? void 0 : selectedTabLabel.focus();
        });
      }
      e.stopPropagation();
    };
  }
  render(item) {
    const vegaCloseIconCss = this.showCloseButton
      ? 'vega-close-icon'
      : 'vega-close-icon vega-hidden';
    return (this.showCloseButton &&
      this.tabItems.length > 1 && (h("div", { class: vegaCloseIconCss, onClick: this.onCloseTab, role: 'button', tabIndex: item.disabled ? -1 : 0, onKeyDown: createEnterKeyHandlerToTriggerClick(), "aria-label": 'Close Button' },
      h("vega-icon", { icon: "close", size: "size-12" }))));
  }
}
__decorate([
  MapToComponentField()
], VegaTabGroupCloseButtonRenderer.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaTabGroupCloseButtonRenderer.prototype, "tabWidthArray", void 0);
__decorate([
  MapToComponentField()
], VegaTabGroupCloseButtonRenderer.prototype, "showCloseButton", void 0);
__decorate([
  MapToComponentField()
], VegaTabGroupCloseButtonRenderer.prototype, "tabItems", void 0);
__decorate([
  MapToComponentField({ componentFieldName: 'vegaTabGroupSelectedTargetController' })
], VegaTabGroupCloseButtonRenderer.prototype, "selectedTargetController", void 0);
__decorate([
  MapToComponentField({ componentFieldName: 'vegaTabGroupPagesController' })
], VegaTabGroupCloseButtonRenderer.prototype, "tabPagesController", void 0);
