import moment from "moment";
import * as echarts from "echarts";
import { pieOrBarOrFunnlColor } from "./config";

export const getTime = function (val) {
  const arr = ["日", "一", "二", "三", "四", "五", "六", ""];
  return {
    time: val ? moment(val).format("MM-DD") : "",
    index: arr[val ? moment(val).day() : 7],
  };
};
/**
 *  @params {'YYYY-MM-DD'|'YYYYMMDD'|timeStamp} date
 *  @return {'dom'}
 **/
export const getHtml = function (date) {
  const obj = getTime(date);
  return `<div>${obj.time}（${obj.index}）</div>`;
};

/**
 *  @params {Array} order
 *  @return {Array}
 * **/
export function getColorList(order) {
  if (order && order.length && order.length <= pieOrBarOrFunnlColor.length) {
    return order.map((cur) => {
      return pieOrBarOrFunnlColor[cur];
    });
  }
  return pieOrBarOrFunnlColor;
}
/**
 *  @params {any} v
 *  @return {Boolean}
 * */
export function isNumerical(v) {
  if (Number.isNaN(v)) return false;
  if (typeof v === "number") return true;
  if (typeof v === "string")
    return /^(-|\+)?\d+(\.\d+)?(E(\+|-)?\d+)?$/i.test(v);
  return false;
}
/**
 *  @params {any} value
 *  @params { Number | String } decimals
 *  @defaultValue {any}
 *  @return { String }
 **/
export function toFormatMoney(value, decimals = 0, defaultValue = "--") {
  if (isNumerical(value)) {
    const _v = parseFloat(value);
    return _v.toLocaleString(undefined, {
      minimumFractionDigits: Number(decimals),
      maximumFractionDigits: Number(decimals),
    });
  }
  return defaultValue;
}

export function getChartColor(arr, color) {
  return new echarts.graphic.LinearGradient(
    ...arr,
    [
      {
        offset: 0,
        color: (color && color[0]) || "#5058FF",
      },
      {
        offset: 1,
        color: (color && color[1]) || "#8891FF",
      },
    ],
    false
  );
}
