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 { Component, Host, h, Prop, Element, Event, State } from '@stencil/core';
import { arrayToClassString } from '../../../utils/ui';
import { generateResponsiveClass } from '../utils';
import { createEnterKeyHandlerToTriggerClick } from '../../../utils/accessibility';
import { sanitizeVegaComponent } from '../../../utils/component';
import { createEventEmitSlimmer, } from '../../../helpers/event-manager/slimmers/event-emit-slimmer';
import { VegaClick } from '../../../helpers/event-manager/event-id/vega-event-id';
import { InjectVegaGlobalSlimmer, InjectVegaSlimmer } from 'vega-slimmer/core';
import { VegaTableHeadCellStateController } from './slimmers/controllers/vega-table-head-cell-state-controller';
import ResponsiveFormatFacade from '../../../helpers/formatter/responsive-map-formatter/responsive-format-facade';
import { getCurrentBreakpoint } from '../../../utils/breakpoints';
import { PageResizeObserverSlimmer } from '../../../helpers/slimmers/page-resize-observer-slimmer';
import { VegaInternalIconManager } from '../../../helpers/icon/internal-icon-manager';
import chevronUp from '../../../global/icons/chevron-up';
import chevronDown from '../../../global/icons/chevron-down';
import arrowUpArrowDown from '../../../global/icons/arrow-up-arrow-down';
import circleArrowDown from '../../../global/icons/circle-arrow-down';
import circleArrowUp from '../../../global/icons/circle-arrow-up';
import { FeatureFlag } from '../../../helpers/feature-control/feature-flag-controller';
import { VegaComponentUsageRuntimeMetricsSlimmer } from '../../../helpers/slimmers/component-usage-runtime-metrics';
import { SortIconRenderer } from './slimmers/renderers/sort-icon-renderer';
import { VegaTableHeadCellSortConfigController } from './slimmers/controllers/vega-table-head-cell-sort-config-controller';
import { VegaInternalColumnHeadCellRenderInputRIC } from '../../../helpers/event-manager/event-id/vega-internal-event-id';
import { VegaTableChildRenderInputConsumerSlimmer } from '../../../helpers/slimmers/vega-table-child-render-input-consumer-slimmer';
/**
 * @vegaVersion 1.44.0
 */
