import type { OrderAllDetailDto } from '#/api/order';

import { getOrderAllDetailApi } from '#/api/order';

interface DrawerActionOptions {
  detail: { value: null | OrderAllDetailDto };
  isDetailLoading: { value: boolean };
  isDrawerOpen: { value: boolean };
  selectedStoreId: { value: string };
}

/**
 * 文件职责：全部订单详情抽屉动作。
 */
export function createDrawerActions(options: DrawerActionOptions) {
  function setDrawerOpen(value: boolean) {
    options.isDrawerOpen.value = value;
    if (!value) {
      options.detail.value = null;
    }
  }

  async function openDetail(orderNo: string) {
    if (!options.selectedStoreId.value || !orderNo) {
      return;
    }

    options.isDrawerOpen.value = true;
    options.detail.value = null;
    options.isDetailLoading.value = true;
    try {
      options.detail.value = await getOrderAllDetailApi({
        storeId: options.selectedStoreId.value,
        orderNo,
      });
    } finally {
      options.isDetailLoading.value = false;
    }
  }

  return {
    openDetail,
    setDrawerOpen,
  };
}
