"use strict";

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

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

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

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

var _CheckoutPickupFromStore = require("../CheckoutPickupFromStore");

var _CheckoutCourierInput = require("../CheckoutCourierInput");

var _UI = require("../../../UI");

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

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

var _CheckoutAddress = require("../CheckoutAddress");

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

var _templateObject;

var CheckoutSelectDeliveryOption = function CheckoutSelectDeliveryOption() {
  var _courierLabelMap, _courierInfoMap;

  var _usePixel = (0, _lib.usePixel)(),
      pixelCheckoutStepOption = _usePixel.pixelCheckoutStepOption;

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

  var couriers = (0, _lib.useCheckoutDelivery)({
    merchantKey: 'DISTRICT'
  });

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

  var labels = (0, _lib.useLocalLabels)();
  var courierNames = Object.keys(couriers || {});
  var courierLabelMap = (_courierLabelMap = {}, _courierLabelMap[_lib.ShippingModulesTypeEnum.Speedy] = labels.speedyOptionLabel, _courierLabelMap[_lib.ShippingModulesTypeEnum.Econt] = labels.econtOptionLabel, _courierLabelMap[_lib.ShippingModulesTypeEnum.Evropat] = labels.evropatOptionLabel, _courierLabelMap[_lib.ShippingModulesTypeEnum.Fan] = labels.fancourierOptionLabel, _courierLabelMap[_lib.ShippingModulesTypeEnum.PickUpFromStore] = labels.pickUpFromStoreOptionLabel, _courierLabelMap);
  var courierInfoMap = (_courierInfoMap = {}, _courierInfoMap[_lib.ShippingModulesTypeEnum.Speedy] = labels.speedyDescriptionLabel, _courierInfoMap[_lib.ShippingModulesTypeEnum.Econt] = labels.econtDescriptionLabel, _courierInfoMap[_lib.ShippingModulesTypeEnum.Evropat] = labels.evropatDescriptionLabel, _courierInfoMap[_lib.ShippingModulesTypeEnum.Fan] = labels.fancourierDescriptionLabel, _courierInfoMap[_lib.ShippingModulesTypeEnum.PickUpFromStore] = labels.pickUpFromStoreDescriptionLabel, _courierInfoMap);
  var courierOptions = courierNames.map(function (courierName) {
    var courier = couriers == null ? void 0 : couriers[courierName];
    return {
      label: courierLabelMap[courierName],
      info: courierInfoMap[courierName],
      value: courierName,
      methods: courier == null ? void 0 : courier.methods,
      isSelected: courier == null ? void 0 : courier.hasSelectedChild,
      onClick: courier == null ? void 0 : courier.methods[0].onClick
    };
  });
  var methodsMap = {
    Address: labels.deliverToAddress,
    Office: labels.deliverToOffice
  };
  var selectedCourierName = courierNames.find(function (courierName) {
    return couriers == null ? void 0 : couriers[courierName].hasSelectedChild;
  });
  var selectedCourier = couriers == null ? void 0 : couriers[selectedCourierName];

  _react["default"].useEffect(function () {
    if (selectedCourierName) {
      pixelCheckoutStepOption(1, selectedCourierName);
    }
  }, [selectedCourierName]);

  var selectedDeliveryType = '';
  var deliveryOptions = (selectedCourier == null ? void 0 : selectedCourier.methods.map(function (method) {
    if (method.selected) selectedDeliveryType = method.methodKey;
    return {
      label: methodsMap[method.methodKey],
      onClick: method.onClick,
      isSelected: method.selected,
      key: method.methodKey
    };
  })) || [];
  var isPickupFromStore = selectedCourierName === _lib.ShippingModulesTypeEnum.PickUpFromStore;
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("div", {
    className: "col-12"
  }, /*#__PURE__*/_react["default"].createElement(_CheckoutCourierInput.CheckoutCourierInput, {
    options: courierOptions
  }), /*#__PURE__*/_react["default"].createElement(_Common.FormError, {
    errorRef: errorRef,
    errors: errors,
    fieldName: "courier"
  }), selectedCourierName && /*#__PURE__*/_react["default"].createElement(_UI.ScrollToOnRender, {
    key: selectedCourierName
  })), !isPickupFromStore && (deliveryOptions == null ? void 0 : deliveryOptions.length) > 1 && /*#__PURE__*/_react["default"].createElement("div", {
    className: "col-12"
  }, /*#__PURE__*/_react["default"].createElement(_UI.CheckoutTitle, {
    className: "my-4 pt-2",
    data: labels.deliveryType
  }), /*#__PURE__*/_react["default"].createElement(_Common.TabRadio, {
    tabStyle: "button",
    name: "adbook-deliveryType",
    options: deliveryOptions
  })), !isPickupFromStore && (deliveryOptions == null ? void 0 : deliveryOptions.length) < 2 && /*#__PURE__*/_react["default"].createElement("div", {
    className: "col-12"
  }, /*#__PURE__*/_react["default"].createElement(_UI.CheckoutTitle, {
    className: "mt-4 pt-2 mb-0 pb-0",
    data: labels.deliveryStepTitle
  })), isPickupFromStore && /*#__PURE__*/_react["default"].createElement(_CheckoutPickupFromStore.CheckoutPickupFromStore, null), !isPickupFromStore && selectedCourierName && /*#__PURE__*/_react["default"].createElement(_CheckoutAddress.CheckoutAddress, {
    selectedCourier: selectedCourierName,
    isOffice: selectedDeliveryType === 'Office'
  }), !selectedCourierName && /*#__PURE__*/_react["default"].createElement(NoCourierSelected, null, labels.noCourierSelectedLabel));
};

exports.CheckoutSelectDeliveryOption = CheckoutSelectDeliveryOption;

var NoCourierSelected = _styledComponents["default"].div(_templateObject || (_templateObject = (0, _taggedTemplateLiteralLoose2["default"])(["\n  font-size: 1.6rem;\n"])));
//# sourceMappingURL=index.js.map