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 } from '../../../../../../../helpers/event-manager/event-id/vega-event-id';
import { TableSettingType } from '../../../setting-event-handlers/setting-event-handler.abstract';
import { RowOperationType } from '../../../setting-event-handlers/row-operation';
import { RTETableHeadBlock } from '../../../dto/table-head-block';
import { RTETableBlock } from '../../../dto/table-block';
import { getParentBlockByType } from '../../../utils/utils';
import { RTETableBodyBlock } from '../../../dto/table-body-block';
/**
 * Slimmer dedicated to building the row button.
 */
export class RowButtonSlimmer extends TableSettingButtonSlimmer {
  constructor() {
    super({
      icon: 'rte-table-row',
      title: 'Row',
      showArrowIcon: true,
    });
    this.handleDropdownItemClick = (input) => {
      this.emitClickEvent([TableSettingType.ROW, input.detail]);
    };
  }
  renderBtnWrapper(renderBtn) {
    return (h("vega-dropdown", { isScreenPosition: true, minWidth: 180, trigger: "click", ref: (el) => {
        DomNodeSubjectObserverFactory.addUniqueObserverToNode(el, VegaDropdownClick, this.handleDropdownItemClick);
      } },
      renderBtn(),
      this.renderActionList()));
  }
  isDisabled() {
    return this.selectedCells.length === 0;
  }
  isSelected() {
    return false;
  }
  /* istanbul ignore next */
  // eslint-disable-next-line @typescript-eslint/no-empty-function
  handleClick() { }
  /**
   * Render the action list.
   *
   * @returns {HTMLElement} The action list element.
   */
  renderActionList() {
    const shouldChecked = this.areSelectedRowsHeaderRows();
    return (h("div", { tabIndex: 0, role: "menu", slot: "items", onMouseDown: this.preventDefaultEvent, onKeyDown: this.preventDefaultEvent },
      h("vega-dropdown-item", { itemKey: RowOperationType.HEADER_ROW },
        h("vega-flex", { alignItems: "center", direction: "row", gap: "size-8" },
          h("vega-toggle-switch", { key: shouldChecked.toString(), size: "small", checked: shouldChecked }),
          h("span", null, "Header row"))),
      h("vega-dropdown-item", { itemKey: RowOperationType.ADD_ROW_ABOVE }, "Insert row above"),
      h("vega-dropdown-item", { itemKey: RowOperationType.ADD_ROW_BELOW }, "Insert row below"),
      h("vega-dropdown-item", { disabled: this.areAllRowsSelected(), itemKey: RowOperationType.DELETE_ROW }, "Delete row"),
      h("vega-dropdown-item", { itemKey: RowOperationType.SELECT_ROW }, "Select row")));
  }
  /**
   * Check if all selected rows are header rows.
   *
   * @returns {boolean} - true if all selected rows are header rows, false otherwise.
   */
  areSelectedRowsHeaderRows() {
    const selectedRows = this.getSelectedRows();
    return (selectedRows.length > 0 &&
      selectedRows.every((row) => row.parent instanceof RTETableHeadBlock));
  }
  /**
   * Check if all rows are selected. Should disable some row operations if true.
   *
   * @returns {boolean} - true if all rows are selected, false otherwise.
   */
  areAllRowsSelected() {
    let flag = false;
    const selectedRows = this.getSelectedRows();
    if (selectedRows.length) {
      const table = getParentBlockByType(selectedRows[0], RTETableBlock);
      if (table) {
        const allRows = table.children
          .filter((block) => block instanceof RTETableHeadBlock || block instanceof RTETableBodyBlock)
          .flatMap((item) => item.children);
        flag = selectedRows.length === allRows.length;
      }
    }
    return flag;
  }
  /**
   * Get all selected rows from selected cells.
   *
   * @returns {RTETableRowBlock[]} - Array of selected table row blocks.
   */
  getSelectedRows() {
    const selectedRows = new Set();
    if (this.selectedCells && this.selectedCells.length) {
      this.selectedCells.forEach((cell) => {
        if (cell.parent) {
          selectedRows.add(cell.parent);
        }
      });
    }
    return Array.from(selectedRows);
  }
}
