import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _extends from '@babel/runtime/helpers/extends';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import isEmpty from '../../node_modules/lodash-es/isEmpty.js';
import 'core-js/modules/es.array.concat.js';
import 'core-js/modules/es.array.filter.js';
import 'core-js/modules/es.array.includes.js';
import 'core-js/modules/es.array.join.js';
import 'core-js/modules/es.symbol.js';
import 'core-js/modules/es.array.map.js';
import 'core-js/modules/es.object.get-own-property-descriptor.js';
import 'core-js/modules/es.object.get-own-property-descriptors.js';
import 'core-js/modules/es.object.keys.js';
import 'core-js/modules/es.object.to-string.js';
import 'core-js/modules/es.string.includes.js';
import 'core-js/modules/es.string.link.js';
import 'core-js/modules/web.dom-collections.for-each.js';
import React__default from 'react';
import { StructuredListWrapper, StructuredListBody, StructuredListRow, StructuredListCell, Link, InlineLoading } from '@carbon/react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { CARD_CONTENT_PADDING, CARD_SIZES } from '../../constants/LayoutConstants.js';
import { CardPropTypes } from '../../constants/CardPropTypes.js';
import Card from '../Card/Card.js';
import { getResizeHandles } from '../../utils/cardUtilityFunctions.js';
import deprecate from '../../internal/deprecate.js';
import dayjs from '../../utils/dayjs.js';

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(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(_ref, _excluded);
  var effectiveTimezone = timeZone || dayjs.tz.guess();
  dayjs.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 ? getResizeHandles(children) : [];
  return /*#__PURE__*/React__default.createElement(Card, _extends({
    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.createElement("div", {
    className: classnames('list-card', className),
    style: {
      paddingTop: 0,
      paddingRight: CARD_CONTENT_PADDING,
      paddingBottom: 0,
      paddingLeft: CARD_CONTENT_PADDING
    }
  }, /*#__PURE__*/React__default.createElement(StructuredListWrapper, null, /*#__PURE__*/React__default.createElement(StructuredListBody
  // TODO: remove deprecated 'testID' in v3.
  , {
    "data-testid": "".concat(testID || testId, "-list-body")
  }, data ? data.map(function (item) {
    return /*#__PURE__*/React__default.createElement(StructuredListRow, {
      key: item.id
    }, /*#__PURE__*/React__default.createElement(StructuredListCell, {
      className: "list-card--item",
      key: "".concat(item.id, "-cell")
    }, /*#__PURE__*/React__default.createElement("div", {
      className: "list-card--item--value"
    }, item.link ? /*#__PURE__*/React__default.createElement(Link, {
      style: {
        display: 'inherit'
      },
      target: "_blank",
      href: item.link
    }, item.value) : item.value), item.extraContent ? /*#__PURE__*/React__default.createElement("div", {
      className: "list-card--item--extra-content"
    }, item.extraContent) : null));
  }) : null, isLoading ? /*#__PURE__*/React__default.createElement(InlineLoading
  // TODO: remove deprecated 'testID' in v3.
  , {
    "data-testid": "".concat(testID || testId, "-loading"),
    description: "Loading data...",
    status: "active"
  }) : null))));
};
ListCard.propTypes = _objectSpread(_objectSpread({}, CardPropTypes), {}, {
  data: PropTypes.arrayOf(PropTypes.shape({
    id: PropTypes.string.isRequired,
    value: PropTypes.string.isRequired,
    link: PropTypes.string,
    extraContent: PropTypes.element
  })),
  isLoading: PropTypes.bool,
  hasMoreData: PropTypes.bool,
  loadData: PropTypes.func.isRequired,
  layout: PropTypes.string,
  size: function size(props, propName, componentName) {
    var error;
    if (!Object.keys(CARD_SIZES).includes(props[propName])) {
      error = new Error("`".concat(componentName, "` prop `").concat(propName, "` must be one of ").concat(Object.keys(CARD_SIZES).join(','), "."));
    }
    // If the size
    if (props[propName] === CARD_SIZES.SMALL || props[propName] === CARD_SIZES.SMALLWIDE || props[propName] === 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.string, "The 'testID' prop has been deprecated. Please use 'testId' instead."),
  testId: PropTypes.string
});
ListCard.defaultProps = {
  isLoading: false,
  hasMoreData: false,
  layout: '',
  data: [],
  size: 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;

export { ListCard$1 as default };
