'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
require('core-js/modules/es.symbol.js');
require('core-js/modules/es.symbol.description.js');
require('core-js/modules/es.array.concat.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/es.promise.js');
var React = require('react');
var PropTypes = require('prop-types');
var classnames = require('classnames');
var Settings = require('../../constants/Settings.js');
var Button = require('../Button/Button.js');
var iconsReact = require('@carbon/icons-react');

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

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var _asyncToGenerator__default = /*#__PURE__*/_interopDefault(_asyncToGenerator);
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefault(_regeneratorRuntime);
var React__default = /*#__PURE__*/_interopDefault(React);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);
var classnames__default = /*#__PURE__*/_interopDefault(classnames);

var iotPrefix = Settings.settings.iotPrefix;
var propTypes = {
  /** Index of the active tear sheet provided by TearSheetWrapper */
  idx: PropTypes__default.default.number,
  /** Tear sheet title */
  title: PropTypes__default.default.string,
  /** Tear sheet description */
  description: PropTypes__default.default.string,
  /** Extra content under description */
  headerExtraContent: PropTypes__default.default.node,
  /** Additional classNames to be provided for TearSheet component */
  className: PropTypes__default.default.string,
  /** onClose optional function. When provided, it will run when the TearSheet is closed */
  onClose: PropTypes__default.default.func,
  /** closeAllTearSheets function is provided via TearSheetWrapper component. It should't be overridden */
  closeAllTearSheets: PropTypes__default.default.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__default.default.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__default.default.func,
  /** i18n messages */
  i18n: PropTypes__default.default.shape({
    close: PropTypes__default.default.string
  }),
  children: PropTypes__default.default.node,
  testId: PropTypes__default.default.string,
  shouldPreventClose: PropTypes__default.default.func,
  role: PropTypes__default.default.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 = React.useCallback(/*#__PURE__*/_asyncToGenerator__default.default(/*#__PURE__*/_regeneratorRuntime__default.default.mark(function _callee() {
    var preventClose;
    return _regeneratorRuntime__default.default.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.default.createElement("div", {
    // TODO: use only testId in v3.
    "data-testid": testId || "".concat(iotPrefix, "--tear-sheet-").concat(idx),
    className: classnames__default.default("".concat(iotPrefix, "--tear-sheet"), className),
    role: role
  }, /*#__PURE__*/React__default.default.createElement("div", {
    className: classnames__default.default("".concat(iotPrefix, "--tear-sheet--header"), _defineProperty__default.default({}, "".concat(iotPrefix, "--tear-sheet--header__extraContent"), !!headerExtraContent))
  }, /*#__PURE__*/React__default.default.createElement(Button, {
    hasIconOnly: true,
    wrapperClasses: "".concat(iotPrefix, "--tear-sheet--header__close-btn"),
    kind: "ghost",
    renderIcon: iconsReact.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.default.createElement("h2", null, title), /*#__PURE__*/React__default.default.createElement("span", {
    className: "".concat(iotPrefix, "--tear-sheet--header--description")
  }, description), headerExtraContent), /*#__PURE__*/React__default.default.createElement("div", {
    className: "".concat(iotPrefix, "--tear-sheet--content")
  }, React.useMemo(function () {
    return /*#__PURE__*/React.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;

module.exports = TearSheet$1;
