'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var iconsReact = require('@cloudtower/icons-react');
var index = require('../Button/index.js');
var index$1 = require('../Icon/index.js');
var useParrotTranslation = require('../../hooks/useParrotTranslation.js');
var antd = require('antd');
var cs = require('classnames');
var React = require('react');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var cs__default = /*#__PURE__*/_interopDefault(cs);
var React__default = /*#__PURE__*/_interopDefault(React);

const PaginationStyle = "E_phljevk";
const DropdownOverlayStyle = "E_d12rtyeg";
const Pagination = props => {
  const {
    current,
    count,
    onChange,
    size,
    onSizeChange,
    showTotal = true,
    className,
    selectorVisible = true
  } = props;
  const sizeRef = React.useRef(size);
  const {
    t
  } = useParrotTranslation();
  React.useEffect(() => {
    if (sizeRef.current === size) return;
    sizeRef.current = size;
    onSizeChange == null ? void 0 : onSizeChange(size);
  }, [count, size, onSizeChange]);
  const selectOptions = React.useMemo(() => {
    if (!selectorVisible) return [];
    return Array.from({
      length: Math.ceil(count / size)
    }, (r, i) => {
      const value = i * size;
      const lastRange2 = value + size;
      return {
        value: i + 1,
        text: t("components.pagination_range", {
          range1: value + 1,
          range2: lastRange2 > count ? count : lastRange2
        })
      };
    });
  }, [selectorVisible, count, size, t]);
  let lastRange = current * size;
  lastRange = lastRange > count ? count : lastRange;
  const renderLeft = () => {
    if (!showTotal) return null;
    const content = t("components.pagination_total", {
      range1: (current - 1) * size + 1,
      range2: lastRange,
      total: count
    });
    if (selectorVisible) {
      return /* @__PURE__ */React__default.default.createElement(antd.Dropdown, {
        placement: "topLeft",
        overlayClassName: DropdownOverlayStyle,
        overlay: /* @__PURE__ */React__default.default.createElement(antd.Menu, null, /* @__PURE__ */React__default.default.createElement(antd.Menu.ItemGroup, {
          title: t("components.push_to")
        }, selectOptions.map(option => /* @__PURE__ */React__default.default.createElement(antd.Menu.Item, {
          key: option.value,
          onClick: () => onChange(option.value)
        }, option.text))))
      }, /* @__PURE__ */React__default.default.createElement(index$1, {
        alt: "arrowChevronDownSmall16Secondary",
        className: "pagination-left dropdown-trigger",
        src: iconsReact.ArrowChevronDownSmall16SecondaryIcon,
        hoverSrc: iconsReact.ArrowChevronDownSmall16BlueIcon,
        prefix: content
      }));
    }
    return /* @__PURE__ */React__default.default.createElement("span", {
      className: "pagination-left"
    }, content);
  };
  if (count === 0) {
    return null;
  }
  return /* @__PURE__ */React__default.default.createElement("div", {
    className: cs__default.default(PaginationStyle, "pagination-wrapper", className, selectorVisible && "has-selector")
  }, renderLeft(), /* @__PURE__ */React__default.default.createElement("span", {
    className: "pagination-right"
  }, current > 1 && /* @__PURE__ */React__default.default.createElement(index, {
    className: "prev-btn",
    type: "quiet",
    size: "small",
    prefixIcon: /* @__PURE__ */React__default.default.createElement(index$1, {
      alt: "arrowChevronLeftSmall16BoldBlue",
      src: iconsReact.ArrowChevronLeftSmall16BoldBlueIcon
    }),
    onClick: () => {
      onChange == null ? void 0 : onChange(current - 1);
    }
  }, t("components.previous_items", {
    size
  })), current * size < count && /* @__PURE__ */React__default.default.createElement(index, {
    className: "next-btn",
    type: "quiet",
    size: "small",
    suffixIcon: /* @__PURE__ */React__default.default.createElement(index$1, {
      alt: "arrowChevronLeftSmall16BoldBlue",
      src: iconsReact.ArrowChevronLeftSmall16BoldBlueIcon
    }),
    onClick: () => {
      onChange == null ? void 0 : onChange(current + 1);
    }
  }, t("components.next_items", {
    size
  }))));
};
var Pagination$1 = Pagination;

exports.PaginationStyle = PaginationStyle;
exports.default = Pagination$1;
