"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.AddCompanyModal = void 0;

var _taggedTemplateLiteralLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteralLoose"));

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

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

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

var _reactHookForm = require("react-hook-form");

var _lib = require("@sentecacommerce-theme/lib");

var _Common = require("../../../../../Common");

var _templateObject, _templateObject2;

var AddCompanyModal = function AddCompanyModal(_ref) {
  var handleClose = _ref.handleClose;

  // TODO: error handling
  var _companiesApi$useAdd = _lib.companiesApi.useAdd(handleClose),
      mutate = _companiesApi$useAdd[0],
      _companiesApi$useAdd$ = _companiesApi$useAdd[1],
      isLoading = _companiesApi$useAdd$.isLoading,
      isError = _companiesApi$useAdd$.isError;

  var onSubmit = /*#__PURE__*/function () {
    var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(data) {
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              mutate({
                city: data.companyCity,
                postalCode: data.zipCode,
                streetName: data.companyStreet,
                streetNumber: data.companyStreetNumber,
                company: {
                  companyId: data.companyId,
                  companyName: data.companyName,
                  companyPerson: data.recipient,
                  vatId: data.vatId
                },
                countryCode: 'BG'
              });

            case 1:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function onSubmit(_x) {
      return _ref2.apply(this, arguments);
    };
  }();

  var _useForm = (0, _reactHookForm.useForm)({
    defaultValues: {
      companyId: '',
      vatId: '',
      companyName: '',
      recipient: '',
      companyAddress: ''
    },
    mode: 'onChange',
    reValidateMode: 'onChange'
  }),
      handleSubmit = _useForm.handleSubmit,
      errors = _useForm.errors,
      control = _useForm.control;

  var _useGlobalLabels = (0, _lib.useGlobalLabels)(),
      forms = _useGlobalLabels.forms,
      company = _useGlobalLabels.company,
      buttons = _useGlobalLabels.buttons,
      infoMessages = _useGlobalLabels.infoMessages;

  var Inputs = /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(InputWrapper, null, /*#__PURE__*/_react["default"].createElement(_reactHookForm.Controller, {
    control: control,
    name: "vatId",
    rules: {
      required: forms.requiredField,
      maxLength: {
        value: 13,
        message: company.invalidVat
      }
    },
    as: /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
      label: company.vatNumber,
      id: "VAT",
      type: "text",
      errors: errors,
      name: "VAT",
      isRequired: false
    })
  }), /*#__PURE__*/_react["default"].createElement(_reactHookForm.Controller, {
    control: control,
    name: "companyId",
    rules: {
      required: forms.requiredField
    },
    as: /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
      label: company.companyId,
      id: "Company ID",
      type: "text",
      isRequired: true,
      errors: errors,
      name: "Company ID"
    })
  }), /*#__PURE__*/_react["default"].createElement(_reactHookForm.Controller, {
    control: control,
    name: "recipient",
    rules: {
      required: forms.requiredField
    },
    as: /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
      label: company.recipient,
      id: "recipient",
      type: "text",
      isRequired: true,
      errors: errors,
      name: "recipient"
    })
  }), /*#__PURE__*/_react["default"].createElement(_reactHookForm.Controller, {
    control: control,
    name: "companyName",
    rules: {
      required: forms.requiredField
    },
    as: /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
      label: company.companyName,
      id: "company-name",
      type: "text",
      isRequired: true,
      errors: errors,
      name: "company-name"
    })
  }), /*#__PURE__*/_react["default"].createElement(CityZip, null, /*#__PURE__*/_react["default"].createElement(_reactHookForm.Controller, {
    control: control,
    name: "companyCity",
    rules: {
      required: forms.requiredField
    },
    as: /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
      label: company.city,
      id: "company-city",
      type: "text",
      isRequired: true,
      errors: errors,
      name: "company-city"
    })
  }), /*#__PURE__*/_react["default"].createElement(_reactHookForm.Controller, {
    control: control,
    name: "zipCode",
    rules: {
      required: forms.requiredField
    },
    as: /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
      label: company.zipCode,
      id: "zipCode",
      type: "number",
      isRequired: true,
      errors: errors,
      name: "zipCode"
    })
  })), /*#__PURE__*/_react["default"].createElement(_reactHookForm.Controller, {
    control: control,
    name: "companyStreet",
    rules: {
      required: forms.requiredField
    },
    as: /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
      label: company.street,
      id: "company-street",
      type: "text",
      isRequired: true,
      errors: errors,
      name: "company-street"
    })
  }), /*#__PURE__*/_react["default"].createElement(_reactHookForm.Controller, {
    control: control,
    name: "companyStreetNumber",
    as: /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
      label: company.streetNum,
      id: "company-street-number",
      type: "text",
      errors: errors,
      name: "company-street-number"
    })
  })));

  return /*#__PURE__*/_react["default"].createElement(_Common.ModalForm, {
    handleClose: handleClose,
    title: buttons.addNewCompany,
    onSubmit: handleSubmit(onSubmit),
    Inputs: Inputs,
    error: isError ? infoMessages.invalidData : undefined,
    disableSubmit: isLoading || isError
  });
};

exports.AddCompanyModal = AddCompanyModal;

var InputWrapper = _styledComponents["default"].div.attrs({
  className: 'mt-1 mb-2 pb-1'
})(_templateObject || (_templateObject = (0, _taggedTemplateLiteralLoose2["default"])([""])));

var CityZip = _styledComponents["default"].span.attrs({
  className: 'd-flex justify-content-between'
})(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteralLoose2["default"])(["\n  > * {\n    &:first-child {\n      flex-basis: 120%;\n      margin-right: 2rem;\n    }\n  }\n"])));
//# sourceMappingURL=index.js.map