"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _propTypes = require("prop-types");

var _propTypes2 = _interopRequireDefault(_propTypes);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var BOX_STATES = ["OPEN", "CLOSED"];
var BOX_BEHAVIOURS = ["TOGGLE", "UNFOLD"];
var CONTENT_VARIANTS = ["PRIMARY", "SECONDARY"];

var ContentBoxContext = _react2["default"].createContext();

function ContentBox(_ref) {
  var propState = _ref.state,
      children = _ref.children,
      as = _ref.as,
      _ref$behaviour = _ref.behaviour,
      behaviour = _ref$behaviour === void 0 ? "TOGGLE" : _ref$behaviour,
      _ref$tone = _ref.tone,
      tone = _ref$tone === void 0 ? "NEUTRAL" : _ref$tone,
      rest = _objectWithoutProperties(_ref, ["state", "children", "as", "behaviour", "tone"]);

  var Component = as || "div";

  var _useState = (0, _react.useState)(_objectSpread({
    boxState: "CLOSED"
  }, propState)),
      _useState2 = _slicedToArray(_useState, 2),
      state = _useState2[0],
      setState = _useState2[1];

  var changeContent = (0, _react.useCallback)(function () {
    return setState(function (oldState) {
      return _objectSpread({}, oldState, {
        boxState: oldState.boxState === "OPEN" ? "CLOSED" : "OPEN"
      });
    });
  }, []);
  var value = (0, _react.useMemo)(function () {
    return {
      state: state,
      boxBehaviour: behaviour,
      changeContent: changeContent,
      boxTone: tone
    };
  }, [state]);
  return _react2["default"].createElement(ContentBoxContext.Provider, {
    value: value
  }, _react2["default"].createElement(Component, rest, children));
}

ContentBox.propTypes = {
  children: _propTypes2["default"].any.isRequired,
  behaviour: _propTypes2["default"].oneOf(BOX_BEHAVIOURS),
  state: _propTypes2["default"].object // PropTypes.oneOf(BOX_STATES),

};

ContentBox.Content = function (_ref2) {
  var children = _ref2.children,
      as = _ref2.as,
      _ref2$variant = _ref2.variant,
      variant = _ref2$variant === void 0 ? "PRIMARY" : _ref2$variant,
      rest = _objectWithoutProperties(_ref2, ["children", "as", "variant"]);

  var Component = as || "div";

  var _ContentBox$useConten = ContentBox.useContentBoxContext(),
      state = _ContentBox$useConten.state,
      boxBehaviour = _ContentBox$useConten.boxBehaviour;

  var componentToReturn = _react2["default"].createElement(Component, rest, children);

  if (state.boxState === "OPEN" && boxBehaviour === "TOGGLE") {
    if (variant === "SECONDARY") {
      return componentToReturn;
    }
  } else if (state.boxState === "OPEN" && boxBehaviour === "UNFOLD") {
    if (variant === "PRIMARY" || variant === "SECONDARY") {
      return componentToReturn;
    }
  } else if (state.boxState === "CLOSED") {
    if (variant === "PRIMARY") {
      return componentToReturn;
    }
  }

  return null;
};

ContentBox.Content.propTypes = {
  children: _propTypes2["default"].any.isRequired,
  variant: _propTypes2["default"].oneOf(CONTENT_VARIANTS)
};

ContentBox.Button = function (_ref3) {
  var children = _ref3.children,
      hiddenIf = _ref3.hiddenIf,
      _ref3$as = _ref3.as,
      as = _ref3$as === void 0 ? "button" : _ref3$as,
      props = _objectWithoutProperties(_ref3, ["children", "hiddenIf", "as"]);

  var Component = as || "button";

  var _ContentBox$useConten2 = ContentBox.useContentBoxContext(),
      state = _ContentBox$useConten2.state,
      changeContent = _ContentBox$useConten2.changeContent,
      boxTone = _ContentBox$useConten2.boxTone;

  if (hiddenIf === state.boxState) {
    return null;
  }

  return _react2["default"].createElement(Component, Object.assign({
    onClick: changeContent,
    tone: boxTone
  }, props), children);
};

ContentBox.Button.propTypes = {
  children: _propTypes2["default"].any.isRequired,
  hiddenIf: _propTypes2["default"].oneOf(BOX_STATES)
};

ContentBox.useContentBoxContext = function () {
  var context = _react2["default"].useContext(ContentBoxContext);

  if (!context) {
    throw new Error("ContentBox compound components cannot be rendered outside the ContentBox component");
  }

  return context;
};

exports["default"] = ContentBox;