import React from 'react';
import { Tag } from 'antd';
import "../../../cartSkuCard/components/resources/index.less";
var PREFIX = 'pisell-cart-sku-card';
var ResourceTag = Tag;
var colors = ['magenta', 'volcano', 'orange', 'gold', 'green', 'cyan', 'blue', 'geekblue', 'purple'];
function getColor(n) {
  return colors[n * 31 % colors.length];
}
var getResourceLabel = function getResourceLabel(resource, fallbackLabel) {
  var _resource$metadata, _resource$metadata2;
  return (resource === null || resource === void 0 || (_resource$metadata = resource.metadata) === null || _resource$metadata === void 0 ? void 0 : _resource$metadata.resource_name) || (resource === null || resource === void 0 || (_resource$metadata2 = resource.metadata) === null || _resource$metadata2 === void 0 ? void 0 : _resource$metadata2.form_name) || fallbackLabel || '';
};
var LineItemResources = function LineItemResources(props) {
  var _props$dataSource = props.dataSource,
    dataSource = _props$dataSource === void 0 ? {} : _props$dataSource;
  var resources = Array.isArray(dataSource === null || dataSource === void 0 ? void 0 : dataSource.resources) ? dataSource.resources : [];
  var fallbackResourceLabel = resources.length === 1 ? dataSource === null || dataSource === void 0 ? void 0 : dataSource.relation_form_name : '';
  var resourceLabels = Array.isArray(dataSource === null || dataSource === void 0 ? void 0 : dataSource.resources) ? resources.map(function (resource, index) {
    return {
      id: Number(resource === null || resource === void 0 ? void 0 : resource.form_id) || Number(resource === null || resource === void 0 ? void 0 : resource.relation_id) || index + 1,
      label: getResourceLabel(resource, fallbackResourceLabel)
    };
  }) : [];
  if (!resourceLabels.length && dataSource !== null && dataSource !== void 0 && dataSource.relation_form_name) {
    resourceLabels.push({
      id: 1,
      label: dataSource.relation_form_name
    });
  }
  if (!resourceLabels.length) return null;
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__resources")
  }, resourceLabels.map(function (resource) {
    return /*#__PURE__*/React.createElement(ResourceTag, {
      key: "".concat(resource.id, "-").concat(resource.label),
      color: getColor(resource.id),
      className: "resource-tag"
    }, resource.label);
  }));
};
export default LineItemResources;