import { apiConfig } from '@/config/api.config';
import React, { createContext, useCallback, useState } from 'react';

interface SucosProps {
  id: number;
  name: string;
}

interface ContextProps {
  sucos: SucosProps[];
  showSearchDialog: boolean;
  handleSearchDialog: (show: boolean) => void;
  showEditDialog: boolean;
  handleEditDialog: (show: boolean, selected_sucos: string | null) => void;
  showAddDialog: boolean;
  handleAddDialog: (show: boolean) => void;
  showDeleteDialog: boolean;
  handleDeleteDialog: (show: boolean, selected_sucos: string | null) => void;
  selectedSucos: string | null;
  getSucosLists: (
    limit: number,
    page: number,
    with_deleted: boolean,
    order_field: any,
    order_direction: any
  ) => Promise<{ data: SucosProps[]; totalCount: number } | undefined>;
}

const initialProps: ContextProps = {
  sucos: [],
  showSearchDialog: false,
  handleSearchDialog: () => {},
  showEditDialog: false,
  handleEditDialog: () => {},
  showAddDialog: false,
  handleAddDialog: () => {},
  showDeleteDialog: false,
  handleDeleteDialog: () => {},
  selectedSucos: null,
  getSucosLists: async () => undefined
};

const ManageSucosContext = createContext<ContextProps>(initialProps);
const API_URL = apiConfig.service_master_data;

const ManageSucosContextProvider = ({ children }: { children: React.ReactNode }) => {
  const [sucos, setSucos] = useState<SucosProps[]>([]);
  const [showSearchDialog, setShowSearchDialog] = useState(false);
  const [showEditDialog, setShowEditDialog] = useState(false);
  const [showAddDialog, setShowAddDialog] = useState(false);
  const [showDeleteDialog, setShowDeleteDialog] = useState(false);
  const [selectedSucos, setSelectedSucos] = useState<string | null>(null);

  const handleSearchDialog = useCallback((show: boolean) => {
    setShowSearchDialog(show);
  }, []);

  const handleAddDialog = useCallback((show: boolean) => {
    setShowAddDialog(show);
  }, []);

  const hanldeEditDialog = useCallback((show: boolean, selected_sucos: string | null) => {
    setSelectedSucos(show ? selected_sucos : null);
    setShowEditDialog(show);
  }, [])
};
