import { SORT, SORT_TYPE, SORT_CODE_TYPE } from "../constants";
import { Pagination } from "../types";

/** 将 sort 的值 变成 desc / asc */
export const sortValue2TypeCode = (sort: SORT_TYPE) => {
  return Object.values(SORT).find((s) => s.value === sort).code;
};

/** 将  desc / asc 变成 后端需要的 sort 的值 */
export const sortTypeCode2Value = (sort: SORT_CODE_TYPE) => {
  return Object.values(SORT).find((s) => s.code === sort).value;
};

/** 在新的标签页打开窗口 */
export function toNewTab(url) {
  const link = document.createElement("a");
  link.href = url;
  link.target = "__blank";
  link.click();
  link.remove();
}

interface Options2PaginationOptions {
  current?: Pagination["current"];
  pageSize?: Pagination["pageSize"];
  [key: string]: any;
}
export function options2Pagination(options?: Options2PaginationOptions) {
  return {
    page: options?.current ?? 1,
    page_size: options?.pageSize ?? 10,
  };
}

/** 首字母大写 */
export function firstUpperCase(str: string) {
  const [first, ...arr] = str;
  return first.toLocaleUpperCase() + arr.join("");
}
