"use strict";

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

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

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

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

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

var CheckoutAddress = function CheckoutAddress(_ref) {
  var _fieldsLabel, _data$moduleData, _data$moduleData$, _data$moduleData$$mod, _data$moduleData$$mod2, _data$moduleData$$mod3, _ref2, _data$postalCode;

  var selectedCourier = _ref.selectedCourier,
      isOffice = _ref.isOffice;

  var _useCheckoutFormShipp = (0, _lib.useCheckoutFormShippingState)(),
      data = _useCheckoutFormShipp.data,
      onInputChange = _useCheckoutFormShipp.onInputChange,
      errors = _useCheckoutFormShipp.errors;

  var methods = (0, _lib.useShippingCourierMethods)({
    courier: selectedCourier,
    address: data
  });
  var methodNames = Object.keys(methods || {});
  var labels = (0, _lib.useLocalLabels)();

  var _useCheckoutErrorFiel = (0, _lib.useCheckoutErrorFields)(),
      errorRef = _useCheckoutErrorFiel.errorRef;

  var fieldsLabel = (_fieldsLabel = {}, _fieldsLabel[_lib.NomenclatureMethodsEnum.Region] = labels.regionLabel, _fieldsLabel[_lib.NomenclatureMethodsEnum.City] = labels.cityLabel, _fieldsLabel[_lib.NomenclatureMethodsEnum.Quarter] = isOffice ? false : labels.quarterLabel, _fieldsLabel[_lib.NomenclatureMethodsEnum.Street] = isOffice ? false : labels.streetLabel, _fieldsLabel[_lib.NomenclatureMethodsEnum.Office] = isOffice ? labels.officeLabel : false, _fieldsLabel);

  var onChange = function onChange(e) {
    onInputChange == null ? void 0 : onInputChange(e.target.name, e.target.value);
  };

  var zipCode = selectedCourier === _lib.ShippingModulesTypeEnum.Fan && (data == null ? void 0 : (_data$moduleData = data.moduleData) == null ? void 0 : (_data$moduleData$ = _data$moduleData[0]) == null ? void 0 : (_data$moduleData$$mod = _data$moduleData$.moduleData) == null ? void 0 : (_data$moduleData$$mod2 = _data$moduleData$$mod.address) == null ? void 0 : (_data$moduleData$$mod3 = _data$moduleData$$mod2.street) == null ? void 0 : _data$moduleData$$mod3.zipCode);
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, methodNames.map(function (name) {
    var method = methods == null ? void 0 : methods[name];
    var label = fieldsLabel[name];
    if (!label) return null;

    if (!method) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "col-12",
        key: (name == null ? void 0 : name.toUpperCase()) + "__" + name
      }, /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
        inputSize: "normal",
        value: (data == null ? void 0 : data[name]) || '',
        onChange: onChange,
        name: name,
        id: "adbook-" + name,
        label: label
      }), /*#__PURE__*/_react["default"].createElement(_Common.FormError, {
        errorRef: errorRef,
        absolute: true,
        errors: errors,
        fieldName: name
      }));
    }

    return /*#__PURE__*/_react["default"].createElement("div", {
      key: (name == null ? void 0 : name.toUpperCase()) + "__" + name,
      className: isOffice ? 'col-12 col-md-6' : 'col-12'
    }, /*#__PURE__*/_react["default"].createElement(_Common.DeliveryAutosuggest, {
      method: method,
      label: label,
      minLength: !isOffice ? 3 : 0
    }), /*#__PURE__*/_react["default"].createElement(_Common.FormError, {
      errorRef: errorRef,
      absolute: true,
      errors: errors,
      fieldName: name
    }));
  }), !isOffice && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("div", {
    className: "col-12 col-md-6"
  }, /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
    inputSize: "normal",
    value: (data == null ? void 0 : data.streetNumber) || '',
    onChange: onChange,
    name: "streetNumber",
    id: "adbook-streetNumber",
    label: labels.streetNumberLabel
  }), /*#__PURE__*/_react["default"].createElement(_Common.FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "streetNumber"
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "col-12 col-md-6"
  }, /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
    inputSize: "normal",
    value: (data == null ? void 0 : data.building) || '',
    onChange: onChange,
    name: "building",
    id: "adbook-building",
    label: labels.buildingLabel
  }), /*#__PURE__*/_react["default"].createElement(_Common.FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "building"
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "col-12 col-md-6"
  }, /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
    inputSize: "normal",
    value: (data == null ? void 0 : data.entrance) || '',
    onChange: onChange,
    name: "entrance",
    id: "adbook-entrance",
    label: labels.entranceLabel
  }), /*#__PURE__*/_react["default"].createElement(_Common.FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "entrance"
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "col-12 col-md-6"
  }, /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
    inputSize: "normal",
    value: (data == null ? void 0 : data.floor) || '',
    onChange: onChange,
    name: "floor",
    id: "adbook-floor",
    label: labels.floorLabel
  }), /*#__PURE__*/_react["default"].createElement(_Common.FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "floor"
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "col-12 col-md-6"
  }, /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
    inputSize: "normal",
    value: (data == null ? void 0 : data.apartment) || '',
    onChange: onChange,
    name: "apartment",
    id: "adbook-apartment",
    label: labels.apartmentLabel
  }), /*#__PURE__*/_react["default"].createElement(_Common.FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "apartment"
  })), (errors == null ? void 0 : errors.any) && /*#__PURE__*/_react["default"].createElement("div", {
    className: "col-12 d-md-none",
    style: {
      marginTop: '-0.6rem'
    }
  }, /*#__PURE__*/_react["default"].createElement(_Common.FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "any"
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "col-12 col-md-6"
  }, /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
    inputSize: "normal",
    type: "number",
    value: (_ref2 = (_data$postalCode = data == null ? void 0 : data.postalCode) != null ? _data$postalCode : zipCode) != null ? _ref2 : '',
    hasValue: Boolean(data == null ? void 0 : data.postalCode) || Boolean(zipCode),
    disabled: true,
    onChange: onChange,
    name: "postalCode",
    id: "adbook-postalCode",
    label: labels.postalCodeLabel
  }), /*#__PURE__*/_react["default"].createElement(_Common.FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "postalCode"
  })), (errors == null ? void 0 : errors.any) && /*#__PURE__*/_react["default"].createElement("div", {
    className: "col-12 d-none d-md-block",
    style: {
      marginTop: '-0.6rem'
    }
  }, /*#__PURE__*/_react["default"].createElement(_Common.FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "any"
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "col-12"
  }, /*#__PURE__*/_react["default"].createElement(_Common.TextInput, {
    inputSize: "normal",
    value: (data == null ? void 0 : data.additionalAddressInfo) || '',
    onChange: onChange,
    name: "additionalAddressInfo",
    id: "adbook-additionalAddressInfo",
    label: labels.additionalAddressInfoLabel
  }), /*#__PURE__*/_react["default"].createElement(_Common.FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "additionalAddressInfo"
  }))));
};

exports.CheckoutAddress = CheckoutAddress;
//# sourceMappingURL=index.js.map