'use client';

import { useMemo } from 'react';
import type { BillFormData } from '../types';
import {
  RECEIVED_STATUS_OPTIONS,
  RECEIVED_CHECK_STATUS_OPTIONS,
  ISSUED_STATUS_OPTIONS,
  ISSUED_CHECK_STATUS_OPTIONS,
  PAYMENT_PLACE_OPTIONS,
  PAYMENT_PLACE_CHECK_OPTIONS,
} from '../constants';

export function useBillConditions(formData: BillFormData) {
  return useMemo(() => {
    const isReceived = formData.direction === 'received';
    const isIssued = formData.direction === 'issued';
    const isCheck = formData.instrumentType === 'cashierCheck' || formData.instrumentType === 'currentCheck';
    const isBill = !isCheck;
    const canBeElectronic = formData.instrumentType === 'promissory';
    const isElectronic = formData.medium === 'electronic';

    const currentStatus = isReceived ? formData.receivedStatus : formData.issuedStatus;

    // 조건부 섹션 표시 플래그
    const showElectronic = isElectronic;
    const showExchangeBill = formData.instrumentType === 'exchange';
    const showDiscount = isReceived && formData.isDiscounted && isBill;
    const showEndorsement = isReceived && formData.receivedStatus === 'endorsed';
    const showCollection = isReceived && formData.receivedStatus === 'collected';
    const showDishonored = currentStatus === 'dishonored';
    const showRenewal = currentStatus === 'renewed' && isBill;
    const showRecourse = isReceived && formData.receivedStatus === 'recourse';
    const showBuyback = isReceived && formData.receivedStatus === 'buyback' && isBill;
    const showAcceptanceRefusal = showExchangeBill && formData.acceptanceStatus === 'refused';

    // 현재 증권종류에 맞는 옵션 목록
    const receivedStatusOptions = isCheck ? RECEIVED_CHECK_STATUS_OPTIONS : RECEIVED_STATUS_OPTIONS;
    const issuedStatusOptions = isCheck ? ISSUED_CHECK_STATUS_OPTIONS : ISSUED_STATUS_OPTIONS;
    const paymentPlaceOptions = isCheck ? PAYMENT_PLACE_CHECK_OPTIONS : PAYMENT_PLACE_OPTIONS;

    // 분할배서 최대 횟수
    const maxSplitCount = isElectronic ? 4 : 10;

    return {
      isReceived,
      isIssued,
      isCheck,
      isBill,
      canBeElectronic,
      isElectronic,
      currentStatus,
      showElectronic,
      showExchangeBill,
      showDiscount,
      showEndorsement,
      showCollection,
      showDishonored,
      showRenewal,
      showRecourse,
      showBuyback,
      showAcceptanceRefusal,
      receivedStatusOptions,
      issuedStatusOptions,
      paymentPlaceOptions,
      maxSplitCount,
    };
  }, [formData.direction, formData.instrumentType, formData.medium, formData.isDiscounted, formData.receivedStatus, formData.issuedStatus, formData.acceptanceStatus]);
}
