import type { ComputedRef, Ref } from 'vue';

/**
 * 文件职责：分类管理复制弹窗动作。
 * 1. 维护复制目标门店选中状态。
 * 2. 管理复制弹窗开关与提交行为。
 */
import type { StoreListItemDto } from '#/api/store';

import { message } from 'ant-design-vue';

interface CreateCopyActionsOptions {
  copyCandidates: ComputedRef<StoreListItemDto[]>;
  copyTargetStoreIds: Ref<string[]>;
  isCopyModalOpen: Ref<boolean>;
  isCopySubmitting: Ref<boolean>;
  selectedStoreId: Ref<string>;
}

export function createCopyActions(options: CreateCopyActionsOptions) {
  function openCopyModal() {
    if (!options.selectedStoreId.value) {
      message.warning('请先选择门店');
      return;
    }
    options.copyTargetStoreIds.value = [];
    options.isCopyModalOpen.value = true;
  }

  function setCopyModalOpen(value: boolean) {
    options.isCopyModalOpen.value = value;
  }

  function handleCopyCheckAll(checked: boolean) {
    options.copyTargetStoreIds.value = checked
      ? options.copyCandidates.value.map((item) => item.id)
      : [];
  }

  function toggleCopyStore(storeId: string, checked: boolean) {
    if (checked) {
      options.copyTargetStoreIds.value = [
        ...new Set([storeId, ...options.copyTargetStoreIds.value]),
      ];
      return;
    }
    options.copyTargetStoreIds.value = options.copyTargetStoreIds.value.filter(
      (item) => item !== storeId,
    );
  }

  async function handleCopySubmit() {
    if (options.copyTargetStoreIds.value.length === 0) {
      message.warning('请至少选择一个目标门店');
      return;
    }
    options.isCopySubmitting.value = true;
    try {
      message.info('复制到其他门店功能开发中');
      options.isCopyModalOpen.value = false;
      options.copyTargetStoreIds.value = [];
    } finally {
      options.isCopySubmitting.value = false;
    }
  }

  return {
    handleCopyCheckAll,
    handleCopySubmit,
    openCopyModal,
    setCopyModalOpen,
    toggleCopyStore,
  };
}
