var _excluded = ["label", "key", "count", "value", "tax", "hidden"],
  _excluded2 = ["list", "mode", "describe"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useEffect, useMemo } from 'react';
import { Divider } from 'antd';
import "./index.less";
import { getLocale } from "../../utils/locales";
import localeTexts from "./locales";
import classNames from 'classnames';
import { PisellText, Iconfont, Tooltip } from '@pisell/materials';
import { locales } from '@pisell/utils';

// 先定义一个内置key的类型

locales.init(localeTexts, getLocale());
var labelMaps = {
  deposit: locales.getText('pisell2.subtotal.deposit'),
  total: locales.getText('pisell2.subtotal.total'),
  sub_total: locales.getText('pisell2.subtotal.subtotal'),
  tax: locales.getText('pisell2.subtotal.tax'),
  delivery_fee: locales.getText('pisell2.subtotal.delivery-fee'),
  discount: locales.getText('pisell2.subtotal.discount'),
  point_deduction: locales.getText('pisell2.subtotal.point-deduction'),
  member_deduction: locales.getText('pisell2.subtotal.member-deduction')
};
var groupByMod = function groupByMod(arr, groupCount) {
  // 参数验证
  if (groupCount < 1) {
    throw new Error('Group count must be greater than 0');
  }
  if (arr.length === 0) {
    return Array(groupCount).fill([]);
  }

  // 创建结果数组并初始化
  var result = Array(groupCount).fill(null).map(function () {
    return [];
  });

  // 使用 reduce 优化遍历过程
  return arr.reduce(function (acc, item, index) {
    var groupIndex = index % groupCount;
    acc[groupIndex].push(item);
    return acc;
  }, result);
};
var Item = function Item(props) {
  var item = props.item,
    bold = props.bold;
  var label = item.label,
    key = item.key,
    count = item.count,
    value = item.value,
    tax = item.tax,
    hidden = item.hidden,
    rest = _objectWithoutProperties(item, _excluded);
  var taxText = useMemo(function () {
    if (!tax) {
      return '';
    }
    var str = '';
    str = str + (tax === null || tax === void 0 ? void 0 : tax.title);
    str = str + ' ' + Number(tax.rate) * 100 + '%';

    // 判断税费是 包含在订单内  还是单独的
    if (tax !== null && tax !== void 0 && tax.is_price_include_tax) {
      str = str + ' ' + locales.getText('pisell2.subtotal.included');
    } else {
      str = str + ' ' + locales.getText('pisell2.subtotal.not-contain');
    }
    return str;
  }, [tax]);
  var renderLabel = useMemo(function () {
    if (label) {
      return label;
    }
    if (key === 'sub_total' && count !== undefined) {
      return /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("span", null, "".concat(labelMaps[key])), /*#__PURE__*/React.createElement("span", {
        style: {
          color: '#667085',
          paddingLeft: '8px'
        }
      }, "".concat(count || 0, " ").concat(locales.getText('pisell2.subtotal.items'))));
    }
    if (key === 'tax') {
      return /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("span", null, "".concat(labelMaps[key])), /*#__PURE__*/React.createElement(Tooltip, {
        title: taxText
      }, /*#__PURE__*/React.createElement(Iconfont, {
        style: {
          color: '#667085',
          paddingLeft: '8px',
          fontSize: '16px'
        },
        type: "pisell2-help-circle"
      })));
    }
    return labelMaps[key];
  }, [label, key, count, taxText]);
  if (hidden) return null;
  return /*#__PURE__*/React.createElement("div", _extends({
    className: classNames('pisell-lowcode__subtotal-item', {
      'pisell-lowcode__subtotal-item-bold': bold
    })
  }, rest), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__subtotal-item-label"
  }, renderLabel), /*#__PURE__*/React.createElement(PisellText.Amount, {
    value: value,
    className: "pisell-lowcode__subtotal-item-value"
  }));
};
var Describe = function Describe(props) {
  var describe = props.describe;
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Divider, {
    className: "pisell-lowcode__subtotal-divider"
  }), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__subtotal-describe"
  }, describe));
};
var defaultList = [{
  key: 'deposit',
  value: 0
}, {
  key: 'total',
  value: 0
}, {
  key: 'sub_total',
  value: 0,
  count: 1
}, {
  key: 'tax',
  value: 0,
  tax: {
    rate: 0.1,
    is_price_include_tax: true,
    title: 'GST'
  }
}, {
  key: 'delivery_fee',
  value: 0
}, {
  key: 'point_deduction',
  value: 0
}, {
  key: 'member_deduction',
  value: 0
}];
var SubTotal = function SubTotal(props) {
  var _props$list = props.list,
    list = _props$list === void 0 ? [] : _props$list,
    _props$mode = props.mode,
    mode = _props$mode === void 0 ? 'column1' : _props$mode,
    _props$describe = props.describe,
    describe = _props$describe === void 0 ? '' : _props$describe,
    rest = _objectWithoutProperties(props, _excluded2);
  var depositItem = useMemo(function () {
    return list.find(function (item) {
      return item.key === 'deposit';
    });
  }, [list]);
  var totalItem = useMemo(function () {
    return list.find(function (item) {
      return item.key === 'total';
    });
  }, [list]);
  var headerItem = useMemo(function () {
    if (depositItem) {
      return depositItem;
    }
    if (totalItem) {
      return totalItem;
    }
    return null;
  }, [depositItem, totalItem]);
  var mapData = useMemo(function () {
    if (list.length === 0) {
      return [];
    }
    var filterList = [];

    // 定金过滤出去，在上面展示
    if (depositItem) {
      filterList.push('deposit');
    }

    // 当没有定金时，总价过滤出去，在上面展示
    if (totalItem) {
      filterList.push('total');
    }
    var groupCount = mode === 'column2' ? 2 : 1;
    return groupByMod(list.filter(function (d) {
      return !filterList.includes(d.key);
    }), groupCount);
  }, [list, depositItem, totalItem]);
  useEffect(function () {
    document.body.id = 'body';
  }, []);
  return /*#__PURE__*/React.createElement("div", _extends({
    className: 'pisell-lowcode__subtotal-wrap'
  }, rest), headerItem && /*#__PURE__*/React.createElement(Item, {
    key: headerItem.key,
    item: headerItem,
    bold: true
  }), depositItem ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Divider, {
    className: "pisell-lowcode__subtotal-divider"
  }), totalItem ? /*#__PURE__*/React.createElement(Item, {
    key: totalItem.key,
    item: totalItem
  }) : null) : null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__subtotal-list"
  }, mapData.map(function (items, index) {
    var isLast = index === mapData.length - 1;
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
      className: "pisell-lowcode__subtotal-item-block"
    }, /*#__PURE__*/React.createElement("div", {
      key: index,
      className: 'pisell-lowcode__subtotal-item-wrap'
    }, items.map(function (d) {
      return /*#__PURE__*/React.createElement(Item, {
        key: d.key,
        item: d
      });
    }))), mode === 'column2' && !isLast ? /*#__PURE__*/React.createElement(Divider, {
      type: "vertical",
      style: {
        height: 'auto'
      }
    }) : null);
  })), describe ? /*#__PURE__*/React.createElement(Describe, {
    describe: describe
  }) : null);
};
export default SubTotal;