'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _extends = require('@babel/runtime/helpers/extends');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var isEmpty = require('../../node_modules/lodash-es/isEmpty.js');
require('core-js/modules/es.array.concat.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.array.includes.js');
require('core-js/modules/es.array.join.js');
require('core-js/modules/es.symbol.js');
require('core-js/modules/es.array.map.js');
require('core-js/modules/es.object.get-own-property-descriptor.js');
require('core-js/modules/es.object.get-own-property-descriptors.js');
require('core-js/modules/es.object.keys.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/es.string.includes.js');
require('core-js/modules/es.string.link.js');
require('core-js/modules/web.dom-collections.for-each.js');
var React = require('react');
var react = require('@carbon/react');
var PropTypes = require('prop-types');
var classnames = require('classnames');
var LayoutConstants = require('../../constants/LayoutConstants.js');
var CardPropTypes = require('../../constants/CardPropTypes.js');
var Card = require('../Card/Card.js');
var cardUtilityFunctions = require('../../utils/cardUtilityFunctions.js');
var deprecate = require('../../internal/deprecate.js');
var dayjs = require('../../utils/dayjs.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var _extends__default = /*#__PURE__*/_interopDefault(_extends);
var _objectWithoutProperties__default = /*#__PURE__*/_interopDefault(_objectWithoutProperties);
var React__default = /*#__PURE__*/_interopDefault(React);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);
var classnames__default = /*#__PURE__*/_interopDefault(classnames);

