"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.CLASSNAME_SECTION_CONTENT = exports.CLASSNAME_ACCORDION_TOGGLE = exports.CLASSNAME_SECTION_TITLE = void 0;

var _react = _interopRequireDefault(require("react"));

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

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _styles = require("@vincent_za/styles");

var _utils = require("@vincent_za/utils");

var _assets = require("@vincent_za/assets");

var _component = _interopRequireDefault(require("../../Scroll/component"));

var _Loader = require("../../Loader");

var _Error = require("../../Error");

var _component2 = _interopRequireDefault(require("../../Svg/component"));

var _component3 = _interopRequireDefault(require("../Search/component"));

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

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 _templateObject9() {
  var data = _taggedTemplateLiteral(["\n    padding-bottom: ", ";\n  "]);

  _templateObject9 = function _templateObject9() {
    return data;
  };

  return data;
}

function _templateObject8() {
  var data = _taggedTemplateLiteral(["\n  padding-bottom: ", ";\n\n  ", ";\n"]);

  _templateObject8 = function _templateObject8() {
    return data;
  };

  return data;
}

function _templateObject7() {
  var data = _taggedTemplateLiteral(["\n  display: ", ";\n  margin-left: ", ";\n  width: 1.4rem;\n  height: 1.4rem;\n\n  svg {\n    fill: ", ";\n\n    &:hover {\n      fill: ", ";\n    }\n  }\n"]);

  _templateObject7 = function _templateObject7() {
    return data;
  };

  return data;
}

function _templateObject6() {
  var data = _taggedTemplateLiteral(["\n  ", ";\n"]);

  _templateObject6 = function _templateObject6() {
    return data;
  };

  return data;
}

function _templateObject5() {
  var data = _taggedTemplateLiteral(["\n  position: relative;\n  margin-left: ", ";\n"]);

  _templateObject5 = function _templateObject5() {
    return data;
  };

  return data;
}

function _templateObject4() {
  var data = _taggedTemplateLiteral(["\n  display: flex;\n  position: relative;\n  align-items: center;\n"]);

  _templateObject4 = function _templateObject4() {
    return data;
  };

  return data;
}

function _templateObject3() {
  var data = _taggedTemplateLiteral(["\n  flex-grow: 1;\n"]);

  _templateObject3 = function _templateObject3() {
    return data;
  };

  return data;
}

function _templateObject2() {
  var data = _taggedTemplateLiteral(["\n  display: flex;\n  align-items: center;\n  margin-bottom: ", ";\n  padding: 0 ", ";\n  height: 1.2rem;\n  text-transform: uppercase;\n  font-family: ", ";\n  color: ", ";\n"]);

  _templateObject2 = function _templateObject2() {
    return data;
  };

  return data;
}

function _templateObject() {
  var data = _taggedTemplateLiteral(["\n  position: relative;\n  display: ", ";\n  flex-direction: column;\n  margin-right: -2px;\n  padding: ", " 0 0;\n  min-width: 100%;\n  background-color: #eee;\n  box-shadow: ", ";\n\n  ", ";\n"]);

  _templateObject = function _templateObject() {
    return data;
  };

  return data;
}

function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

var CLASSNAME_SECTION_TITLE = 'section-title';
exports.CLASSNAME_SECTION_TITLE = CLASSNAME_SECTION_TITLE;
var CLASSNAME_ACCORDION_TOGGLE = 'accordion-toggle';
exports.CLASSNAME_ACCORDION_TOGGLE = CLASSNAME_ACCORDION_TOGGLE;
var CLASSNAME_SECTION_CONTENT = 'section-content';
exports.CLASSNAME_SECTION_CONTENT = CLASSNAME_SECTION_CONTENT;

var Wrap = _styledComponents.default.div(_templateObject(), function (props) {
  return props.isHidden ? 'none' : 'flex';
}, _styles.REM_UNIT_XLG, function (props) {
  return "-4px -2px 0px -2px ".concat(props.accentColor, ", -4px 2px 0px -2px ").concat(props.accentColor);
}, function (props) {
  return props.isDisabled && "\n    pointer-events: none;\n    opacity: 0.3;\n  ";
});

var WrapScroll = (0, _component.default)(Wrap);

