"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _Tag = _interopRequireDefault(require("../Tag"));

var _js = require("../../globals/js");

var _excluded = ["label", "labelStatus", "interactive", "image", "heading", "subHeading", "caption", "tag", "children", "className"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

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; }

var prefix = _js.settings.prefix;

var CardExternal = function CardExternal(_ref) {
  var _classNames;

  var label = _ref.label,
      labelStatus = _ref.labelStatus,
      interactive = _ref.interactive,
      image = _ref.image,
      heading = _ref.heading,
      subHeading = _ref.subHeading,
      caption = _ref.caption,
      tag = _ref.tag,
      children = _ref.children,
      className = _ref.className,
      other = _objectWithoutProperties(_ref, _excluded);

  var myRef = (0, _react.useRef)();
  var supportiveTextFontSize = 14;
  var supportiveTextLineHeight = 1.5; // Set a maximum of 3 lines of supportive text by default

  var _useState = (0, _react.useState)(supportiveTextFontSize * supportiveTextLineHeight * 3),
      _useState2 = _slicedToArray(_useState, 2),
      supportiveTextHeight = _useState2[0],
      setsupportiveTextHeight = _useState2[1]; // statusStyle apply different style(color, backgroundColor) based on its active state 


  var statusStyle = labelStatus ? "success" : "neutral";
  var wrapperClasses = (0, _classnames.default)("".concat(prefix, "--card-ext"), (_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "--card-ext--interactive"), interactive), _defineProperty(_classNames, "".concat(className), className), _classNames));
  /**
   * Get the height of the supportive text element.
   * This is useful to calculate a dynamic maximum amount of line text with final
   * ellipsis for the "supportive text" element, depending of the available
   * height.
   *
   * Cards' title has no limits in terms of line text, whereas we can decide to
   * truncate the supportive text at 3 number of lines. The multiline truncation
   * can be obtained by using the "line clamp" CSS property that truncates text
   * at a specific number of lines.
   * The problem is, if we place three cards on a Flexbox/Grid parent, these
   * cards will have the same height, depending on the highest of the three.
   * This means that if one card has a multiline title, the other two will grow
   * in height, showing a significant white space. To fill the latter cards'
   * white space we need to increase the number of their "supportive text" lines.
   * To do this we need to divide the "supportive text" element's height by its
   * font-size and line-height in order to get exact amount of line text that are
   * needed to fill the extra white space.
   *
   */

  (0, _react.useEffect)(function () {
    setsupportiveTextHeight(myRef.current.offsetHeight);
  }, [myRef]);
  return (
    /*#__PURE__*/

    /* Also expose a custom classname prop */

    /**
     * A prop will establish if the card is either informative (default) or
     * interactive.
     * If interactive, a modifier will be added and an animation will be shown
     * on hover event ("${prefix}--card-ext--interactive").
     */
    _react.default.createElement("div", {
      className: wrapperClasses
    }, label && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(prefix, "--card-ext__label ").concat(prefix, "--card-ext__label--").concat(statusStyle)
    }, label), /*#__PURE__*/_react.default.createElement("figure", {
      className: "".concat(prefix, "--card-ext__media")
    }, /*#__PURE__*/_react.default.createElement("img", {
      className: "".concat(prefix, "--card-ext__image"),
      src: image,
      alt: "Card picture"
    })), /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(prefix, "--card-ext__info-wrapper ").concat(prefix, "--card-ext__info-wrapper--with-divider")
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(prefix, "--card-ext__primary-title")
    }, subHeading && /*#__PURE__*/_react.default.createElement("p", {
      className: "".concat(prefix, "--card-ext__subheading")
    }, subHeading), heading && /*#__PURE__*/_react.default.createElement("p", {
      className: "".concat(prefix, "--card-ext__heading")
    }, heading)), /*#__PURE__*/_react.default.createElement("div", {
      className: other.truncated ? "".concat(prefix, "--card-ext__supportive-text ").concat(prefix, "--card-ext__supportive-text--truncated") : "".concat(prefix, "--card-ext__supportive-text"),
      ref: myRef
    }, /*#__PURE__*/_react.default.createElement("p", {
      style: {
        WebkitLineClamp: Math.floor(supportiveTextHeight / supportiveTextLineHeight / supportiveTextFontSize)
      }
    }, caption)), tag && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(prefix, "--card-ext__tags")
    }, /*#__PURE__*/_react.default.createElement(_Tag.default, {
      className: "".concat(prefix, "--card-ext__tag"),
      type: "custom"
    }, tag))), children)
  );
};

CardExternal.propTypes = {
  /**
   label description for card if any
  */
  label: _propTypes.default.string,

  /**
   labelStatus is either true or false, it applies  for card if any
  */
  labelStatus: _propTypes.default.bool,
  image: _propTypes.default.string,
  heading: _propTypes.default.string,
  subHeading: _propTypes.default.string,
  caption: _propTypes.default.string,
  tag: _propTypes.default.string,
  children: _propTypes.default.node,
  interactive: _propTypes.default.bool,
  className: _propTypes.default.string
};
var _default = CardExternal;
exports.default = _default;