import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
import _regeneratorRuntime from '@babel/runtime/regenerator';
import 'core-js/modules/es.symbol.js';
import 'core-js/modules/es.symbol.description.js';
import 'core-js/modules/es.array.concat.js';
import 'core-js/modules/es.object.to-string.js';
import 'core-js/modules/es.promise.js';
import React__default, { useCallback, useMemo, cloneElement } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { settings } from '../../constants/Settings.js';
import Button from '../Button/Button.js';
import { Close } from '@carbon/icons-react';

var iotPrefix = settings.iotPrefix;
var propTypes = {
  /** Index of the active tear sheet provided by TearSheetWrapper */
  idx: PropTypes.number,
  /** Tear sheet title */
  title: PropTypes.string,
  /** Tear sheet description */
  description: PropTypes.string,
  /** Extra content under description */
  headerExtraContent: PropTypes.node,
  /** Additional classNames to be provided for TearSheet component */
  className: PropTypes.string,
  /** onClose optional function. When provided, it will run when the TearSheet is closed */
  onClose: PropTypes.func,
  /** closeAllTearSheets function is provided via TearSheetWrapper component. It should't be overridden */
  closeAllTearSheets: PropTypes.func,
  /** openNextSheet function is provided by TearSheetWrapper component and it shouldn't be overriden. This function can be accessed via children prop to open the next sheet */
  openNextSheet: PropTypes.func,
  /** goToPreviousSheet function is provided by TearSheetWrapper component and it shouldn't be overriden. This function can be accessed via children prop to go back to the previous sheet or close the current one if its index is 0 */
  goToPreviousSheet: PropTypes.func,
  /** i18n messages */
  i18n: PropTypes.shape({
    close: PropTypes.string
  }),
  children: PropTypes.node,
  testId: PropTypes.string,
  shouldPreventClose: PropTypes.func,
  role: PropTypes.string
};
var defaultProps = {
  idx: undefined,
  title: 'Title',
  description: 'Description',
  closeAllTearSheets: undefined,
  openNextSheet: undefined,
  goToPreviousSheet: undefined,
  headerExtraContent: null,
  className: undefined,
  onClose: undefined,
  i18n: {
    close: 'Close'
  },
  children: undefined,
  // TODO: set default testId in v3.
  testId: '',
  shouldPreventClose: function shouldPreventClose() {
    return new Promise(function (res) {
      return res(false);
    });
  },
  role: undefined
};
var TearSheet = function TearSheet(_ref) {
  var idx = _ref.idx,
    title = _ref.title,
    description = _ref.description,
    headerExtraContent = _ref.headerExtraContent,
    className = _ref.className,
    onClose = _ref.onClose,
    closeAllTearSheets = _ref.closeAllTearSheets,
    openNextSheet = _ref.openNextSheet,
    goToPreviousSheet = _ref.goToPreviousSheet,
    i18n = _ref.i18n,
    children = _ref.children,
    testId = _ref.testId,
    shouldPreventClose = _ref.shouldPreventClose,
    role = _ref.role;
  var onCloseButton = useCallback(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var preventClose;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.next = 2;
          return shouldPreventClose();
        case 2:
          preventClose = _context.sent;
          if (!preventClose) {
            if (onClose) {
              onClose();
            }
            goToPreviousSheet();
          }
        case 4:
        case "end":
          return _context.stop();
      }
    }, _callee);
  })), [shouldPreventClose, onClose, goToPreviousSheet]);
  return /*#__PURE__*/React__default.createElement("div", {
    // TODO: use only testId in v3.
    "data-testid": testId || "".concat(iotPrefix, "--tear-sheet-").concat(idx),
    className: classnames("".concat(iotPrefix, "--tear-sheet"), className),
    role: role
  }, /*#__PURE__*/React__default.createElement("div", {
    className: classnames("".concat(iotPrefix, "--tear-sheet--header"), _defineProperty({}, "".concat(iotPrefix, "--tear-sheet--header__extraContent"), !!headerExtraContent))
  }, /*#__PURE__*/React__default.createElement(Button, {
    hasIconOnly: true,
    wrapperClasses: "".concat(iotPrefix, "--tear-sheet--header__close-btn"),
    kind: "ghost",
    renderIcon: Close,
    iconDescription: i18n.close,
    tooltipAlignment: "end",
    tooltipPosition: "bottom",
    onClick: onCloseButton
    // TODO: use only testId in v3.
    ,
    testId: testId ? "".concat(testId, "-close-button-").concat(idx) : "tearSheetCloseBtn".concat(idx)
  }), /*#__PURE__*/React__default.createElement("h2", null, title), /*#__PURE__*/React__default.createElement("span", {
    className: "".concat(iotPrefix, "--tear-sheet--header--description")
  }, description), headerExtraContent), /*#__PURE__*/React__default.createElement("div", {
    className: "".concat(iotPrefix, "--tear-sheet--content")
  }, useMemo(function () {
    return /*#__PURE__*/cloneElement(children, {
      idx: idx,
      openNextSheet: openNextSheet,
      goToPreviousSheet: goToPreviousSheet,
      closeAllTearSheets: closeAllTearSheets
    });
  }, [children, closeAllTearSheets, goToPreviousSheet, idx, openNextSheet])));
};
TearSheet.propTypes = propTypes;
TearSheet.defaultProps = defaultProps;
TearSheet.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "TearSheet",
  "props": {
    "idx": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Index of the active tear sheet provided by TearSheetWrapper",
      "type": {
        "name": "number"
      },
      "required": false
    },
    "title": {
      "defaultValue": {
        "value": "'Title'",
        "computed": false
      },
      "description": "Tear sheet title",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "description": {
      "defaultValue": {
        "value": "'Description'",
        "computed": false
      },
      "description": "Tear sheet description",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "closeAllTearSheets": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "closeAllTearSheets function is provided via TearSheetWrapper component. It should't be overridden",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "openNextSheet": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "openNextSheet function is provided by TearSheetWrapper component and it shouldn't be overriden. This function can be accessed via children prop to open the next sheet",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "goToPreviousSheet": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "goToPreviousSheet function is provided by TearSheetWrapper component and it shouldn't be overriden. This function can be accessed via children prop to go back to the previous sheet or close the current one if its index is 0",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "headerExtraContent": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Extra content under description",
      "type": {
        "name": "node"
      },
      "required": false
    },
    "className": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Additional classNames to be provided for TearSheet component",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "onClose": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "onClose optional function. When provided, it will run when the TearSheet is closed",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{\n  close: 'Close',\n}",
        "computed": false
      },
      "description": "i18n messages",
      "type": {
        "name": "shape",
        "value": {
          "close": {
            "name": "string",
            "required": false
          }
        }
      },
      "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
    },
    "shouldPreventClose": {
      "defaultValue": {
        "value": "() => new Promise((res) => res(false))",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "role": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    }
  }
};
var TearSheet$1 = TearSheet;

export { TearSheet$1 as default };
