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 { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
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 { findParent } from '../../../../../utils/ui';
import changeManager from '../../../../../helpers/change-manager/change-manager';
import domNodeSubjectFactory from '../../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { VegaInternalPaginationPageSizeChange } from '../../../../../helpers/event-manager/event-id/vega-internal-event-id';
import { normalizePageSizeOption, sortPageSizeOptions, includesPageSize, } from '../../../utils/page-size-option-utils';
import { PAGE_SIZE_ALL } from '../../../constants/page-size';
export class PageSizeSelectorMobileDropdownRenderer extends VegaSlimmer {
  /**
   * Renders the dropdown component with the provided children.
   *
   * @param {HTMLElement} children HTML element children to be rendered inside the dropdown.
   * @returns {HTMLElement} The rendered dropdown component.
   */
  render(children) {
    var _a;
    /* istanbul ignore next */
    const options = this.isAllActive || includesPageSize(this.pageSizeSelectorOptions, this.pageSize)
      ? this.pageSizeSelectorOptions
      : [...this.pageSizeSelectorOptions, this.pageSize];
    const normalized = options.map(normalizePageSizeOption);
    const mergedPageSizeOptions = sortPageSizeOptions(normalized);
    const itemsPerPageLabel = this.translationSlimmer.t('Items Per Page');
    const selectedValue = this.isAllActive ? PAGE_SIZE_ALL : this.pageSize;
    return (h("vega-dropdown", { trigger: "click", selectType: "single", alignment: "center", minWidth: 160, selectedSourceKey: selectedValue.toString(), source: [
        {
          key: 'items-per-page',
          label: itemsPerPageLabel,
          children: [
            ...mergedPageSizeOptions.map((option) => ({
              key: option.value.toString(),
              label: option.label,
            })),
          ],
        },
      ], ref: (node) => {
        DomNodeSubjectObserverFactory.addUniqueObserverToNode(node, VegaDropdownClick, (payload) => {
          this.notifyParentPagination(payload.detail);
        });
      }, isScreenPosition: (_a = this.dropdownProps) === null || _a === void 0 ? void 0 : _a.isScreenPosition }, children));
  }
  notifyParentPagination(detail) {
    const pagination = findParent(this.host, 'vega-pagination');
    if (pagination) {
      changeManager.notify(domNodeSubjectFactory.getSubject(pagination, VegaInternalPaginationPageSizeChange), {
        host: this.host,
        detail,
      });
    }
  }
}
__decorate([
  MapToComponentField()
], PageSizeSelectorMobileDropdownRenderer.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], PageSizeSelectorMobileDropdownRenderer.prototype, "translationSlimmer", void 0);
__decorate([
  MapToComponentField()
], PageSizeSelectorMobileDropdownRenderer.prototype, "pageSizeSelectorOptions", void 0);
__decorate([
  MapToComponentField({ writable: true })
], PageSizeSelectorMobileDropdownRenderer.prototype, "pageSize", void 0);
__decorate([
  MapToComponentField()
], PageSizeSelectorMobileDropdownRenderer.prototype, "dropdownProps", void 0);
__decorate([
  MapToComponentField()
], PageSizeSelectorMobileDropdownRenderer.prototype, "total", void 0);
__decorate([
  MapToComponentField()
], PageSizeSelectorMobileDropdownRenderer.prototype, "isAllActive", void 0);
