import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import 'core-js/modules/es.array.concat.js';
import 'core-js/modules/es.symbol.js';
import 'core-js/modules/es.array.filter.js';
import 'core-js/modules/es.array.map.js';
import 'core-js/modules/es.array.slice.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.promise.js';
import 'core-js/modules/web.dom-collections.for-each.js';
import React__default, { useState, useMemo, Children, createRef, useEffect, cloneElement } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { settings } from '../../constants/Settings.js';

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 iotPrefix = settings.iotPrefix;
var propTypes = {
  /** If isOpen is true, TearSheetWrapper opens with the first TearSheet active */
  isOpen: PropTypes.bool,
  /** Additional classNames to be provided for TearSheetWrapper */
  className: PropTypes.string,
  /** onCloseAllTearSheets optional function. When provided, it will run when the closeAllTearSheets function is called from a child component */
  onCloseAllTearSheets: PropTypes.func,
  children: PropTypes.node,
  testId: PropTypes.string
};
var defaultProps = {
  isOpen: false,
  className: undefined,
  onCloseAllTearSheets: undefined,
  children: undefined,
  // TODO: set default in v3
  testId: ''
};
var baseClassName = "".concat(iotPrefix, "--tear-sheet-wrapper");
var TearSheetWrapper = function TearSheetWrapper(_ref) {
  var _animationClasses$ove;
  var isOpen = _ref.isOpen,
    className = _ref.className,
    onCloseAllTearSheets = _ref.onCloseAllTearSheets,
    children = _ref.children,
    testId = _ref.testId;
  var _useState = useState(null),
    _useState2 = _slicedToArray(_useState, 2),
    activeTearSheetIdx = _useState2[0],
    setActiveTearSheetIdx = _useState2[1];
  var childrenArray = useMemo(function () {
    return Children.toArray(children).slice(0, 2);
  }, [children]); // Limit of 2 TearSheets
  var _useState3 = useState(childrenArray.map(function () {
      return /*#__PURE__*/createRef();
    })),
    _useState4 = _slicedToArray(_useState3, 1),
    containers = _useState4[0];
  var _useState5 = useState(function () {
      return childrenArray.reduce(function (acc, c, idx) {
        return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, "container".concat(idx), []));
      }, {
        overlay: []
      });
    }),
    _useState6 = _slicedToArray(_useState5, 2),
    animationClasses = _useState6[0],
    setAnimationClasses = _useState6[1];
  useEffect(function () {
    return isOpen ? setActiveTearSheetIdx(0) : setActiveTearSheetIdx(null);
  }, [isOpen]);
  useEffect(function () {
    setAnimationClasses(function (prevState) {
      if (activeTearSheetIdx === null) {
        return _objectSpread(_objectSpread({}, prevState), {}, {
          overlay: []
        });
      }
      return _objectSpread({}, Object.keys(prevState).reduce(function (acc, i) {
        return i === 'overlay' ? _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, i, ["".concat(baseClassName, "__is-visible")])) : i === "container".concat(activeTearSheetIdx) ? _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, i, [])) : _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, i, ["".concat(baseClassName, "--container__is-hidden")]));
      }, {}));
    });
  }, [activeTearSheetIdx]);
  var goToSheet = function goToSheet(idx) {
    return setActiveTearSheetIdx(idx);
  };
  var closeAllTearSheets = function closeAllTearSheets() {
    if (onCloseAllTearSheets) {
      onCloseAllTearSheets();
      setActiveTearSheetIdx(null);
    } else {
      setActiveTearSheetIdx(null);
    }
  };
  return /*#__PURE__*/React__default.createElement("div", {
    className: classnames(baseClassName, className, _objectSpread({}, animationClasses === null || animationClasses === void 0 || (_animationClasses$ove = animationClasses.overlay) === null || _animationClasses$ove === void 0 ? void 0 : _animationClasses$ove.reduce(function (acc, curr) {
      return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, curr, isOpen));
    }, {})))
    // TODO: use only testId in v3.
    ,
    "data-testid": testId || "".concat(iotPrefix, "--tear-sheet-wrapper")
  }, childrenArray.map(function (tearSheet, idx, arr) {
    var _animationClasses;
    var newTearSheet = /*#__PURE__*/cloneElement(tearSheet, {
      openNextSheet: function openNextSheet() {
        return idx < arr.length - 1 && goToSheet(idx + 1);
      },
      goToPreviousSheet: function goToPreviousSheet() {
        return goToSheet(idx > 0 ? idx - 1 : null);
      },
      closeAllTearSheets: closeAllTearSheets,
      idx: idx
    });
    return (
      /*#__PURE__*/
      // eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
      React__default.createElement("div", {
        id: "container-".concat(idx),
        key: "container-".concat(idx)
        // TODO: use only testId in v3.
        ,
        "data-testid": testId ? "".concat(testId, "-container-").concat(idx) : "container-".concat(idx),
        className: classnames("".concat(baseClassName, "--container"), _objectSpread({}, animationClasses === null || animationClasses === void 0 || (_animationClasses = animationClasses["container".concat(idx)]) === null || _animationClasses === void 0 ? void 0 : _animationClasses.reduce(function (acc, curr) {
          return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, curr, true));
        }, {}))),
        ref: containers[idx],
        onClick: function onClick(e) {
          return e.stopPropagation();
        }
      }, newTearSheet)
    );
  }));
};
TearSheetWrapper.propTypes = propTypes;
TearSheetWrapper.defaultProps = defaultProps;
TearSheetWrapper.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "TearSheetWrapper",
  "props": {
    "isOpen": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "If isOpen is true, TearSheetWrapper opens with the first TearSheet active",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "className": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Additional classNames to be provided for TearSheetWrapper",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "onCloseAllTearSheets": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "onCloseAllTearSheets optional function. When provided, it will run when the closeAllTearSheets function is called from a child component",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "children": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "",
      "type": {
        "name": "node"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "''",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    }
  }
};
var TearSheetWrapper$1 = TearSheetWrapper;

export { TearSheetWrapper$1 as default };
