import { apiQueryPorts } from '@/services/slave/sgw/ShipController';
import { useCallback, useState } from 'react';

export default function useHomeport() {
  const [homeports, setHomeports] = useState<SgwModel.Port[]>([]);
  const [loading, setLoading] = useState(false);

  const getHomeportsByProvinceCode = useCallback(async () => {
    setLoading(true);
    try {
      const params: SgwModel.PortQueryParams = {
        name: '',
        order: 'name',
        dir: 'asc',
        limit: 100,
        offset: 0,
      };

      console.log('Calling apiQueryPorts with params:', params);
      const res = await apiQueryPorts(params);
      console.log('apiQueryPorts response:', res);
      setHomeports(res?.ports || []);
    } catch (err) {
      console.error('Fetch Homeports failed:', err);
      setHomeports([]);
    } finally {
      setLoading(false);
    }
  }, []);

  return {
    homeports,
    loading,
    getHomeportsByProvinceCode,
  };
}
