export const CommonFunctions = {
  stringTruncate: (str: string, len: number) => {
    return str.length > len ? str.slice(0, len) + "..." : str;
  },
  getFileExtension: (filename: string) => {
    const index = filename.lastIndexOf(".");
    return index !== -1 ? filename.substring(index + 1).toLowerCase() : null;
  },
  getDateFormatYmd: (date: string) => {
    if (date != null && date != "") {
      const inputDate = new Date(date);
      const year = inputDate.getFullYear();
      const month = (inputDate.getMonth() + 1).toString().padStart(2, "0");
      const day = inputDate.getDate().toString().padStart(2, "0");
      const formattedDate = `${year}.${month}.${day}`;

      return formattedDate;
    } else {
      return "";
    }
  },
  getDateFormatHis: (date: string) => {
    if (date != null && date != "") {
      const timeMatch = date.match(/\b\d{2}:\d{2}:\d{2}\b/);
      if (timeMatch) {
        return timeMatch[0];
      } else {
        return "";
      }
    } else {
      return "";
    }
  },
  getQuantumDateFormat: (dateString: string) => {
    if (dateString != null && dateString != "" && dateString != "-") {
      const date = new Date(dateString);

      // 날짜와 시간을 각각 포맷팅
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");
      const hours = String(date.getHours()).padStart(2, "0");
      const minutes = String(date.getMinutes()).padStart(2, "0");
      const seconds = String(date.getSeconds()).padStart(2, "0");

      return `${year}.${month}.${day} ${hours}:${minutes}:${seconds}`;
    } else {
      return "-";
    }
    dateString;
  },
  getQuantumDateFormatToCustom: (dateString: string) => {
    if (dateString != null && dateString != "" && dateString != "-") {
      const date = new Date(dateString);

      // toLocaleString을 사용해 형식 지정
      return date.toLocaleString("en-US", {
        month: "short", // Oct
        day: "2-digit", // 07
        year: "numeric", // 2024
        hour: "2-digit", // 10
        minute: "2-digit", // 37
        hour12: true, // AM/PM 형식
      });
    } else {
      return "-";
    }
  },
  getTimeDifferenceInSeconds: (dateString1: string, dateString2: string) => {
    const date1 = new Date(dateString1);
    const date2 = new Date(dateString2);

    // 두 날짜의 밀리초 차이 계산
    const differenceInMs = Math.abs(date1.getTime() - date2.getTime());

    // 밀리초 차이를 초 단위로 변환
    const differenceInSeconds = Math.floor(differenceInMs / 1000);
    return differenceInSeconds;
  },
  getJobChartData: (data: string[]) => {
    const binaryMapping: Record<string, string> = {
      "0x0": "00",
      "0x1": "01",
      "0x2": "10",
      "0x3": "11",
    };

    const frequency: Record<string, number> = {};
    data.forEach((d) => {
      const binary = binaryMapping[d] || "00";
      frequency[binary] = (frequency[binary] || 0) + 1;
    });

    return frequency;
  },
};
