import { useState, useEffect } from 'react';
import axios from 'axios';
import { apiConfig } from '@/config/api.config';

const API_URL = apiConfig.service_dashboard;

interface YearData {
  year: string;
}
interface UseFetchselectYearResult {
  selectYear: YearData[];
  isselectYearLoading: boolean;
  selectYearError: any;
}

const useFetchYear = (
  path: string = '',
  start_date: string = '',
  end_date: string = '',
  year: string = ''
): UseFetchselectYearResult => {
  const [selectYear, setSelectYear] = useState<YearData[]>([]);
  const [isselectYearLoading, setIsselectYearLoading] = useState<boolean>(false);
  const [selectYearError, setSelectYearError] = useState<any>(null);

  useEffect(() => {
    const fetchData = async () => {
      try {
        setIsselectYearLoading(true);
        const response = await axios.get(`${API_URL}/dashboard/year`);
        setSelectYear(response.data.data.map((item: { year: string }) => ({ year: item.year })));
      } catch (err) {
        setSelectYearError(err);
      } finally {
        setIsselectYearLoading(false);
      }
    };

    fetchData();
  }, [path, start_date, end_date, year]);

  return { selectYear, isselectYearLoading, selectYearError };
};

export { useFetchYear };
