import { useMemo } from 'react';
import { APIError } from '@Models/GeneralTypes';
import {
  postMessageDialog,
  postMessageConfirm,
  removeMessageDialog,
  removeMessageConfirm,
} from '@Reducers/message/actions';
import { MessageDialogObject, MessageConfirmObject } from '@Models/Redux/Message/types';
import { errorCatch } from '@Reducers/_Capture/errorCapture';
import useDispatch from './useDispatch';

interface useMessageDefine {
  dialog: (dialog: MessageDialogObject) => void;
  confirm: (confirm: MessageConfirmObject) => void;
  removeDialog: () => void;
  removeConfirm: () => void;
  error: (error: APIError) => void;
}

/**
 * 描述 : 取得 Redux Action 裡面關於 Message 的 Action
 */

export default function useMessage(): useMessageDefine {
  /* Global & Local State */
  const dispatch = useDispatch();
  /* Functions */
  const apiCallPostMessageDialog = (dialog: MessageDialogObject): void => {
    dispatch(postMessageDialog(dialog));
  };
  const apiCallPostMessageConfirm = (confirm: MessageConfirmObject): void => {
    dispatch(postMessageConfirm(confirm));
  };
  const apiCallRemoveMessageDialog = (): void => {
    dispatch(removeMessageDialog());
  };
  const apiCallRemoveMessageConfirm = (): void => {
    dispatch(removeMessageConfirm());
  };
  const apiCallError = (error): void => {
    dispatch(errorCatch(error));
  };
  /* Main */
  return useMemo(
    () => ({
      dialog: apiCallPostMessageDialog,
      confirm: apiCallPostMessageConfirm,
      removeDialog: apiCallRemoveMessageDialog,
      removeConfirm: apiCallRemoveMessageConfirm,
      error: apiCallError,
    }),
    [],
  );
}
