"use client";
import { useState, useCallback } from "react";
import { useEntityQuery } from "./useEntityQuery";
import { useEntityMutations } from "./useEntityMutations";
function useEntity(options) {
  const {
    entityConfig,
    pageSize = 10,
    includeChildren = false,
    autoFetch = true
  } = options;
  const [searchQuery, setSearchQuery] = useState("");
  const [filters, setFilters] = useState({});
  const [sort, setSort] = useState(null);
  const [currentPage, setCurrentPage] = useState(1);
  const [validationErrors, setValidationErrors] = useState({});
  const {
    items,
    total,
    totalPages,
    isLoading,
    error: queryError,
    refetch
  } = useEntityQuery({
    entityConfig,
    pageSize,
    includeChildren,
    search: searchQuery,
    filters,
    sort: sort || void 0,
    enabled: autoFetch
  });
  const {
    create,
    update,
    delete: deleteItem,
    bulkDelete,
    isCreating,
    isUpdating,
    isDeleting
  } = useEntityMutations({
    entityConfig,
    onError: (error) => {
      if (error.message === "Validation failed") {
        setValidationErrors({});
      }
    }
  });
  const canCreate = true;
  const canRead = true;
  const canUpdate = true;
  const canDelete = true;
  const handleSetSearchQuery = useCallback((query) => {
    setSearchQuery(query);
    setCurrentPage(1);
  }, []);
  const handleSetFilters = useCallback((newFilters) => {
    setFilters(newFilters);
    setCurrentPage(1);
  }, []);
  const handleSetSort = useCallback((field, direction) => {
    setSort({ field, direction });
  }, []);
  const handleCreate = useCallback(
    async (data) => {
      setValidationErrors({});
      try {
        const result = await create(data);
        return result;
      } catch (error) {
        throw error;
      }
    },
    [create]
  );
  const handleUpdate = useCallback(
    async (id, data) => {
      setValidationErrors({});
      try {
        const result = await update({ id, data });
        return result;
      } catch (error) {
        throw error;
      }
    },
    [update]
  );
  const handleDelete = useCallback(
    async (id) => {
      try {
        await deleteItem(id);
      } catch (error) {
        throw error;
      }
    },
    [deleteItem]
  );
  const handleBulkDelete = useCallback(
    async (ids) => {
      try {
        await bulkDelete(ids);
      } catch (error) {
        throw error;
      }
    },
    [bulkDelete]
  );
  const refresh = useCallback(async () => {
    await refetch();
  }, [refetch]);
  const reset = useCallback(() => {
    setSearchQuery("");
    setFilters({});
    setSort(null);
    setCurrentPage(1);
    setValidationErrors({});
  }, []);
  const fetchOne = useCallback(async (id) => {
    console.warn("fetchOne not implemented in useEntity, use useEntityOne hook instead");
  }, []);
  const fetchAll = useCallback(
    async (options2 = {}) => {
      if (options2.page) setCurrentPage(options2.page);
      if (options2.search !== void 0) setSearchQuery(options2.search);
      if (options2.filters) setFilters(options2.filters);
      await refetch();
    },
    [refetch]
  );
  const createChild = useCallback(async () => {
    console.warn("Child operations not implemented in useEntity");
  }, []);
  const updateChild = useCallback(async () => {
    console.warn("Child operations not implemented in useEntity");
  }, []);
  const deleteChild = useCallback(async () => {
    console.warn("Child operations not implemented in useEntity");
  }, []);
  return {
    // Data
    items,
    item: null,
    // Not implemented in list hook
    childData: {},
    // Not implemented in list hook
    // Loading states
    isLoading,
    isCreating,
    isUpdating,
    isDeleting,
    // Error states
    error: (queryError == null ? void 0 : queryError.message) || null,
    validationErrors,
    // Pagination
    currentPage,
    totalPages,
    totalItems: total,
    // Search & filtering
    searchQuery,
    filters,
    sort,
    // Permissions
    canCreate,
    canRead,
    canUpdate,
    canDelete,
    // CRUD operations
    fetchAll,
    fetchOne,
    create: handleCreate,
    update: handleUpdate,
    delete: handleDelete,
    bulkDelete: handleBulkDelete,
    // Search & filtering
    setSearchQuery: handleSetSearchQuery,
    setFilters: handleSetFilters,
    setSort: handleSetSort,
    // Child entity operations
    createChild,
    updateChild,
    deleteChild,
    // Utility
    refresh,
    reset
  };
}
export {
  useEntity
};
