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 { TableSettingType } from '../../../setting-event-handlers/setting-event-handler.abstract';
import { MapToComponentField } from 'vega-slimmer/core';
import { MergeCellsOperationType } from '../../../setting-event-handlers/merge-cells-operation';
import { getAdjacentCell, isRectangularSelection } from '../../../utils/utils';
import { SplitCellOperationType } from '../../../setting-event-handlers/split-cell-operation';
/**
 * Slimmer dedicated to building the merge cells button.
 */
export class MergeCellsButtonSlimmer extends TableSettingButtonSlimmer {
  constructor() {
    super({
      icon: 'rte-table-merge-cells',
      title: 'Merge cells',
      showArrowIcon: true,
    });
    this.handleDropdownItemClick = (input) => {
      if (Object.values(SplitCellOperationType).includes(input.detail)) {
        this.emitClickEvent([TableSettingType.SPLIT_CELL, input.detail]);
        return;
      }
      this.emitClickEvent([TableSettingType.MERGE_CELLS, input.detail]);
    };
    this.handleDropdownVisibilityChange = ({ eventName }) => {
      this.isMergeCellsDropdownOpen = eventName.toLocaleLowerCase().indexOf('show') >= 0;
    };
  }
  renderBtnWrapper(renderBtn) {
    return (h("vega-dropdown", { isScreenPosition: true, minWidth: 150, 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.isMergeCellsDropdownOpen;
  }
  isDisabled() {
    if (this.selectedCells.length === 0) {
      return true;
    }
    const hasTableCell = this.selectedCells.some((item) => item.type === 'table-cell');
    const hasTableHeadCell = this.selectedCells.some((item) => item.type === 'table-head-cell');
    if (hasTableCell && hasTableHeadCell) {
      return true;
    }
    return !isRectangularSelection(this.selectedCells);
  }
  handleClick() {
    this.isMergeCellsDropdownOpen = true;
  }
  /**
   * Render the action list.
   *
   * @returns {HTMLElement} The action list element.
   */
  renderActionList() {
    const disabledMap = this.getMergeDirectionDisabledMap();
    return (h("div", { tabIndex: 0, role: "menu", slot: "items", onMouseDown: this.preventDefaultEvent, onKeyDown: this.preventDefaultEvent },
      h("vega-dropdown-item", { itemKey: MergeCellsOperationType.MERGE_CELLS }, "Merge cells"),
      h("vega-dropdown-item", { itemKey: MergeCellsOperationType.MERGE_CELL_UP, disabled: disabledMap[MergeCellsOperationType.MERGE_CELL_UP] }, "Merge cell up"),
      h("vega-dropdown-item", { itemKey: MergeCellsOperationType.MERGE_CELL_RIGHT, disabled: disabledMap[MergeCellsOperationType.MERGE_CELL_RIGHT] }, "Merge cell right"),
      h("vega-dropdown-item", { itemKey: MergeCellsOperationType.MERGE_CELL_DOWN, disabled: disabledMap[MergeCellsOperationType.MERGE_CELL_DOWN] }, "Merge cell down"),
      h("vega-dropdown-item", { itemKey: MergeCellsOperationType.MERGE_CELL_LEFT, disabled: disabledMap[MergeCellsOperationType.MERGE_CELL_LEFT] }, "Merge cell left"),
      h("vega-dropdown-item", { itemKey: SplitCellOperationType.SPLIT_CELL_VERTICALLY, disabled: !this.selectSingleCell() }, "Split cell vertically"),
      h("vega-dropdown-item", { itemKey: SplitCellOperationType.SPLIT_CELL_HORIZONTALLY, disabled: !this.selectSingleCell() }, "Split cell horizontally")));
  }
  selectSingleCell() {
    return this.selectedCells.length === 1;
  }
  getMergeDirectionDisabledMap() {
    if (this.selectedCells.length !== 1) {
      return {
        [MergeCellsOperationType.MERGE_CELL_UP]: true,
        [MergeCellsOperationType.MERGE_CELL_RIGHT]: true,
        [MergeCellsOperationType.MERGE_CELL_DOWN]: true,
        [MergeCellsOperationType.MERGE_CELL_LEFT]: true,
      };
    }
    const cell = this.selectedCells[0];
    const row = cell.parent;
    const table = row.parent;
    const rowIndex = table.children.indexOf(row);
    const cellIndex = row.children.indexOf(cell);
    const isFirstRow = rowIndex === 0;
    const isLastRow = rowIndex === table.children.length - 1;
    const isFirstCol = cellIndex === 0;
    const isLastCol = cellIndex === row.children.length - 1;
    const getDisabledForDirection = (direction, isOnEdge) => {
      if (isOnEdge) {
        return true;
      }
      const adjacentCell = getAdjacentCell(cell, direction);
      return !adjacentCell || !isRectangularSelection([cell, adjacentCell]);
    };
    return {
      [MergeCellsOperationType.MERGE_CELL_UP]: getDisabledForDirection(MergeCellsOperationType.MERGE_CELL_UP, isFirstRow),
      [MergeCellsOperationType.MERGE_CELL_RIGHT]: getDisabledForDirection(MergeCellsOperationType.MERGE_CELL_RIGHT, isLastCol),
      [MergeCellsOperationType.MERGE_CELL_DOWN]: getDisabledForDirection(MergeCellsOperationType.MERGE_CELL_DOWN, isLastRow),
      [MergeCellsOperationType.MERGE_CELL_LEFT]: getDisabledForDirection(MergeCellsOperationType.MERGE_CELL_LEFT, isFirstCol),
    };
  }
}
__decorate([
  MapToComponentField({ writable: true })
], MergeCellsButtonSlimmer.prototype, "isMergeCellsDropdownOpen", void 0);
