"use client";

import * as React from "react";
import { useState, useCallback } from "react";
import { SimpleVendorMaterials } from "./simple-vendor-materials";
import { VendorPossibleMaterial, getAllConfirmedMaterials, getAllVendorInputMaterials } from "../vendor-possible-material-service";

interface SimpleVendorMaterialsWrapperProps {
  vendorId: number;
  initialConfirmedMaterials: VendorPossibleMaterial[];
  initialVendorInputMaterials: VendorPossibleMaterial[];
}

export function SimpleVendorMaterialsWrapper({
  vendorId,
  initialConfirmedMaterials,
  initialVendorInputMaterials,
}: SimpleVendorMaterialsWrapperProps) {
  const [confirmedMaterials, setConfirmedMaterials] = useState(initialConfirmedMaterials);
  const [vendorInputMaterials, setVendorInputMaterials] = useState(initialVendorInputMaterials);
  const [isRefreshing, setIsRefreshing] = useState(false);

  const handleDataRefresh = useCallback(async () => {
    setIsRefreshing(true);
    try {
      const [newConfirmedMaterials, newVendorInputMaterials] = await Promise.all([
        getAllConfirmedMaterials(vendorId),
        getAllVendorInputMaterials(vendorId)
      ]);
      
      setConfirmedMaterials(newConfirmedMaterials);
      setVendorInputMaterials(newVendorInputMaterials);
    } catch (error) {
      console.error("데이터 새로고침 실패:", error);
    } finally {
      setIsRefreshing(false);
    }
  }, [vendorId]);

  return (
    <SimpleVendorMaterials
      vendorId={vendorId}
      confirmedMaterials={confirmedMaterials}
      vendorInputMaterials={vendorInputMaterials}
      onDataRefresh={handleDataRefresh}
    />
  );
}
