import _taggedTemplateLiteralLoose from "@babel/runtime/helpers/taggedTemplateLiteralLoose";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";

var _templateObject, _templateObject2;

import React from 'react';
import styled from 'styled-components';
import { useForm, Controller } from 'react-hook-form';
import { companiesApi, useGlobalLabels } from '@sentecacommerce-theme/lib';
import { ModalForm } from '../../ModalForm';
import { TextInput } from '../../TextInput';
export var EditCompanyModal = (_ref) => {
  var _queryData$company, _queryData$company2, _queryData$company3, _queryData$company4;

  var {
    handleClose,
    queryData
  } = _ref;
  // TODO: error handling
  var [mutate, {
    isLoading,
    isError
  }] = companiesApi.useUpdate(handleClose);
  var {
    forms,
    company,
    buttons,
    infoMessages
  } = useGlobalLabels();

  var onSubmit = /*#__PURE__*/function () {
    var _ref2 = _asyncToGenerator(function* (data) {
      var _queryData$_id;

      mutate({
        id: (_queryData$_id = queryData._id) != null ? _queryData$_id : '',
        dto: {
          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'
        }
      });
    });

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

  var {
    handleSubmit,
    errors,
    control
  } = useForm({
    defaultValues: {
      companyId: queryData == null ? void 0 : (_queryData$company = queryData.company) == null ? void 0 : _queryData$company.companyId,
      vatId: queryData == null ? void 0 : (_queryData$company2 = queryData.company) == null ? void 0 : _queryData$company2.vatId,
      companyName: queryData == null ? void 0 : (_queryData$company3 = queryData.company) == null ? void 0 : _queryData$company3.companyName,
      recipient: queryData == null ? void 0 : (_queryData$company4 = queryData.company) == null ? void 0 : _queryData$company4.companyPerson,
      companyCity: queryData == null ? void 0 : queryData.city,
      zipCode: queryData == null ? void 0 : queryData.postalCode,
      companyStreet: queryData == null ? void 0 : queryData.streetName,
      companyStreetNumber: queryData == null ? void 0 : queryData.streetNumber
    },
    mode: 'onChange',
    reValidateMode: 'onChange'
  });
  var Inputs = /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(InputWrapper, null, /*#__PURE__*/React.createElement(Controller, {
    control: control,
    name: "vatId",
    rules: {
      required: forms.requiredField
    },
    as: /*#__PURE__*/React.createElement(TextInput, {
      label: company.vatNumber,
      id: "VAT",
      type: "text",
      errors: errors,
      name: "VAT",
      isRequired: false
    })
  }), /*#__PURE__*/React.createElement(Controller, {
    control: control,
    name: "companyId",
    rules: {
      required: forms.requiredField
    },
    as: /*#__PURE__*/React.createElement(TextInput, {
      label: company.companyId,
      id: "Company ID",
      type: "text",
      isRequired: true,
      errors: errors,
      name: "Company ID"
    })
  }), /*#__PURE__*/React.createElement(Controller, {
    control: control,
    name: "recipient",
    rules: {
      required: forms.requiredField
    },
    as: /*#__PURE__*/React.createElement(TextInput, {
      label: company.recipient,
      id: "recipient",
      type: "text",
      isRequired: true,
      errors: errors,
      name: "recipient"
    })
  }), /*#__PURE__*/React.createElement(Controller, {
    control: control,
    name: "companyName",
    rules: {
      required: forms.requiredField
    },
    as: /*#__PURE__*/React.createElement(TextInput, {
      label: company.companyName,
      id: "company-name",
      type: "text",
      isRequired: true,
      errors: errors,
      name: "company-name"
    })
  }), /*#__PURE__*/React.createElement(CityZip, null, /*#__PURE__*/React.createElement(Controller, {
    control: control,
    name: "companyCity",
    rules: {
      required: forms.requiredField
    },
    as: /*#__PURE__*/React.createElement(TextInput, {
      label: company.city,
      id: "company-city",
      type: "text",
      isRequired: true,
      errors: errors,
      name: "company-city"
    })
  }), /*#__PURE__*/React.createElement(Controller, {
    control: control,
    name: "zipCode",
    rules: {
      required: forms.requiredField
    },
    as: /*#__PURE__*/React.createElement(TextInput, {
      label: company.zipCode,
      id: "zipCode",
      type: "number",
      isRequired: true,
      errors: errors,
      name: "zipCode"
    })
  })), /*#__PURE__*/React.createElement(Controller, {
    control: control,
    name: "companyStreet",
    rules: {
      required: forms.requiredField
    },
    as: /*#__PURE__*/React.createElement(TextInput, {
      label: company.street,
      id: "company-street",
      type: "text",
      isRequired: true,
      errors: errors,
      name: "company-street"
    })
  }), /*#__PURE__*/React.createElement(Controller, {
    control: control,
    name: "companyStreetNumber",
    as: /*#__PURE__*/React.createElement(TextInput, {
      label: company.streetNum,
      id: "company-street-number",
      type: "text",
      errors: errors,
      name: "company-street-number"
    })
  })));
  return /*#__PURE__*/React.createElement(ModalForm, {
    handleClose: handleClose,
    title: company.editCompanyDetails,
    onSubmit: handleSubmit(onSubmit),
    Inputs: Inputs,
    error: isError ? infoMessages.invalidData : undefined,
    disableSubmit: isLoading || isError,
    buttonLabel: buttons.submit
  });
};
var InputWrapper = styled.div.attrs({
  className: 'mt-1 mb-2 pb-1'
})(_templateObject || (_templateObject = _taggedTemplateLiteralLoose([""])));
var CityZip = styled.span.attrs({
  className: 'd-flex justify-content-between'
})(_templateObject2 || (_templateObject2 = _taggedTemplateLiteralLoose(["\n  > * {\n    &:first-child {\n      flex-basis: 120%;\n      margin-right: 2rem;\n    }\n  }\n"])));
//# sourceMappingURL=index.js.map