import { h } from "vue";
import { NIcon } from "naive-ui";
/**
 * 格式化时间戳为字符串
 * @param {number} time - 待格式化的时间戳
 * @returns {string} - 格式化后的日期字符串
 */
export function formatDate(time: number): string {
  const date: Date = new Date(time);
  const year: number | string = date.getFullYear();
  let month: number | string = date.getMonth() + 1;
  let day: number | string = date.getDate();
  let hour: number | string = date.getHours();
  let minute: number | string = date.getMinutes();
  let second: number | string = date.getSeconds();

  if (month < 10) {
    month = "0" + month;
  }

  if (day < 10) {
    day = "0" + day;
  }

  if (hour < 10) {
    hour = "0" + hour;
  }

  if (minute < 10) {
    minute = "0" + minute;
  }

  if (second < 10) {
    second = "0" + second;
  }

  /**
   * 格式化后的日期字符串
   * @type {string}
   */
  const formattedDate: string =
    year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

  return formattedDate;
}

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path: string) {
  const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
  return isExternal;
}

/**
 * * render 图标
 *  @param icon 图标
 *  @param set 设置项
 */
export const renderIcon = (icon: any, set = {}) => {
  return () => h(NIcon, set, { default: () => h(icon) });
};
