import dayjs from "dayjs";
import weekday from "dayjs/plugin/weekday";

dayjs.extend(weekday);

export interface homeWidgetProps {
  /**
   * @description 组件id
   */
  id?: string;
  /**
   * @description 组件参数
   */
  initParam?: any;
}

/**
 * 时间格式转义, Diff to DetaTime
 * @param data {unit:string, startDiff:number, endDiff:number}
 * @returns {Object}
 */
export function getDiffToDate(data: any) {
  data.startDiff = data.startDiff ?? 0;
  data.endDiff = data.endDiff ?? 0;

  const today = dayjs();
  let startTime = "",
    endTime = "";

  if (data.unit == "day") {
    startTime = today.add(data.startDiff, data.unit).startOf("day").format("YYYY-MM-DD HH:mm:ss");
    endTime = today.add(data.endDiff, data.unit).endOf("day").format("YYYY-MM-DD HH:mm:ss");
  } else if (data.unit == "week") {
    startTime = today.weekday(data.startDiff).startOf("day").format("YYYY-MM-DD HH:mm:ss");
    endTime = today.weekday(data.endDiff).endOf("day").format("YYYY-MM-DD HH:mm:ss");
  }
  console.log("startTime :>> ", startTime);
  console.log("endTime :>> ", endTime);
  return {
    ...data,
    startTime,
    endTime
  };
}

/**
 * 时间格式转义, detaTime to Diff
 * @param data {unit:string, startTime:string, endTime:string}
 * @returns
 */
export function setDateToDiff(data: any) {
  const today = dayjs(dayjs().format("YYYY-MM-DD"));
  const _start = dayjs(dayjs(data.startTime).format("YYYY-MM-DD"));
  const _end = dayjs(dayjs(data.endTime).format("YYYY-MM-DD"));
  let startDiff = 0,
    endDiff = 0;

  if (data.unit == "day") {
    startDiff = _start.diff(today, data.unit);
    endDiff = _end.diff(today, data.unit);
  } else if (data.unit == "week") {
    startDiff = _start.diff(today.startOf("week"), data.unit) * 7 + _start.day();
    endDiff = _end.diff(today.startOf("week"), data.unit) * 7 + _end.day();
  }

  return {
    ...data,
    startDiff,
    endDiff
  };
}
