// The Lucia project.
// Copyright 2023-2026 DSP, inc.  All rights reserved.
// Authors:
//   chiayin.kuo@dsp.im (chiayin), 2023/1/31
//   imacat.yang@dsp.im (imacat), 2023/9/23
//   cindy.chang@dsp.im (Cindy Chang), 2024/5/30
/**
 * @module stores/performance Performance analysis store for fetching
 *   and managing frequency/duration chart data.
 */

import { defineStore } from "pinia";
import apiClient from "@/api/client.js";
import apiError from "@/module/apiError.js";

/** Pinia store for the Discover Performance page data. */
export const usePerformanceStore = defineStore("performanceStore", {
  state: () => ({
    allPerformanceData: null,
    freqChartData: null,
    freqChartOptions: null,
    freqChartXData: {
      minX: null,
      maxX: null,
      xData: null,
      content: null,
    },
  }),
  getters: {
    performanceData: (state) => {
      return state.allPerformanceData;
    },
  },
  actions: {
    /**
     * Get Performance
     * @param {string} type - The file type ('log' or 'filter').
     * @param {number} id - The file ID.
     */
    async getPerformance(type, id) {
      let api = "";

      switch (type) {
        case "log":
          api = `/api/logs/${id}/performance`;
          break;
        case "filter":
          api = `/api/filters/${id}/performance`;
          break;
      }
      try {
        const response = await apiClient.get(api);
        this.allPerformanceData = response.data;
      } catch (error) {
        apiError(error, "Failed to load the Performance.");
      }
    },
    /**
     * In PrimeVue format
     * @param {object} freqChartData
     */
    setFreqChartData(freqChartData) {
      this.freqChartData = freqChartData;
    },
    /**
     * In PrimeVue format
     * @param {object} freqChartOptions
     */
    setFreqChartOptions(freqChartOptions) {
      this.freqChartOptions = freqChartOptions;
    },
    /**
     *
     * @param {object} freqChartXData
     */
    setFreqChartXData(freqChartXData) {
      this.freqChartXData = freqChartXData;
    },
  },
});
