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;
};
var __rest = (this && this.__rest) || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
    t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
import { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import { arrayToClassString } from '../../../../utils/ui';
import { h } from '@stencil/core';
import { DomNodeSubjectObserverFactory } from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaChange, VegaPageSizeChange } from '../../../../helpers/event-manager/event-id/vega-event-id';
import ResponsiveFormatFacade from '../../../../helpers/formatter/responsive-map-formatter/responsive-format-facade';
export class VegaTablePaginationRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.paginationRefCallback = (ref) => {
      DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, VegaChange, this.handlePaginationChange);
      DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, VegaPageSizeChange, this.handlePaginationPageSizeChange);
    };
    /**
     * handle the pagination page change after observer the notify
     *
     * @param {NotifyObserverPayload<number>} input payload message, detail is the current page index
     */
    this.handlePaginationChange = (input) => {
      var _a, _b;
      this.vegaTablePaginationController.setCurrentPage(input.detail);
      const event = new CustomEvent('vegaChange', { detail: input.detail });
      (_b = (_a = this.pagination).vegaChange) === null || _b === void 0 ? void 0 : _b.call(_a, event);
    };
    /**
     * handle the pagination page size change after observer the notify
     *
     * @param {NotifyObserverPayload<number>} input payload message, detail is the current page size
     */
    this.handlePaginationPageSizeChange = (input) => {
      var _a, _b;
      this.vegaTablePaginationController.setPageSize(input.detail);
      const event = new CustomEvent('vegaPageSizeChange', {
        detail: input.detail,
      });
      (_b = (_a = this.pagination).vegaPageSizeChange) === null || _b === void 0 ? void 0 : _b.call(_a, event);
    };
    this.getPaginationConfig = () => {
      const _a = this.pagination, { position } = _a, otherProps = __rest(_a, ["position"]);
      const currentExtensions = ResponsiveFormatFacade.formatCustom(this.pagination.extensions);
      return Object.assign(Object.assign({}, otherProps), { position: (currentExtensions === null || currentExtensions === void 0 ? void 0 : currentExtensions.length) > 1 ? undefined : position || 'bottom-center', pageSize: this.pageSize });
    };
  }
  render() {
    if (!this.pagination)
      return;
    const { pageSize, position, extensions, pageSizeSelectorOptions } = this.getPaginationConfig();
    const paginationWrapperClasses = ['vega-table-pagination-container', position];
    const total = this.vegaTableDataSourceController.getCurrentData().length;
    return (total > 0 && (h("div", { class: arrayToClassString(paginationWrapperClasses) },
      h("vega-pagination", { total: total, pageSize: pageSize, current: this.currentPage, extensions: extensions, pageSizeSelectorOptions: pageSizeSelectorOptions, 
        /* eslint-disable-next-line react/jsx-no-bind */
        ref: this.paginationRefCallback }))));
  }
}
__decorate([
  MapToComponentField()
], VegaTablePaginationRenderer.prototype, "pagination", void 0);
__decorate([
  MapToComponentField()
], VegaTablePaginationRenderer.prototype, "pageSize", void 0);
__decorate([
  MapToComponentField()
], VegaTablePaginationRenderer.prototype, "currentPage", void 0);
__decorate([
  MapToComponentField()
], VegaTablePaginationRenderer.prototype, "vegaTablePaginationController", void 0);
__decorate([
  MapToComponentField()
], VegaTablePaginationRenderer.prototype, "vegaTableDataSourceController", void 0);
