"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _base = _interopRequireDefault(require("@emotion/styled/base"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react2 = require("@emotion/react");
var _reactImmutableProptypes = _interopRequireDefault(require("react-immutable-proptypes"));
var _Icon = _interopRequireDefault(require("./Icon"));
var _styles = require("./styles");
var _Dropdown = _interopRequireWildcard(require("./Dropdown"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _EMOTION_STRINGIFIED_CSS_ERROR__() { return "You have tried to stringify object returned from `css` function. It isn't supposed to be used directly (e.g. as value of the `className` prop), but rather handed to emotion so it can handle it (e.g. as value of `css` prop)."; }
const TopBarContainer = /*#__PURE__*/(0, _base.default)("div", {
  target: "ekazr9y3",
  label: "TopBarContainer"
})("align-items:center;background-color:", _styles.colors.textFieldBorder, ";display:flex;justify-content:space-between;margin:0 -14px;padding:13px;" + (process.env.NODE_ENV === "production" ? "" : "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 4328 characters] */"));
var _ref = process.env.NODE_ENV === "production" ? {
  name: "5r0eyy-ExpandButtonContainer",
  styles: "display:flex;align-items:center;font-size:14px;font-weight:500;line-height:1;label:ExpandButtonContainer;"
} : {
  name: "5r0eyy-ExpandButtonContainer",
  styles: "display:flex;align-items:center;font-size:14px;font-weight:500;line-height:1;label:ExpandButtonContainer;",
  map: "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 4328 characters] */",
  toString: _EMOTION_STRINGIFIED_CSS_ERROR__
};
const ExpandButtonContainer = /*#__PURE__*/(0, _base.default)("div", {
  target: "ekazr9y2",
  label: "ExpandButtonContainer"
})(props => props.hasHeading && _ref, ";" + (process.env.NODE_ENV === "production" ? "" : "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 4332 characters] */"));
const ExpandButton = /*#__PURE__*/(0, _base.default)("button", {
  target: "ekazr9y1",
  label: "ExpandButton"
})(_styles.buttons.button, ";padding:4px;background-color:transparent;color:inherit;&:last-of-type{margin-right:4px;}" + (process.env.NODE_ENV === "production" ? "" : "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 4332 characters] */"));
const AddButton = /*#__PURE__*/(0, _base.default)("button", {
  target: "ekazr9y0",
  label: "AddButton"
})(_styles.buttons.button, " ", _styles.buttons.widget, " ", _Icon.default, "{margin-left:6px;}" + (process.env.NODE_ENV === "production" ? "" : "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 4332 characters] */"));
class ObjectWidgetTopBar extends _react.default.Component {
  renderAddUI() {
    if (!this.props.allowAdd) {
      return null;
    }
    if (this.props.types && this.props.types.size > 0) {
      return this.renderTypesDropdown(this.props.types);
    } else {
      return this.renderAddButton();
    }
  }
  renderTypesDropdown(types) {
    return (0, _react2.jsx)(_Dropdown.default, {
      renderButton: () => (0, _react2.jsx)(_Dropdown.StyledDropdownButton, null, this.props.t('editor.editorWidgets.list.addType', {
        item: this.props.label
      }))
    }, types.map((type, idx) => (0, _react2.jsx)(_Dropdown.DropdownItem, {
      key: idx,
      label: type.get('label', type.get('name')),
      onClick: () => this.props.onAddType(type.get('name'))
    })));
  }
  renderAddButton() {
    return (0, _react2.jsx)(AddButton, {
      onClick: this.props.onAdd
    }, this.props.t('editor.editorWidgets.list.add', {
      item: this.props.label
    }), (0, _react2.jsx)(_Icon.default, {
      type: "add",
      size: "xsmall"
    }));
  }
  render() {
    const {
      onCollapseToggle,
      collapsed,
      heading = null
    } = this.props;
    return (0, _react2.jsx)(TopBarContainer, null, (0, _react2.jsx)(ExpandButtonContainer, {
      hasHeading: !!heading
    }, (0, _react2.jsx)(ExpandButton, {
      onClick: onCollapseToggle,
      "data-testid": "expand-button"
    }, (0, _react2.jsx)(_Icon.default, {
      type: "chevron",
      direction: collapsed ? 'right' : 'down',
      size: "small"
    })), heading), this.renderAddUI());
  }
}
_defineProperty(ObjectWidgetTopBar, "propTypes", {
  allowAdd: _propTypes.default.bool,
  types: _reactImmutableProptypes.default.list,
  onAdd: _propTypes.default.func,
  onAddType: _propTypes.default.func,
  onCollapseToggle: _propTypes.default.func,
  collapsed: _propTypes.default.bool,
  heading: _propTypes.default.node,
  label: _propTypes.default.string,
  t: _propTypes.default.func.isRequired
});
var _default = exports.default = ObjectWidgetTopBar;