'use strict';
import { c } from 'react/compiler-runtime';
import { jsxs, jsx } from 'react/jsx-runtime';
import { useState, useEffect } from 'react';
import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent } from '../../../fd-component-toolbox/dropdown-menu/DropdownMenu.js';
import { shouldDisabledNextPageButton, handlePageSizeChange, goToPreviousPage, goToNextPage } from './Pager.funcs.js';
import { Select, SelectTrigger, SelectContent, SelectGroup, SelectItem } from '../../../fd-component-toolbox/select/Select.js';
import { Button } from '../../../fd-component-toolbox/button/Button.js';
import { Icon, iconNames } from '../../../fd-component-toolbox/icon/Icon.js';
import { cn } from '../../../../fd-toolbox/lib/utils.js';
import { tableDisplayModes } from '../../../../fd-toolbox/enums/enums.js';
import { elements } from '../../../../fd-toolbox/constants/elements.js';
import { elementPrefixes } from '../../constants/element-prefixes.js';
import { Tooltip } from '../../../fd-component-toolbox/tooltip/Tooltip.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
const pageSizeOptions = [1, 3, 6, 10, 25, 50, 100];
function Pager(t0) {
  const $ = c(21);
  const {
    displayMode,
    pageSize: pageSizeProp,
    rowCount,
    onPaging,
    setIsActive,
    setPageNumber,
    pageNumber
  } = t0;
  const [pageSize, setPageSize] = useState(pageSizeProp);
  const [isOpened, setIsOpened] = useState(false);
  let t1;
  let t2;
  if ($[0] !== pageSizeProp) {
    t1 = () => {
      setPageSize(pageSizeProp);
    };
    t2 = [pageSizeProp];
    $[0] = pageSizeProp;
    $[1] = t1;
    $[2] = t2;
  } else {
    t1 = $[1];
    t2 = $[2];
  }
  useEffect(t1, t2);
  let t3;
  if ($[3] !== pageNumber || $[4] !== pageSize || $[5] !== rowCount) {
    t3 = pageNumber === 1 && !shouldDisabledNextPageButton(rowCount, pageSize) || pageNumber > 1;
    $[3] = pageNumber;
    $[4] = pageSize;
    $[5] = rowCount;
    $[6] = t3;
  } else {
    t3 = $[6];
  }
  const shouldShowPagingArrows = t3;
  let t4;
  let t5;
  if ($[7] !== setIsActive) {
    t4 = () => setIsActive(true);
    t5 = () => setIsActive(false);
    $[7] = setIsActive;
    $[8] = t4;
    $[9] = t5;
  } else {
    t4 = $[8];
    t5 = $[9];
  }
  let t6;
  if ($[10] !== displayMode || $[11] !== isOpened || $[12] !== onPaging || $[13] !== pageNumber || $[14] !== pageSize || $[15] !== rowCount || $[16] !== setPageNumber || $[17] !== shouldShowPagingArrows || $[18] !== t4 || $[19] !== t5) {
    t6 = jsxs("div", {
      className: "flex items-center gap-1 pt-2 pb-1 pl-1 text-sm select-none",
      onFocus: t4,
      onBlur: t5,
      children: [(displayMode ?? tableDisplayModes.default) !== tableDisplayModes.minimal && jsxs(DropdownMenu, {
        open: isOpened,
        onOpenChange: setIsOpened,
        children: [jsx(DropdownMenuTrigger, {
          children: jsx(Tooltip, {
            content: "Pages",
            side: "top",
            className: "mb-1 size-7",
            delayDuration: 100,
            children: jsx(Icon, {
              id: elements.pager.openPagerButtonId,
              name: "dotsHorizontal"
            })
          })
        }), jsx(DropdownMenuContent, {
          align: "start",
          children: jsxs(Select, {
            value: pageSize.toString(),
            onValueChange: newSize => handlePageSizeChange(setPageSize, pageNumber, onPaging, setPageNumber, setIsOpened, newSize ?? void 0),
            onOpenChange: open => !open && setIsOpened(false),
            children: [jsx(SelectTrigger, {
              id: elements.pager.pagerSelectTriggerId,
              className: "hover:bg-muted/50 focus:bg-muted/50 text-muted-foreground h-7 w-full cursor-pointer rounded-sm border-0 p-1",
              size: "short",
              children: jsxs("div", {
                className: "flex items-center justify-between gap-2 text-sm",
                children: [jsx("span", {
                  children: "Rows"
                }), jsx("span", {
                  children: pageSize
                })]
              })
            }), jsx(SelectContent, {
              className: "p-0",
              children: jsx(SelectGroup, {
                children: pageSizeOptions.map(_temp)
              })
            })]
          })
        })]
      }), shouldShowPagingArrows && jsxs("div", {
        children: [jsx(Button, {
          id: elements.pager.previousPageButton,
          disabled: pageNumber === 1,
          size: "icon",
          variant: "ghost",
          onClick: () => goToPreviousPage(pageNumber, pageSize, onPaging, setPageNumber),
          className: "size-7 shrink-0",
          children: jsx(Icon, {
            name: iconNames.chevronLeft,
            className: "size-4.5"
          })
        }), jsx(Button, {
          id: elements.pager.nextPageButton,
          disabled: shouldDisabledNextPageButton(rowCount, pageSize),
          size: "icon",
          variant: "ghost",
          onClick: () => goToNextPage(pageNumber, pageSize, onPaging, setPageNumber),
          className: cn("size-7 shrink-0", rowCount === 0 && "hidden"),
          children: jsx(Icon, {
            name: iconNames.chevronRight,
            className: "size-4.5"
          })
        })]
      }), pageNumber > 1 && jsxs("div", {
        className: "mr-1",
        children: ["Page ", pageNumber]
      })]
    });
    $[10] = displayMode;
    $[11] = isOpened;
    $[12] = onPaging;
    $[13] = pageNumber;
    $[14] = pageSize;
    $[15] = rowCount;
    $[16] = setPageNumber;
    $[17] = shouldShowPagingArrows;
    $[18] = t4;
    $[19] = t5;
    $[20] = t6;
  } else {
    t6 = $[20];
  }
  return t6;
}
function _temp(option) {
  return jsx(SelectItem, {
    className: "text-muted-foreground cursor-pointer",
    id: elementPrefixes.selectItem + option.toString(),
    value: option.toString(),
    children: option
  }, option);
}
__name(Pager, "Pager");

export { Pager, pageSizeOptions };
