/** Sentinel value representing the "All" page size option, which displays all records at once. */
const PAGE_SIZE_ALL = -1;

/**
 * Normalizes a page-size option (number or object) into a `NormalizedPageSizeOption` with `value` and `label`.
 *
 * @param {VegaPaginationPageSizeOption} option - A page-size option, either a plain number or an object with `value` and `label`.
 * @returns {NormalizedPageSizeOption} A normalized option object.
 */
function normalizePageSizeOption(option) {
  return typeof option === 'number' ? { value: option, label: option.toString() } : option;
}
/**
 * Sorts page-size options in ascending order, pinning the "All" option (value `-1`) last.
 *
 * @param {NormalizedPageSizeOption[]} options - An array of normalized page-size options.
 * @returns {NormalizedPageSizeOption[]} A new sorted array.
 */
function sortPageSizeOptions(options) {
  return [...options].sort((a, b) => {
    if (a.value === PAGE_SIZE_ALL)
      return 1;
    if (b.value === PAGE_SIZE_ALL)
      return -1;
    return a.value - b.value;
  });
}
/**
 * Resolves the sentinel value `-1` ("All") to `total`; returns other values unchanged.
 *
 * @param {number} value - The page size value, potentially the sentinel `-1`.
 * @param {number} total - The total number of items.
 * @returns {number} The resolved page size number.
 */
function resolvePageSizeValue(value, total) {
  return value === PAGE_SIZE_ALL ? total : value;
}
/**
 * Returns `true` if the options array contains an "All" entry (`{ value: -1 }`).
 *
 * @param {VegaPaginationPageSizeOption[]} options - An array of page-size options.
 * @returns {boolean} `true` when an "All" option is present.
 */
function hasAllOption(options) {
  return options.some((opt) => typeof opt === 'object' && opt.value === PAGE_SIZE_ALL);
}
/**
 * Returns `true` if any option in the array has a `value` equal to `pageSize`.
 *
 * @param {VegaPaginationPageSizeOption[]} options - An array of page-size options.
 * @param {number} pageSize - The page size to look for.
 * @returns {boolean} `true` when the page size is found.
 */
function includesPageSize(options, pageSize) {
  return options.some((opt) => {
    const value = typeof opt === 'number' ? opt : opt.value;
    return value === pageSize;
  });
}

export { PAGE_SIZE_ALL as P, hasAllOption as h, includesPageSize as i, normalizePageSizeOption as n, resolvePageSizeValue as r, sortPageSizeOptions as s };
