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 { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { findParent } from '../../../../../utils/ui';
import { getCurrentBreakpoint } from '../../../../../utils/breakpoints';
import { Breakpoints } from '../../../../../constants/tokens/breakpoints';
import { generateColSpan } from '../../../utils';
/*
 * The next line is covered in e2e-test
 * @see{should-render-responsive-columns-with-expandable-row-and-current-breakpoint-is-smaller-than-S}
 * @see{should-render-responsive-columns-with-expandable-row-and-current-breakpoint-changed}
 */
/* istanbul ignore next */
/**
 * The controller for expand row col span.
 */
export class VegaTableExpandRowColSpanController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * Update state colSpan
     */
    this.updateColSpan = () => {
      this.colSpan = generateColSpan(this.getVisibleColumnsNumber(), this.withMultipleSelection);
    };
  }
  /**
   * Update the expand row content col span when component will load
   */
  componentWillLoad() {
    this.updateColSpan();
  }
  /**
   * Update the expand row content col span when current breakpoint changed
   */
  watchCurrentBreakpoint() {
    this.updateColSpan();
  }
  /**
   * Update the expand row content col span when selection type changed
   */
  watchWithMultipleSelection() {
    this.updateColSpan();
  }
  /**
   * Get visual columns number, it will be changed by the breakpoints
   *
   * @returns {number} The visible columns number
   */
  getVisibleColumnsNumber() {
    const table = findParent(this.host, 'vega-table');
    const currentBreakpoint = getCurrentBreakpoint();
    const isTemplateRenderMode = table.children &&
      Array.from(table.children).filter((item) => { var _a; return !((_a = item.getAttribute('slot')) === null || _a === void 0 ? void 0 : _a.startsWith('vega-table-expand-row-')); }).length > 0;
    const cells = isTemplateRenderMode
      ? table.querySelectorAll('vega-table-row:nth-of-type(1) vega-table-cell')
      : table.shadowRoot.querySelectorAll('vega-table-row:nth-of-type(1) vega-table-cell');
    return Array.from(cells).filter((cell) => {
      if (!cell.breakpoint)
        return true;
      if (currentBreakpoint === 'default')
        return false;
      return Breakpoints[currentBreakpoint] >= Breakpoints[cell.breakpoint];
    }).length;
  }
}
__decorate([
  MapToComponentField()
], VegaTableExpandRowColSpanController.prototype, "host", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableExpandRowColSpanController.prototype, "colSpan", void 0);
__decorate([
  MapToComponentField()
], VegaTableExpandRowColSpanController.prototype, "withMultipleSelection", void 0);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaTableExpandRowColSpanController.prototype, "componentWillLoad", null);
__decorate([
  MapToComponentMethod('watchCurrentBreakpoint')
], VegaTableExpandRowColSpanController.prototype, "watchCurrentBreakpoint", null);
__decorate([
  MapToComponentMethod('watchWithMultipleSelection')
], VegaTableExpandRowColSpanController.prototype, "watchWithMultipleSelection", null);
