"use strict";

exports.__esModule = true;
exports.default = void 0;
var _get2 = _interopRequireDefault(require("lodash/fp/get"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _novaIcons = require("@coorpacademy/nova-icons");
var _brandFormGroup = _interopRequireDefault(require("../../molecule/brand-form-group"));
var _provider = _interopRequireDefault(require("../../atom/provider"));
var _button = _interopRequireDefault(require("../../atom/button"));
var _buttonLink = _interopRequireDefault(require("../../atom/button-link"));
var _link = _interopRequireDefault(require("../../atom/link"));
var _loader = _interopRequireDefault(require("../../atom/loader"));
var _style = _interopRequireDefault(require("./style.css"));
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 && {}.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(e) { return e && e.__esModule ? e : { default: e }; }
const buildButtonSection = (onSubmit, submitValue, onReset, resetValue, tooltip, disabled, isModified, isPending, massiveBattleForm) => {
  if (!onSubmit && !onReset) {
    return null;
  }
  const disabledSubmit = disabled || isPending || !isModified;
  const submitButton = onSubmit ? /*#__PURE__*/_react.default.createElement("div", {
    "data-tip": tooltip.title,
    "data-for": "submitButton",
    className: massiveBattleForm ? _style.default.massiveBattleButton : _style.default.saveButton
  }, /*#__PURE__*/_react.default.createElement(_buttonLink.default, {
    type: "primary",
    label: submitValue,
    disabled: disabledSubmit,
    "data-testid": "submit-button",
    usage: "submit",
    onClick: onSubmit,
    icon: {
      position: 'left',
      faIcon: {
        name: 'circle-check',
        color: '#FFFFFF',
        size: 16
      }
    }
  })) : null;
  const resetButton = onReset ? /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.resetButton
  }, /*#__PURE__*/_react.default.createElement(_buttonLink.default, {
    usage: "reset",
    label: resetValue,
    "data-testid": "reset-button",
    onClick: onReset,
    icon: {
      position: 'left',
      faIcon: {
        name: 'trash',
        size: 16
      }
    }
  })) : null;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.buttons
  }, submitButton, resetButton);
};
const BrandForm = (props, context) => {
  const {
    groups,
    disabled,
    isModified,
    isPending,
    onSubmit,
    submitValue,
    onReset,
    resetValue,
    back,
    tooltip,
    isLoading,
    theme
  } = props;
  const {
    skin
  } = context;
  const darkColor = (0, _get2.default)('common.dark', skin);
  const massiveBattleForm = groups[0]?.fields?.[0]?.type === 'titleAndInput';
  const backView = back ? /*#__PURE__*/_react.default.createElement("p", {
    className: _style.default.back
  }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionNavigationArrowLeft, {
    style: {
      color: darkColor
    },
    className: _style.default.arrowBack
  }), /*#__PURE__*/_react.default.createElement(_link.default, {
    href: back.link,
    className: _style.default.backDesc
  }, back.desc)) : null;
  const brandGroups = groups.map((group, index) => {
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.group,
      key: index
    }, /*#__PURE__*/_react.default.createElement(_brandFormGroup.default, group));
  });
  const buttonSection = buildButtonSection(onSubmit, submitValue, onReset, resetValue, tooltip, disabled, isModified, isPending, darkColor, theme, massiveBattleForm);
  const handleSubmit = (0, _react.useMemo)(() => e => {
    e.preventDefault();
    return onSubmit(e);
  }, [onSubmit]);
  const handleReset = (0, _react.useMemo)(() => e => {
    e.preventDefault();
    return onReset(e);
  }, [onReset]);
  return /*#__PURE__*/_react.default.createElement("div", null, backView, isLoading ? /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.loaderContainer
  }, /*#__PURE__*/_react.default.createElement(_loader.default, {
    className: _style.default.loader,
    theme: "coorpmanager"
  })) : /*#__PURE__*/_react.default.createElement("form", {
    onSubmit: handleSubmit,
    onReset: handleReset,
    className: _style.default.form
  }, brandGroups, buttonSection));
};
BrandForm.contextTypes = {
  skin: _provider.default.childContextTypes.skin
};
BrandForm.defaultProps = {
  tooltip: {
    title: '',
    place: 'top'
  }
};
BrandForm.propTypes = process.env.NODE_ENV !== "production" ? {
  groups: _propTypes.default.arrayOf(_propTypes.default.shape(_brandFormGroup.default.propTypes)).isRequired,
  disabled: _propTypes.default.bool,
  isModified: _propTypes.default.bool,
  isPending: _propTypes.default.bool,
  onSubmit: _propTypes.default.func,
  submitValue: _button.default.propTypes.submitValue,
  onReset: _propTypes.default.func,
  resetValue: _button.default.propTypes.submitValue,
  back: _propTypes.default.shape({
    desc: _link.default.propTypes.children,
    link: _link.default.propTypes.href
  }),
  tooltip: _propTypes.default.shape({
    title: _propTypes.default.string,
    place: _propTypes.default.string
  }),
  isLoading: _propTypes.default.bool,
  theme: _propTypes.default.string
} : {};
var _default = exports.default = BrandForm;
//# sourceMappingURL=index.js.map