import React from 'react';
import { useLocalLabels, useCheckoutFormShippingState, useCheckoutErrorFields, ShippingModulesTypeEnum, useShippingCourierMethods, NomenclatureMethodsEnum } from '@sentecacommerce-theme/lib';
import { TextInput as Input, DeliveryAutosuggest, FormError } from '../../../../../Common';
export var CheckoutAddress = (_ref) => {
  var _data$moduleData, _data$moduleData$, _data$moduleData$$mod, _data$moduleData$$mod2, _data$moduleData$$mod3, _ref2, _data$postalCode;

  var {
    selectedCourier,
    isOffice
  } = _ref;
  var {
    data,
    onInputChange,
    errors
  } = useCheckoutFormShippingState();
  var methods = useShippingCourierMethods({
    courier: selectedCourier,
    address: data
  });
  var methodNames = Object.keys(methods || {});
  var labels = useLocalLabels();
  var {
    errorRef
  } = useCheckoutErrorFields();
  var fieldsLabel = {
    [NomenclatureMethodsEnum.Region]: labels.regionLabel,
    [NomenclatureMethodsEnum.City]: labels.cityLabel,
    [NomenclatureMethodsEnum.Quarter]: isOffice ? false : labels.quarterLabel,
    [NomenclatureMethodsEnum.Street]: isOffice ? false : labels.streetLabel,
    [NomenclatureMethodsEnum.Office]: isOffice ? labels.officeLabel : false
  };

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

  var zipCode = selectedCourier === 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.createElement(React.Fragment, null, methodNames.map(name => {
    var method = methods == null ? void 0 : methods[name];
    var label = fieldsLabel[name];
    if (!label) return null;

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

    return /*#__PURE__*/React.createElement("div", {
      key: (name == null ? void 0 : name.toUpperCase()) + "__" + name,
      className: isOffice ? 'col-12 col-md-6' : 'col-12'
    }, /*#__PURE__*/React.createElement(DeliveryAutosuggest, {
      method: method,
      label: label,
      minLength: !isOffice ? 3 : 0
    }), /*#__PURE__*/React.createElement(FormError, {
      errorRef: errorRef,
      absolute: true,
      errors: errors,
      fieldName: name
    }));
  }), !isOffice && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "col-12 col-md-6"
  }, /*#__PURE__*/React.createElement(Input, {
    inputSize: "normal",
    value: (data == null ? void 0 : data.streetNumber) || '',
    onChange: onChange,
    name: "streetNumber",
    id: "adbook-streetNumber",
    label: labels.streetNumberLabel
  }), /*#__PURE__*/React.createElement(FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "streetNumber"
  })), /*#__PURE__*/React.createElement("div", {
    className: "col-12 col-md-6"
  }, /*#__PURE__*/React.createElement(Input, {
    inputSize: "normal",
    value: (data == null ? void 0 : data.building) || '',
    onChange: onChange,
    name: "building",
    id: "adbook-building",
    label: labels.buildingLabel
  }), /*#__PURE__*/React.createElement(FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "building"
  })), /*#__PURE__*/React.createElement("div", {
    className: "col-12 col-md-6"
  }, /*#__PURE__*/React.createElement(Input, {
    inputSize: "normal",
    value: (data == null ? void 0 : data.entrance) || '',
    onChange: onChange,
    name: "entrance",
    id: "adbook-entrance",
    label: labels.entranceLabel
  }), /*#__PURE__*/React.createElement(FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "entrance"
  })), /*#__PURE__*/React.createElement("div", {
    className: "col-12 col-md-6"
  }, /*#__PURE__*/React.createElement(Input, {
    inputSize: "normal",
    value: (data == null ? void 0 : data.floor) || '',
    onChange: onChange,
    name: "floor",
    id: "adbook-floor",
    label: labels.floorLabel
  }), /*#__PURE__*/React.createElement(FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "floor"
  })), /*#__PURE__*/React.createElement("div", {
    className: "col-12 col-md-6"
  }, /*#__PURE__*/React.createElement(Input, {
    inputSize: "normal",
    value: (data == null ? void 0 : data.apartment) || '',
    onChange: onChange,
    name: "apartment",
    id: "adbook-apartment",
    label: labels.apartmentLabel
  }), /*#__PURE__*/React.createElement(FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "apartment"
  })), (errors == null ? void 0 : errors.any) && /*#__PURE__*/React.createElement("div", {
    className: "col-12 d-md-none",
    style: {
      marginTop: '-0.6rem'
    }
  }, /*#__PURE__*/React.createElement(FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "any"
  })), /*#__PURE__*/React.createElement("div", {
    className: "col-12 col-md-6"
  }, /*#__PURE__*/React.createElement(Input, {
    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.createElement(FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "postalCode"
  })), (errors == null ? void 0 : errors.any) && /*#__PURE__*/React.createElement("div", {
    className: "col-12 d-none d-md-block",
    style: {
      marginTop: '-0.6rem'
    }
  }, /*#__PURE__*/React.createElement(FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "any"
  })), /*#__PURE__*/React.createElement("div", {
    className: "col-12"
  }, /*#__PURE__*/React.createElement(Input, {
    inputSize: "normal",
    value: (data == null ? void 0 : data.additionalAddressInfo) || '',
    onChange: onChange,
    name: "additionalAddressInfo",
    id: "adbook-additionalAddressInfo",
    label: labels.additionalAddressInfoLabel
  }), /*#__PURE__*/React.createElement(FormError, {
    errorRef: errorRef,
    absolute: true,
    errors: errors,
    fieldName: "additionalAddressInfo"
  }))));
};
//# sourceMappingURL=index.js.map