var TitleWrap = _styledComponents.default.div(_templateObject2(), function (props) {
  return props.isAccordion && !props.isOpen || props.isInitial || props.isNoContent ? 0 : _styles.REM_UNIT_MD;
}, _styles.REM_UNIT_LG, _styles.FONT_BOLD, function (props) {
  return props.accentColor;
});

var Title = _styledComponents.default.span(_templateObject3());

var ActionsWrap = _styledComponents.default.div(_templateObject4());

var ErrorFloatStyled = (0, _styledComponents.default)(_Error.ErrorFloat)(_templateObject5(), _styles.REM_UNIT_MD);

var AccordionToggle = _styledComponents.default.div(_templateObject6(), function (props) {
  return props.isDisabled && "\n    pointer-events: none;\n    opacity: 0.6;\n  ";
});

var AccordionSvg = (0, _styledComponents.default)(_component2.default)(_templateObject7(), function (props) {
  return props.isVisible ? 'block' : 'none';
}, _styles.REM_UNIT_LG, function (props) {
  return props.isOpen ? _styles.GRAY_X11 : _styles.SONIC_SILVER;
}, _styles.GRAY_X11);

var Content = _styledComponents.default.div(_templateObject8(), function (props) {
  return props.isResponsiveAndOpen ? _styles.REM_UNIT_MD : _styles.REM_UNIT_XLG;
}, _styles.media.sm(_templateObject9(), _styles.REM_UNIT_XLG));

var ContentScroll = (0, _component.default)(Content);