var _excluded = ["id", "title", "size", "data", "isLoading", "isResizable", "loadData", "hasMoreData", "layout", "className", "children", "testID", "testId", "timeZone"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty__default.default(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var ListCard = function ListCard(_ref) {
  var id = _ref.id,
    title = _ref.title,
    size = _ref.size,
    data = _ref.data,
    isLoading = _ref.isLoading,
    isResizable = _ref.isResizable,
    loadData = _ref.loadData,
    hasMoreData = _ref.hasMoreData;
    _ref.layout;
    var className = _ref.className,
    children = _ref.children,
    testID = _ref.testID,
    testId = _ref.testId,
    timeZone = _ref.timeZone,
    others = _objectWithoutProperties__default.default(_ref, _excluded);
  var effectiveTimezone = timeZone || dayjs.default.tz.guess();
  dayjs.default.tz.setDefault(effectiveTimezone);
  var handleScroll = function handleScroll(e) {
    var element = e.target;
    //  height of the elements content - height element’s content is scrolled vertically === height of the scrollable part of the element
    if (element.scrollHeight - element.scrollTop === element.clientHeight && hasMoreData && !isLoading) {
      loadData();
    }
  };
  var resizeHandles = isResizable ? cardUtilityFunctions.getResizeHandles(children) : [];
  return /*#__PURE__*/React__default.default.createElement(Card.default, _extends__default.default({
    id: id,
    title: title,
    size: size,
    onScroll: handleScroll,
    isEmpty: isEmpty(data),
    resizeHandles: resizeHandles
    // TODO: remove deprecated 'testID' in v3.
    ,
    testId: testID || testId,
    timeZone: effectiveTimezone
  }, others), /*#__PURE__*/React__default.default.createElement("div", {
    className: classnames__default.default('list-card', className),
    style: {
      paddingTop: 0,
      paddingRight: LayoutConstants.CARD_CONTENT_PADDING,
      paddingBottom: 0,
      paddingLeft: LayoutConstants.CARD_CONTENT_PADDING
    }
  }, /*#__PURE__*/React__default.default.createElement(react.StructuredListWrapper, null, /*#__PURE__*/React__default.default.createElement(react.StructuredListBody
  // TODO: remove deprecated 'testID' in v3.
  , {
    "data-testid": "".concat(testID || testId, "-list-body")
  }, data ? data.map(function (item) {
    return /*#__PURE__*/React__default.default.createElement(react.StructuredListRow, {
      key: item.id
    }, /*#__PURE__*/React__default.default.createElement(react.StructuredListCell, {
      className: "list-card--item",
      key: "".concat(item.id, "-cell")
    }, /*#__PURE__*/React__default.default.createElement("div", {
      className: "list-card--item--value"
    }, item.link ? /*#__PURE__*/React__default.default.createElement(react.Link, {
      style: {
        display: 'inherit'
      },
      target: "_blank",
      href: item.link
    }, item.value) : item.value), item.extraContent ? /*#__PURE__*/React__default.default.createElement("div", {
      className: "list-card--item--extra-content"
    }, item.extraContent) : null));
  }) : null, isLoading ? /*#__PURE__*/React__default.default.createElement(react.InlineLoading
  // TODO: remove deprecated 'testID' in v3.
  , {
    "data-testid": "".concat(testID || testId, "-loading"),
    description: "Loading data...",
    status: "active"
  }) : null))));
};
ListCard.propTypes = _objectSpread(_objectSpread({}, CardPropTypes.CardPropTypes), {}, {
  data: PropTypes__default.default.arrayOf(PropTypes__default.default.shape({
    id: PropTypes__default.default.string.isRequired,
    value: PropTypes__default.default.string.isRequired,
    link: PropTypes__default.default.string,
    extraContent: PropTypes__default.default.element
  })),
  isLoading: PropTypes__default.default.bool,
  hasMoreData: PropTypes__default.default.bool,
  loadData: PropTypes__default.default.func.isRequired,
  layout: PropTypes__default.default.string,
  size: function size(props, propName, componentName) {
    var error;
    if (!Object.keys(LayoutConstants.CARD_SIZES).includes(props[propName])) {
      error = new Error("`".concat(componentName, "` prop `").concat(propName, "` must be one of ").concat(Object.keys(LayoutConstants.CARD_SIZES).join(','), "."));
    }
    // If the size
    if (props[propName] === LayoutConstants.CARD_SIZES.SMALL || props[propName] === LayoutConstants.CARD_SIZES.SMALLWIDE || props[propName] === LayoutConstants.CARD_SIZES.SMALLFULL) {
      error = new Error("Deprecation notice: `".concat(componentName, "` prop `").concat(propName, "` cannot be `").concat(props[propName], "` as the lists will not render correctly. Minimum size is `MEDIUM`"));
    }
    return error;
  },
  // TODO: remove deprecated 'testID' in v3
  // eslint-disable-next-line react/require-default-props
  testID: deprecate(PropTypes__default.default.string, "The 'testID' prop has been deprecated. Please use 'testId' instead."),
  testId: PropTypes__default.default.string
});
ListCard.defaultProps = {
  isLoading: false,
  hasMoreData: false,
  layout: '',
  data: [],
  size: LayoutConstants.CARD_SIZES.MEDIUM,
  // TODO: replace with 'list-card' in v3. to set better defaults instead of inheriting from Card
  testId: 'Card'
};
ListCard.displayName = 'ListCard';
ListCard.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "ListCard",
  "props": {
    "isLoading": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "hasMoreData": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "layout": {
      "defaultValue": {
        "value": "''",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "data": {
      "defaultValue": {
        "value": "[]",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "arrayOf",
        "value": {
          "name": "shape",
          "value": {
            "id": {
              "name": "string",
              "required": true
            },
            "value": {
              "name": "string",
              "required": true
            },
            "link": {
              "name": "string",
              "required": false
            },
            "extraContent": {
              "name": "element",
              "required": false
            }
          }
        }
      },
      "required": false
    },
    "size": {
      "defaultValue": {
        "value": "CARD_SIZES.MEDIUM",
        "computed": true
      },
      "description": "",
      "type": {
        "name": "custom",
        "raw": "(props, propName, componentName) => {\n  let error;\n  if (!Object.keys(CARD_SIZES).includes(props[propName])) {\n    error = new Error(\n      `\\`${componentName}\\` prop \\`${propName}\\` must be one of ${Object.keys(CARD_SIZES).join(\n        ','\n      )}.`\n    );\n  }\n  // If the size\n  if (\n    props[propName] === CARD_SIZES.SMALL ||\n    props[propName] === CARD_SIZES.SMALLWIDE ||\n    props[propName] === CARD_SIZES.SMALLFULL\n  ) {\n    error = new Error(\n      `Deprecation notice: \\`${componentName}\\` prop \\`${propName}\\` cannot be \\`${props[propName]}\\` as the lists will not render correctly. Minimum size is \\`MEDIUM\\``\n    );\n  }\n  return error;\n}"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'Card'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "loadData": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "testID": {
      "description": "",
      "type": {
        "name": "custom",
        "raw": "deprecate(\n  PropTypes.string,\n  `The 'testID' prop has been deprecated. Please use 'testId' instead.`\n)"
      },
      "required": false
    }
  },
  "composes": ["../../constants/CardPropTypes"]
};
var ListCard$1 = ListCard;

module.exports = ListCard$1;
