import React from 'react';
import "../../../cartSkuCard/components/resources/index.less";
var PREFIX = 'pisell-cart-sku-card';
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
    });
  }
  var visibleResourceLabels = resourceLabels.filter(function (resource) {
    return Boolean(resource.label);
  });
  if (!visibleResourceLabels.length) return null;
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__resources ").concat(PREFIX, "__resources--title")
  }, visibleResourceLabels.map(function (resource, index) {
    return /*#__PURE__*/React.createElement(React.Fragment, {
      key: "".concat(resource.id, "-").concat(resource.label)
    }, index > 0 ? /*#__PURE__*/React.createElement("span", {
      className: "resource-separator"
    }, "\xB7") : null, /*#__PURE__*/React.createElement("span", {
      className: "resource-title"
    }, resource.label));
  }));
};
export default LineItemResources;