var PageSection =
/*#__PURE__*/
function (_React$Component) {
  _inherits(PageSection, _React$Component);

  function PageSection(props) {
    var _this;

    _classCallCheck(this, PageSection);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(PageSection).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "openAccordion", function () {
      _this.setState({
        isOpen: true
      });
    });

    _defineProperty(_assertThisInitialized(_this), "closeAccordion", function () {
      _this.setState({
        isOpen: false
      });
    });

    _this.state = {
      isOpen: _this.props.isOpenByDefault
    };
    return _this;
  }

  _createClass(PageSection, [{
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps, nextState) {
      if (nextState.isOpen !== this.state.isOpen) return true;
      if (nextProps.isHidden !== this.props.isHidden) return true;
      if (nextProps.isNoContent !== this.props.isNoContent) return true;
      if (nextProps.isOpenByDefault !== this.props.isOpenByDefault) return true;
      if (nextProps.isAccordionDisabled !== this.props.isAccordionDisabled) return true;
      if (nextProps.title !== this.props.title) return true;
      if (nextProps.fieldName !== this.props.fieldName) return true;
      if (nextProps.formName !== this.props.formName) return true;
      if (nextProps.asyncStatus !== this.props.asyncStatus) return true;
      if (nextProps.children !== this.props.children) return true;
      return false;
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      if (prevProps.isOpenByDefault !== this.props.isOpenByDefault) {
        this.setState({
          isOpen: this.props.isOpenByDefault
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var isOpen = this.state.isOpen;
      var _this$props = this.props,
          isTouchDevice = _this$props.isTouchDevice,
          isSearch = _this$props.isSearch,
          isAccordion = _this$props.isAccordion,
          isAccordionDisabled = _this$props.isAccordionDisabled,
          isTableResponsive = _this$props.isTableResponsive,
          isHidden = _this$props.isHidden,
          isDisabled = _this$props.isDisabled,
          isNoContent = _this$props.isNoContent,
          title = _this$props.title,
          actionTemplate = _this$props.actionTemplate,
          fieldName = _this$props.fieldName,
          formName = _this$props.formName,
          children = _this$props.children,
          className = _this$props.className,
          onSearchReset = _this$props.onSearchReset,
          onChange = _this$props.onChange,
          hasAsyncStatus = _this$props.hasAsyncStatus,
          isSpinnerAlwaysVisible = _this$props.isSpinnerAlwaysVisible,
          isInfoErrorVariant = _this$props.isInfoErrorVariant,
          infoMessage = _this$props.infoMessage,
          asyncStatus = _this$props.asyncStatus;
      var isInitial = asyncStatus.isInitial,
          isSuccessful = asyncStatus.isSuccessful,
          isError = asyncStatus.isError;
      var determineIsInfoErrorVariant = hasAsyncStatus && asyncStatus.isSuccessful && isInfoErrorVariant;
      var accentColor = isError ? _styles.SUNSET_ORANGE : determineIsInfoErrorVariant ? _styles.VERY_LIGHT_TANGELO : _styles.STEEL_BLUE;
      return _react.default.createElement(WrapScroll, {
        isTouchDevice: isTouchDevice,
        isHidden: isHidden,
        isDisabled: isDisabled,
        isOverflowX: true,
        accentColor: accentColor,
        className: className
      }, _react.default.createElement(TitleWrap, {
        accentColor: accentColor,
        isAccordion: isAccordion,
        isOpen: isOpen,
        isNoContent: isNoContent,
        isError: isError,
        isInitial: isInitial,
        className: CLASSNAME_SECTION_TITLE
      }, _react.default.createElement(Title, null, title), actionTemplate && _react.default.createElement(ActionsWrap, null, actionTemplate), !isInitial && isSearch && isSuccessful && _react.default.createElement(_component3.default, {
        form: formName,
        fieldName: fieldName,
        onSearchReset: onSearchReset,
        onChange: onChange
      }), isAccordion && !isNoContent && _react.default.createElement(AccordionToggle, {
        isDisabled: isAccordionDisabled || isError
      }, _react.default.createElement(AccordionSvg, {
        isVisible: !isOpen && !isInitial,
        svg: _assets.addSvg,
        className: CLASSNAME_ACCORDION_TOGGLE,
        onClick: this.openAccordion
      }), _react.default.createElement(AccordionSvg, {
        isOpen: true,
        isVisible: isOpen && !isInitial,
        svg: _assets.subtractSvg,
        className: CLASSNAME_ACCORDION_TOGGLE,
        onClick: this.closeAccordion
      })), hasAsyncStatus && _react.default.createElement(_Loader.SpinnerLoader, {
        isAlwaysVisible: isSpinnerAlwaysVisible,
        asyncStatus: asyncStatus,
        size: 2,
        isStatic: true
      }), _react.default.createElement(ErrorFloatStyled, {
        isInfoVariantMaybe: determineIsInfoErrorVariant,
        hasAsyncStatus: hasAsyncStatus,
        infoMessage: infoMessage,
        asyncStatus: asyncStatus,
        tooltipPosition: "left"
      })), _react.default.createElement(ContentScroll, {
        isTouchDevice: isTouchDevice,
        isOverflowX: true,
        isResponsiveAndOpen: isTableResponsive && isOpen,
        className: CLASSNAME_SECTION_CONTENT
      }, !isAccordion && children, isAccordion && isOpen && children));
    }
  }]);

  return PageSection;
}(_react.default.Component);

PageSection.propTypes = {
  isTouchDevice: _propTypes.bool.isRequired,
  isSearch: _propTypes.bool,
  isAccordion: _propTypes.bool,
  isOpenByDefault: _propTypes.bool,
  isAccordionDisabled: _propTypes.bool,
  isTableResponsive: _propTypes.bool,
  isHidden: _propTypes.bool,
  isDisabled: _propTypes.bool,
  isNoContent: _propTypes.bool,
  actionTemplate: _propTypes.node,
  title: _propTypes.string.isRequired,
  fieldName: _propTypes.string,
  formName: _propTypes.string,
  children: _propTypes.node.isRequired,
  className: _propTypes.string,
  onSearchReset: _propTypes.func,
  onChange: _propTypes.func,
  hasAsyncStatus: _propTypes.bool,
  isSpinnerAlwaysVisible: _propTypes.bool,
  isInfoErrorVariant: _propTypes.bool,
  infoMessage: _propTypes.string,
  asyncStatus: (0, _propTypes.shape)(_utils.PROP_ASYNC_STATUS_OPTIONAL)
};
PageSection.defaultProps = {
  isSearch: false,
  isAccordion: false,
  isOpenByDefault: false,
  isAccordionDisabled: false,
  isTableResponsive: false,
  isHidden: false,
  isDisabled: false,
  isNoContent: false,
  actionTemplate: undefined,
  fieldName: undefined,
  formName: undefined,
  className: undefined,
  onSearchReset: function onSearchReset() {},
  onChange: function onChange() {},
  hasAsyncStatus: false,
  isSpinnerAlwaysVisible: false,
  isInfoErrorVariant: false,
  infoMessage: undefined,
  asyncStatus: {}
};
var _default = PageSection;
exports.default = _default;