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 { TableSettingButtonSlimmer } from './table-setting-button-slimmer.abstract';
import { DomNodeSubjectObserverFactory } from '../../../../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaDropdownClick, VegaDropdownHide, VegaDropdownShow, } from '../../../../../../../helpers/event-manager/event-id/vega-event-id';
import { ColumnOperationType } from '../../../setting-event-handlers/column-operation';
import { TableSettingType } from '../../../setting-event-handlers/setting-event-handler.abstract';
import { MapToComponentField } from 'vega-slimmer/core';
import { ToggleHeaderTableColumnStrategy } from '../../../action-handler-strategies/toggle-header-table-column-strategy';
import { getParentBlockByType, getSelectedCellsBoundary, getTableColumnCount } from '../../../utils/utils';
import { RTETableBlock } from '../../../dto/table-block';
/**
 * Slimmer dedicated to building the column button.
 */
export class ColumnButtonSlimmer extends TableSettingButtonSlimmer {
  constructor() {
    super({
      icon: 'rte-table-column',
      title: 'Column',
      showArrowIcon: true,
    });
    this.handleDropdownItemClick = (input) => {
      this.emitClickEvent([TableSettingType.COLUMN, input.detail]);
    };
    this.handleDropdownVisibilityChange = ({ eventName }) => {
      this.isColumnDropdownOpen = eventName.toLocaleLowerCase().indexOf('show') >= 0;
    };
  }
  renderBtnWrapper(renderBtn) {
    return (h("vega-dropdown", { isScreenPosition: true, minWidth: 210, trigger: "click", ref: (el) => {
        DomNodeSubjectObserverFactory.addUniqueObserverToNode(el, VegaDropdownClick, this.handleDropdownItemClick);
        DomNodeSubjectObserverFactory.addUniqueObserverToNode(el, VegaDropdownShow, this.handleDropdownVisibilityChange);
        DomNodeSubjectObserverFactory.addUniqueObserverToNode(el, VegaDropdownHide, this.handleDropdownVisibilityChange);
      } },
      renderBtn(),
      this.renderActionList()));
  }
  isSelected() {
    return this.isColumnDropdownOpen;
  }
  isDisabled() {
    return this.selectedCells.length === 0;
  }
  /* istanbul ignore next */
  // eslint-disable-next-line @typescript-eslint/no-empty-function
  handleClick() { }
  /**
   * Render the action list.
   *
   * @returns {HTMLElement} The action list element.
   */
  renderActionList() {
    return (h("div", { tabIndex: 0, role: "menu", slot: "items", onMouseDown: this.preventDefaultEvent, onKeyDown: this.preventDefaultEvent },
      h("vega-dropdown-item", { itemKey: ColumnOperationType.HEADER_COLUMN },
        h("vega-flex", { alignItems: "center", gap: "size-8" },
          h("vega-toggle-switch", { checked: ToggleHeaderTableColumnStrategy.isFullHeaderSelection(this.selectedCells), size: "small" }),
          "Header Column")),
      h("vega-dropdown-item", { itemKey: ColumnOperationType.ADD_COLUMN_LEFT }, "Insert column left"),
      h("vega-dropdown-item", { itemKey: ColumnOperationType.ADD_COLUMN_RIGHT }, "Insert column right"),
      h("vega-dropdown-item", { itemKey: ColumnOperationType.DELETE_COLUMN, disabled: this.allColumnSelected(this.selectedCells) }, "Delete column"),
      h("vega-dropdown-item", { itemKey: ColumnOperationType.SELECT_COLUMN }, "Select column")));
  }
  /**
   * Determine if all columns are selected based on the selected cell range.
   *
   * @param selectedCellRange
   * @returns {boolean} true if all columns are selected
   */
  allColumnSelected(selectedCellRange) {
    if (selectedCellRange.length === 0) {
      return false;
    }
    const firstSelectedCell = selectedCellRange[0];
    const table = getParentBlockByType(firstSelectedCell, RTETableBlock);
    if (!table) {
      return false;
    }
    const [minCol, maxCol] = getSelectedCellsBoundary(selectedCellRange, table);
    const tableCount = getTableColumnCount(table);
    return maxCol - minCol + 1 === tableCount;
  }
}
__decorate([
  MapToComponentField({ writable: true })
], ColumnButtonSlimmer.prototype, "isColumnDropdownOpen", void 0);
