import type { MemberTierEditorForm } from '../../types';

import type { MemberCouponPickerItemDto } from '#/api/member';

interface CouponActionOptions {
  couponItems: { value: MemberCouponPickerItemDto[] };
  couponKeyword: { value: string };
  couponModalOpen: { value: boolean };
  couponTarget: { value: '' | 'birthday' | 'monthly' };
  loadCouponItems: (keyword: string) => Promise<void>;
  selectedCouponIds: { value: string[] };
  tierForm: { value: MemberTierEditorForm | null };
}

/**
 * 文件职责：等级编辑抽屉内优惠券选择动作。
 */
export function createCouponActions(options: CouponActionOptions) {
  function setCouponModalOpen(value: boolean) {
    options.couponModalOpen.value = value;
    if (!value) {
      options.selectedCouponIds.value = [];
      options.couponTarget.value = '';
      options.couponKeyword.value = '';
    }
  }

  function setCouponKeyword(value: string) {
    options.couponKeyword.value = value;
  }

  async function openCouponPicker(
    target: 'birthday' | 'monthly',
    selectedIds: string[],
  ) {
    options.couponTarget.value = target;
    options.selectedCouponIds.value = [...selectedIds];
    options.couponModalOpen.value = true;
    options.couponKeyword.value = '';
    await options.loadCouponItems('');
  }

  async function searchCoupons() {
    await options.loadCouponItems(options.couponKeyword.value);
  }

  function toggleCoupon(couponId: string) {
    const exists = options.selectedCouponIds.value.includes(couponId);
    if (exists) {
      options.selectedCouponIds.value = options.selectedCouponIds.value.filter(
        (item) => item !== couponId,
      );
      return;
    }

    options.selectedCouponIds.value = [
      ...options.selectedCouponIds.value,
      couponId,
    ];
  }

  function applyCoupons() {
    if (!options.tierForm.value || !options.couponTarget.value) {
      setCouponModalOpen(false);
      return;
    }

    options.tierForm.value =
      options.couponTarget.value === 'birthday'
        ? {
            ...options.tierForm.value,
            benefits: {
              ...options.tierForm.value.benefits,
              birthday: {
                ...options.tierForm.value.benefits.birthday,
                couponTemplateIds: [...options.selectedCouponIds.value],
              },
            },
          }
        : {
            ...options.tierForm.value,
            benefits: {
              ...options.tierForm.value.benefits,
              monthlyCoupon: {
                ...options.tierForm.value.benefits.monthlyCoupon,
                couponTemplateIds: [...options.selectedCouponIds.value],
              },
            },
          };

    setCouponModalOpen(false);
  }

  function resolveCouponLabelMap() {
    const labelMap: Record<string, string> = {};
    for (const item of options.couponItems.value) {
      labelMap[item.couponTemplateId] = item.displayText || item.name;
    }
    return labelMap;
  }

  return {
    applyCoupons,
    openCouponPicker,
    resolveCouponLabelMap,
    searchCoupons,
    setCouponKeyword,
    setCouponModalOpen,
    toggleCoupon,
  };
}
