import React, { useMemo } from 'react';
import { locales } from '@pisell/utils';
import Iconfont from "../../../../iconfont";
import "./index.less";
var PREFIX = 'pisell-line-item';
var holderIconMap = {
  form: 'pisell2-file-05',
  minors: 'pisell2-user-check-01',
  waiver: 'pisell2-file-lock-03'
};
var isEmptyHolderId = function isEmptyHolderId(value) {
  if (Array.isArray(value)) return value.length === 0;
  return value === undefined || value === null || value === '';
};
var normalizeHolderIds = function normalizeHolderIds(value) {
  if (isEmptyHolderId(value)) return [];
  return Array.isArray(value) ? value : [value];
};
var pickHolderId = function pickHolderId(dataSource) {
  var _ref, _ref2, _dataSource$holder_id, _dataSource$metadata, _dataSource$_extend, _dataSource$_extend2;
  return (_ref = (_ref2 = (_dataSource$holder_id = dataSource === null || dataSource === void 0 ? void 0 : dataSource.holder_id) !== null && _dataSource$holder_id !== void 0 ? _dataSource$holder_id : dataSource === null || dataSource === void 0 || (_dataSource$metadata = dataSource.metadata) === null || _dataSource$metadata === void 0 ? void 0 : _dataSource$metadata.holder_id) !== null && _ref2 !== void 0 ? _ref2 : dataSource === null || dataSource === void 0 || (_dataSource$_extend = dataSource._extend) === null || _dataSource$_extend === void 0 || (_dataSource$_extend = _dataSource$_extend.product) === null || _dataSource$_extend === void 0 || (_dataSource$_extend = _dataSource$_extend.metadata) === null || _dataSource$_extend === void 0 ? void 0 : _dataSource$_extend.holder_id) !== null && _ref !== void 0 ? _ref : dataSource === null || dataSource === void 0 || (_dataSource$_extend2 = dataSource._extend) === null || _dataSource$_extend2 === void 0 || (_dataSource$_extend2 = _dataSource$_extend2.product) === null || _dataSource$_extend2 === void 0 || (_dataSource$_extend2 = _dataSource$_extend2._extend) === null || _dataSource$_extend2 === void 0 ? void 0 : _dataSource$_extend2.holder_id;
};
var pickHolderMaxCount = function pickHolderMaxCount(dataSource, holderIds) {
  var _ref3, _ref4, _ref5, _ref6, _ref7, _dataSource$holderMax, _dataSource$_extend3, _dataSource$_extend4, _dataSource$_extend5;
  var count = ((_ref3 = (_ref4 = (_ref5 = (_ref6 = (_ref7 = (_dataSource$holderMax = dataSource === null || dataSource === void 0 ? void 0 : dataSource.holderMaxCount) !== null && _dataSource$holderMax !== void 0 ? _dataSource$holderMax : dataSource === null || dataSource === void 0 ? void 0 : dataSource.holder_max_count) !== null && _ref7 !== void 0 ? _ref7 : dataSource === null || dataSource === void 0 ? void 0 : dataSource.number) !== null && _ref6 !== void 0 ? _ref6 : dataSource === null || dataSource === void 0 || (_dataSource$_extend3 = dataSource._extend) === null || _dataSource$_extend3 === void 0 || (_dataSource$_extend3 = _dataSource$_extend3.product) === null || _dataSource$_extend3 === void 0 ? void 0 : _dataSource$_extend3.num) !== null && _ref5 !== void 0 ? _ref5 : dataSource === null || dataSource === void 0 || (_dataSource$_extend4 = dataSource._extend) === null || _dataSource$_extend4 === void 0 || (_dataSource$_extend4 = _dataSource$_extend4.product) === null || _dataSource$_extend4 === void 0 ? void 0 : _dataSource$_extend4.product_quantity) !== null && _ref4 !== void 0 ? _ref4 : dataSource === null || dataSource === void 0 || (_dataSource$_extend5 = dataSource._extend) === null || _dataSource$_extend5 === void 0 || (_dataSource$_extend5 = _dataSource$_extend5.product) === null || _dataSource$_extend5 === void 0 || (_dataSource$_extend5 = _dataSource$_extend5._extend) === null || _dataSource$_extend5 === void 0 ? void 0 : _dataSource$_extend5.quantity) !== null && _ref3 !== void 0 ? _ref3 : holderIds.length) || 1;
  return Number(count) || 1;
};
var LineItemHolders = function LineItemHolders(props) {
  var _props$dataSource = props.dataSource,
    dataSource = _props$dataSource === void 0 ? {} : _props$dataSource,
    _props$holderOptions = props.holderOptions,
    holderOptions = _props$holderOptions === void 0 ? [] : _props$holderOptions,
    isFormSubject = props.isFormSubject;
  var holderIds = useMemo(function () {
    return normalizeHolderIds(pickHolderId(dataSource));
  }, [dataSource]);
  var holderMaxCount = pickHolderMaxCount(dataSource, holderIds);
  var showUnassigned = Boolean(isFormSubject || (dataSource === null || dataSource === void 0 ? void 0 : dataSource.isFormSubject));
  var holderType = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.holderType) || (dataSource === null || dataSource === void 0 ? void 0 : dataSource.holder_type) || 'minors';
  var holderIcon = holderIconMap[holderType] || holderIconMap.minors;
  if (!holderIds.length) {
    if (!showUnassigned) return null;
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__holders")
    }, /*#__PURE__*/React.createElement("div", {
      className: "holder-item unassigned"
    }, holderMaxCount > 1 ? /*#__PURE__*/React.createElement("span", null, holderMaxCount, "/", holderMaxCount, ' ') : null, /*#__PURE__*/React.createElement("span", {
      className: "holder-label"
    }, locales.getText('pisell2.cart.sku-card.unassigned'))));
  }
  var renderHolder = function renderHolder(id, index) {
    var _holder$label;
    var holder = holderOptions.find(function (item) {
      return String(item.id) === String(id);
    });
    var label = (_holder$label = holder === null || holder === void 0 ? void 0 : holder.label) !== null && _holder$label !== void 0 ? _holder$label : String(id);
    return /*#__PURE__*/React.createElement("div", {
      className: "holder-item",
      key: "".concat(id, "-").concat(index)
    }, /*#__PURE__*/React.createElement("span", {
      className: "holder-icon"
    }, /*#__PURE__*/React.createElement(Iconfont, {
      type: holderIcon
    })), /*#__PURE__*/React.createElement("span", {
      className: "holder-label"
    }, label));
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__holders")
  }, holderIds.map(function (id, index) {
    return renderHolder(id, index);
  }));
};
export default LineItemHolders;