import { useEffect, useMemo } from 'react';
import type { Dispatch, MutableRefObject, SetStateAction } from 'react';
import usePasskeys from '../../settings/usePasskeys';
import TagManager from '../../tag_manager';
import useCorrespondents from './useCorrespondents';
import useDocumentCorrespondentActions from './useDocumentCorrespondentActions';
import useTags from './useTags';
import type { Identifier } from '../../types/identifiers';

interface UseWorkspaceTaxonomiesArgs {
  apiClient: any;
  notifyApiError: (error: unknown, fallbackMessage?: string, variant?: string) => void;
  setStatusMessage: (message: string, variant?: string) => void;
  tagManager: TagManager;
  tenantIdRef: MutableRefObject<Identifier | null>;
  currentTenantId: Identifier | null;
  setActiveTagFilters: Dispatch<SetStateAction<Identifier[]>>;
  mapDocumentCaches: (mapper: (doc: any) => any | undefined) => void;
  updateDocumentCaches: (id: Identifier, updater: (doc: any) => any) => void;
  token: string;
}

const useWorkspaceTaxonomies = ({
  apiClient,
  notifyApiError,
  setStatusMessage,
  tagManager,
  tenantIdRef,
  currentTenantId,
  setActiveTagFilters,
  mapDocumentCaches,
  updateDocumentCaches,
  token,
}: UseWorkspaceTaxonomiesArgs) => {
  const {
    tags,
    refreshTags,
    handleTagCreate,
    handleTagUpdate,
    handleTagDelete,
    setTags,
  } = useTags({
    apiClient,
    notifyApiError,
    setStatusMessage,
    tagManager,
    tenantIdRef,
    setActiveTagFilters,
    mapDocumentCaches,
  });

  useEffect(() => {
    tenantIdRef.current = currentTenantId;
  }, [currentTenantId, tenantIdRef]);

  const tagLookupById = useMemo(() => {
    const map = new Map();
    tags.forEach((tag) => {
      if (tag?.id) {
        map.set(tag.id, tag);
      }
    });
    return map;
  }, [tags]);

  const {
    correspondents,
    refreshCorrespondents,
    handleCorrespondentCreate,
    handleCorrespondentUpdate,
    handleCorrespondentDelete,
    setCorrespondents,
  } = useCorrespondents({
    apiClient,
    notifyApiError,
    setStatusMessage,
    tenantIdRef,
    mapDocumentCaches,
  });

  const {
    correspondentLookupByName,
    handleDocumentCorrespondentAttach,
    handleCorrespondentRemove,
    handleCorrespondentAdd,
  } = useDocumentCorrespondentActions({
    apiClient,
    correspondents,
    handleCorrespondentCreate,
    notifyApiError,
    setStatusMessage,
    updateDocumentCaches,
  });

  const {
    passkeys,
    passkeysSupported,
    passkeysLoading,
    registeringPasskey,
    revokingPasskeyId,
    refreshPasskeys,
    registerPasskey,
    revokePasskey,
  } = usePasskeys({
    notifyApiError,
    setStatusMessage,
    token,
  });

  return {
    tags,
    refreshTags,
    handleTagCreate,
    handleTagUpdate,
    handleTagDelete,
    setTags,
    tagLookupById,
    correspondents,
    refreshCorrespondents,
    handleCorrespondentCreate,
    handleCorrespondentUpdate,
    handleCorrespondentDelete,
    setCorrespondents,
    correspondentLookupByName,
    handleDocumentCorrespondentAttach,
    handleCorrespondentRemove,
    handleCorrespondentAdd,
    passkeys,
    passkeysSupported,
    passkeysLoading,
    registeringPasskey,
    revokingPasskeyId,
    refreshPasskeys,
    registerPasskey,
    revokePasskey,
  };
};

export default useWorkspaceTaxonomies;
