import XEUtils from "xe-utils";
function hasTimestampValueType(valueFormat) {
  return valueFormat === "timestamp";
}
function hasDateValueType(valueFormat) {
  return valueFormat === "date";
}
function handleValueFormat(type, valueFormat) {
  if (valueFormat) {
    if (!(hasDateValueType(valueFormat) || hasTimestampValueType(valueFormat))) {
      return valueFormat;
    }
  }
  if (type === "time") {
    return "HH:mm:ss";
  }
  if (type === "datetime") {
    return "yyyy-MM-dd HH:mm:ss";
  }
  return "yyyy-MM-dd";
}
function toStringTimeDate(str) {
  const rest = new Date(2e3, 0, 1);
  if (str) {
    let h = 0;
    let m = 0;
    let s = 0;
    if (XEUtils.isNumber(str) || /^[0-9]{11,15}$/.test(`${str}`)) {
      str = new Date(Number(str));
    }
    if (XEUtils.isDate(str)) {
      h = str.getHours();
      m = str.getMinutes();
      s = str.getSeconds();
    } else {
      str = XEUtils.toValueString(str);
      const parses = str.match(/^(\d{1,2})(:(\d{1,2}))?(:(\d{1,2}))?/);
      if (parses) {
        h = XEUtils.toNumber(parses[1]);
        m = XEUtils.toNumber(parses[3]);
        s = XEUtils.toNumber(parses[5]);
      }
    }
    rest.setHours(h);
    rest.setMinutes(m);
    rest.setSeconds(s);
    return rest;
  }
  return rest;
}
function getDateQuarter(date) {
  const month = date.getMonth();
  if (month < 3) {
    return 1;
  } else if (month < 6) {
    return 2;
  } else if (month < 9) {
    return 3;
  }
  return 4;
}
const parseDateValue = (val, type, options) => {
  const { valueFormat } = options;
  if (val) {
    if (type === "time") {
      return toStringTimeDate(val);
    }
    if (XEUtils.isNumber(val) || /^[0-9]{10,15}$/.test(`${val}`)) {
      return new Date(Number(val));
    }
    if (XEUtils.isString(val)) {
      return XEUtils.toStringDate(XEUtils.last(val.split(",")), valueFormat);
    }
    return XEUtils.toStringDate(val, valueFormat);
  }
  return null;
};
const parseDateString = (val, type, options) => {
  const dValue = parseDateValue(val, type, options);
  return dValue ? XEUtils.toDateString(dValue, options.valueFormat) : "";
};
function parseDateObj(val, type, options) {
  const { labelFormat, firstDay } = options;
  let dValue = null;
  let dLabel = "";
  if (val) {
    dValue = parseDateValue(val, type, options);
  }
  if (XEUtils.isValidDate(dValue)) {
    dLabel = XEUtils.toDateString(dValue, labelFormat, { firstDay });
    if (labelFormat && type === "week") {
      const oldYyyy = dValue.getFullYear();
      const M = dValue.getMonth() + 1;
      const W = XEUtils.getYearWeek(dValue, firstDay);
      if (checkWeekOfsetYear(W, M)) {
        const formatY = "yyyy";
        const newYyyy = oldYyyy + 1;
        const yyIndex = labelFormat.indexOf(formatY);
        if (yyIndex > -1) {
          dLabel = dLabel.substring(0, yyIndex) + newYyyy + dLabel.substring(yyIndex + formatY.length);
        }
      }
    }
  } else {
    dValue = null;
  }
  return { label: dLabel, value: dValue };
}
function getDateByCode(code, val, type, options) {
  const { valueFormat, firstDay } = options;
  let dValue = null;
  const value = (code === "prev" || code === "next" ? /* @__PURE__ */ new Date() : val ? parseDateValue(val, type, options) : null) || /* @__PURE__ */ new Date();
  switch (code) {
    case "prev":
    case "next":
    case "minus":
    case "plus": {
      const offsetNum = code === "plus" || code === "next" ? 1 : -1;
      switch (type) {
        case "date":
        case "datetime":
          dValue = XEUtils.getWhatDay(value, offsetNum);
          break;
        case "week":
          dValue = XEUtils.getWhatWeek(value, offsetNum, firstDay, firstDay);
          break;
        case "month":
          dValue = XEUtils.getWhatMonth(value, offsetNum);
          break;
        case "quarter":
          dValue = XEUtils.getWhatQuarter(value, offsetNum);
          break;
        case "year":
          dValue = XEUtils.getWhatYear(value, offsetNum);
          break;
      }
      break;
    }
    default:
      dValue = /* @__PURE__ */ new Date();
      break;
  }
  return {
    value: dValue ? XEUtils.toDateString(dValue, valueFormat) : ""
  };
}
const rangeDateOffsetNumMaps = {
  last180: -180,
  last90: -90,
  last60: -60,
  last30: -30,
  last7: -7,
  last3: -3,
  last1: -1
};
function getRangeDateOffsetNum(code) {
  return rangeDateOffsetNumMaps[code] || 0;
}
function getRangeDateByCode(code, val, type, options) {
  const { valueFormat, firstDay } = options;
  if (XEUtils.isArray(val)) {
    val = val.join("");
  }
  const value = (val ? parseDateValue(val, type, options) : null) || /* @__PURE__ */ new Date();
  let sValue = null;
  const eValue = value;
  switch (code) {
    case "last1":
    case "last3":
    case "last7":
    case "last30":
    case "last60":
    case "last90":
    case "last180": {
      const offsetNum = getRangeDateOffsetNum(code);
      switch (type) {
        case "date":
        case "datetime":
          sValue = XEUtils.getWhatDay(value, offsetNum);
          break;
        case "week":
          sValue = XEUtils.getWhatWeek(value, offsetNum, firstDay, firstDay);
          break;
        case "month":
          sValue = XEUtils.getWhatMonth(value, offsetNum);
          break;
        case "quarter":
          sValue = XEUtils.getWhatQuarter(value, offsetNum);
          break;
        case "year":
          sValue = XEUtils.getWhatYear(value, offsetNum);
          break;
      }
      break;
    }
    default:
      sValue = /* @__PURE__ */ new Date();
      break;
  }
  const startValue = sValue ? XEUtils.toDateString(sValue, valueFormat) : "";
  const endValue = eValue ? XEUtils.toDateString(eValue, valueFormat) : "";
  return {
    startValue,
    endValue
  };
}
const checkWeekOfsetYear = (W, M) => {
  return `${W}` === "1" && `${M}` === "12";
};
export {
  getDateByCode,
  getDateQuarter,
  getRangeDateByCode,
  handleValueFormat,
  hasDateValueType,
  hasTimestampValueType,
  parseDateObj,
  parseDateString,
  parseDateValue,
  toStringTimeDate
};