export class VegaTableHeadCell {
  constructor() {
    this.globalSlimmers = {};
    this.clickEventEmitter = createEventEmitSlimmer(VegaTableHeadCell, VegaClick);
    this.vegaTableHeadCellStateController = new VegaTableHeadCellStateController();
    this.pageResizeObserverSlimmer = new PageResizeObserverSlimmer();
    this.vegaComponentUsageRuntimeMetricsSlimmer = new VegaComponentUsageRuntimeMetricsSlimmer();
    this.sortIconRenderer = new SortIconRenderer();
    this.vegaTableHeadCellSortConfigController = new VegaTableHeadCellSortConfigController();
    this.vegaTableRenderInputConsumerSlimmer = new VegaTableChildRenderInputConsumerSlimmer(VegaInternalColumnHeadCellRenderInputRIC);
    this.currentBreakpoint = getCurrentBreakpoint();
    this.handleSort = (e) => {
      this.sortOrder = this.getNextSortOrder(this.sortOrder);
      this.clickEventEmitter.emit({ sortOrder: this.sortOrder }, e);
      this.vegaTableHeadCellSortConfigController.notifyOtherHeadCellOnClickSort();
    };
    /**
     * Get the next sorter order
     *
     * @param {string} sortOrder - The prop name of which sorter is sorted by
     * @returns {Nullable<'desc' | 'asc'>} Return the next sorter order
     */
    this.getNextSortOrder = (sortOrder) => {
      var _a;
      const isReverseSortOrder = FeatureFlag.isEnabled('VEGA_TABLE.REVERSE_SORT_ORDER');
      const baseOrderSequence = isReverseSortOrder
        ? ['asc', 'desc']
        : ['desc', 'asc'];
      const orderSequence = ((_a = this.sortConfig) === null || _a === void 0 ? void 0 : _a.disableUnsorted)
        ? baseOrderSequence
        : [...baseOrderSequence, null];
      return orderSequence[(orderSequence.indexOf(sortOrder) + 1) % orderSequence.length];
    };
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g;
    const sortableConfig = this.sorter
      ? {
        role: 'button',
        onKeyDown: createEnterKeyHandlerToTriggerClick(),
        tabIndex: 0,
        onClick: (e) => this.handleSort(e),
      }
      : null;
    return sanitizeVegaComponent(h(Host, { class: {
        [`density-${ResponsiveFormatFacade.formatCustom(this.density)}`]: true,
        'sortable': !!this.sorter,
        'column-divider': this.columnDivider,
      }, style: { width: this.width } },
      h("th", Object.assign({ class: {
          'vega-table-cell': true,
          'break-word': !FeatureFlag.isEnabled('VEGA_TABLE.ENABLE_NEW_CELL_BREAK_WORDS_STYLE'),
          'is-group-cell': (_a = this.renderInput) === null || _a === void 0 ? void 0 : _a.headHasGroupColumn,
          'hide-right-border': ((_b = this.renderInput) === null || _b === void 0 ? void 0 : _b.isViewEnd) || ((_c = this.renderInput) === null || _c === void 0 ? void 0 : _c.isNoPinnedEnd),
          'right-divider': (_d = this.renderInput) === null || _d === void 0 ? void 0 : _d.isLeftEndPinned,
          'left-divider': (_e = this.renderInput) === null || _e === void 0 ? void 0 : _e.isRightStartPinned,
          [generateResponsiveClass(this.breakpoint, 'v-table-cell')]: true,
        } }, sortableConfig, { colSpan: (_f = this.renderInput) === null || _f === void 0 ? void 0 : _f.colSpan, rowSpan: (_g = this.renderInput) === null || _g === void 0 ? void 0 : _g.rowSpan }),
        h("div", { class: arrayToClassString(['vega-table-column', this.textAlign]) },
          h("span", { class: "vega-table-column-label" },
            h("slot", null)),
          this.sortIconRenderer.render()))), this.host);
  }
  static get is() { return "vega-table-head-cell"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["vega-table-head-cell.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["vega-table-head-cell.css"]
  }; }
  static get properties() { return {
    "breakpoint": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "VegaTableColumnBreakpoint",
        "resolved": "\"L\" | \"M\" | \"S\" | \"XL\"",
        "references": {
          "VegaTableColumnBreakpoint": {
            "location": "import",
            "path": "../interface"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.44.0"
          }],
        "text": "Defines the screen width at which the column becomes hidden.\n   If not set, the column remains visible at all times."
      },
      "attribute": "breakpoint",
      "reflect": false
    },
    "sorter": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "VegaTableSorterConfig",
        "resolved": "\"event\" | ((a: VegaTableDataSourceItem, b: VegaTableDataSourceItem) => number)",
        "references": {
          "VegaTableSorterConfig": {
            "location": "import",
            "path": "../interface"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.44.0"
          }],
        "text": "Defines the column's sorting configuration."
      },
      "attribute": "sorter",
      "reflect": false
    },
    "sortOrder": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "VegaTableSortOrderType",
        "resolved": "\"asc\" | \"desc\"",
        "references": {
          "VegaTableSortOrderType": {
            "location": "import",
            "path": "../interface"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.8.0"
          }],
        "text": "Defines the column's sorting order."
      },
      "attribute": "sort-order",
      "reflect": false
    },
    "textAlign": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "'left' | 'right' | 'center'",
        "resolved": "\"center\" | \"left\" | \"right\"",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.44.0"
          }],
        "text": "Determines the alignment of text within the head cell."
      },
      "attribute": "text-align",
      "reflect": false
    },
    "width": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.44.0"
          }],
        "text": "Specifies the width of the head cell."
      },
      "attribute": "width",
      "reflect": false
    },
    "sortConfig": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "VegaTableSortConfig",
        "resolved": "{ type: \"single\" | \"multiple\"; disableUnsorted?: boolean; }",
        "references": {
          "VegaTableSortConfig": {
            "location": "import",
            "path": "../interface"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.42.0"
          }],
        "text": "Specifies the sorting configuration for the table header cell."
      }
    },
    "pinned": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "VegaTablePinnedType",
        "resolved": "\"left\" | \"right\"",
        "references": {
          "VegaTablePinnedType": {
            "location": "import",
            "path": "../interface"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.57.0"
          }],
        "text": "Specifies the pinned type of the head cell."
      },
      "attribute": "pinned",
      "reflect": false
    }
  }; }
  static get states() { return {
    "currentBreakpoint": {},
    "density": {},
    "columnDivider": {},
    "renderInput": {}
  }; }
  static get events() { return [{
      "method": "vegaClick",
      "name": "vegaClick",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.44.0"
          }],
        "text": "An event emitter notifying that the head cell has been clicked."
      },
      "complexType": {
        "original": "VegaTableHeadCellClickEvent",
        "resolved": "{ sortOrder: VegaTableSortOrderType; }",
        "references": {
          "VegaTableHeadCellClickEvent": {
            "location": "import",
            "path": "../interface"
          }
        }
      }
    }, {
      "method": "click",
      "name": "click",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "eventSemantics",
            "text": "namespace:native"
          }, {
            "name": "vegaVersion",
            "text": "2.0.0"
          }],
        "text": "An event emitter notifying that the head cell has been clicked."
      },
      "complexType": {
        "original": "VegaTableHeadCellClickEvent",
        "resolved": "{ sortOrder: VegaTableSortOrderType; }",
        "references": {
          "VegaTableHeadCellClickEvent": {
            "location": "import",
            "path": "../interface"
          }
        }
      }
    }]; }
  static get elementRef() { return "host"; }
}
(() => {
  VegaInternalIconManager.register({
    'chevron-up': chevronUp,
    'chevron-down': chevronDown,
    'arrow-up-arrow-down': arrowUpArrowDown,
    'circle-arrow-down': circleArrowDown,
    'circle-arrow-up': circleArrowUp,
  });
})();
__decorate([
  InjectVegaGlobalSlimmer()
], VegaTableHeadCell.prototype, "globalSlimmers", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaTableHeadCell.prototype, "clickEventEmitter", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaTableHeadCell.prototype, "vegaTableHeadCellStateController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaTableHeadCell.prototype, "pageResizeObserverSlimmer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaTableHeadCell.prototype, "vegaComponentUsageRuntimeMetricsSlimmer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaTableHeadCell.prototype, "sortIconRenderer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaTableHeadCell.prototype, "vegaTableHeadCellSortConfigController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaTableHeadCell.prototype, "vegaTableRenderInputConsumerSlimmer", void 0);
