/**
 * 表格行事件
 */

import React from 'react';
import Invoice from '../../../../..';
import IGood from '../../../../../InvoiceController/InvoiceControllerState/GoodsListState/IGood';
import { LineAttributeType } from '../../../../../InvoiceController';

export default () => {
  /** 控制器 */
  const controller = Invoice.useInvoiceController();

  const model = controller.useMemo((s) => s.model, []);

  const onClick = React.useCallback(
    async (record: IGood) => {
      // if (model === 'prefab') return;
      if (record.level && record.level > 0) return;
      if (model === 'readOnly') return;


      const editGood = controller.state.goodsListState.editGood;
      if (record.lineAttribute !== LineAttributeType.正常) return;

      if (editGood) {
        if (editGood.$index === record.$index) return;
        await controller.saveEditGood();
        // await controller.wait(2000);
        if (
          controller.getGoodsTaxRateList &&
          record.taxClassificationCode &&
          !record.goodsTaxRateList
        ) {
          record.goodsTaxRateList = await controller.getGoodsTaxRateList(
            record.taxClassificationCode,
          );
          // 最简单版本的模拟请求
          // record.goodsTaxRateList = await new Promise(resolve => {
          //   setTimeout(() => {
          //     // 根据税编返回不同的税率
          //     const taxRatesMap: Record<string, number[]> = {
          //       '001': [1.03, 0.06, 0.09],
          //       '002': [1.05, 0.10],
          //       '003': [1.06, 0.13],
          //       'default': [1.03, 0.06, 0.09, 0.13, 0.16]
          //     };

          //     const rates = taxRatesMap[record.taxClassificationCode+''] || taxRatesMap['default'];
          //     console.log(`模拟接口返回税率: ${rates.join(', ')}`);
          //     resolve(rates);
          //   }, 1300); // 300ms延迟模拟网络请求
          // });
          // if (record.taxRate !== undefined && record.taxRate !== null && !record.goodsTaxRateList.includes(record.taxRate)) {
          //   record.taxRate = undefined;
          // }
        }
        // await controller.pipeline(async s => { s.goodsListState.editGood ||  (await controller.setEditGood(record)) })();
        // await controller.pipeline(async (s) => {

        // })();

        await controller.setEditGood(record);
        await controller.pipeline(async (s) => { s.goodsListState.goodsList = s.goodsListState.goodsList.map(e => e.$index === record.$index ? record : e) })();
        // await controller.run(async (s) => { s.goodsListState.goodsListVersion++ });
      } else {
        if (
          controller.getGoodsTaxRateList &&
          record.taxClassificationCode &&
          !record.goodsTaxRateList
        ) {
          record.goodsTaxRateList = await controller.getGoodsTaxRateList(
            record.taxClassificationCode,
          );
          // 最简单版本的模拟请求
          // record.goodsTaxRateList = await new Promise(resolve => {
          //   setTimeout(() => {
          //     // 根据税编返回不同的税率
          //     const taxRatesMap: Record<string, number[]> = {
          //       '001': [0.03, 0.06, 0.09],
          //       '002': [0.05, 0.10],
          //       '003': [0.06, 0.13],
          //       'default': [0.03, 0.06, 0.09, 0.13, 0.16]
          //     };

          //     const rates = taxRatesMap[record.taxClassificationCode+''] || taxRatesMap['default'];
          //     console.log(`模拟接口返回税率: ${rates.join(', ')}`);
          //     resolve(rates);
          //   }, 1300); // 300ms延迟模拟网络请求
          // });
        }
        await controller.setEditGood(record);
        await controller.pipeline(async (s) => { s.goodsListState.goodsList = s.goodsListState.goodsList.map(e => e.$index === record.$index ? record : e) })()
        // await controller.run(async (s) => { s.goodsListState.goodsListVersion++ });
      }
    },
    [controller, model],
  );

  return {
    onClick,
  };
};
