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

var _templateObject;

import React from 'react';
import { CheckoutPickupFromStore } from '../CheckoutPickupFromStore';
import { CheckoutCourierInput } from '../CheckoutCourierInput';
import { CheckoutTitle, ScrollToOnRender } from '../../../UI';
import { TabRadio, FormError } from '../../../../../Common';
import { useCheckoutDelivery, useLocalLabels, useCheckoutFormShippingState, useCheckoutErrorFields, ShippingModulesTypeEnum, usePixel, useConfigState } from '@sentecacommerce-theme/lib';
import { CheckoutAddress } from '../CheckoutAddress';
import styled from 'styled-components';
export var CheckoutSelectDeliveryOption = () => {
  var {
    pixelCheckoutStepOption
  } = usePixel();
  var {
    errors
  } = useCheckoutFormShippingState();
  var {
    defaultMerchant
  } = useConfigState();
  var couriers = useCheckoutDelivery({
    merchantKey: defaultMerchant
  });
  var {
    errorRef
  } = useCheckoutErrorFields();
  var labels = useLocalLabels();
  var courierNames = Object.keys(couriers || {});
  var courierLabelMap = {
    [ShippingModulesTypeEnum.Speedy]: labels.speedyOptionLabel,
    [ShippingModulesTypeEnum.Econt]: labels.econtOptionLabel,
    [ShippingModulesTypeEnum.Evropat]: labels.evropatOptionLabel,
    [ShippingModulesTypeEnum.Fan]: labels.fancourierOptionLabel,
    [ShippingModulesTypeEnum.PickUpFromStore]: labels.pickUpFromStoreOptionLabel
  };
  var courierInfoMap = {
    [ShippingModulesTypeEnum.Speedy]: labels.speedyDescriptionLabel,
    [ShippingModulesTypeEnum.Econt]: labels.econtDescriptionLabel,
    [ShippingModulesTypeEnum.Evropat]: labels.evropatDescriptionLabel,
    [ShippingModulesTypeEnum.Fan]: labels.fancourierDescriptionLabel,
    [ShippingModulesTypeEnum.PickUpFromStore]: labels.pickUpFromStoreDescriptionLabel
  };
  var courierOptions = courierNames.map(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(courierName => {
    return couriers == null ? void 0 : couriers[courierName].hasSelectedChild;
  });
  var selectedCourier = couriers == null ? void 0 : couriers[selectedCourierName];
  React.useEffect(() => {
    if (selectedCourierName) {
      pixelCheckoutStepOption(1, selectedCourierName);
    }
  }, [selectedCourierName]);
  var selectedDeliveryType = '';
  var deliveryOptions = (selectedCourier == null ? void 0 : selectedCourier.methods.map(method => {
    if (method.selected) selectedDeliveryType = method.methodKey;
    return {
      label: methodsMap[method.methodKey],
      onClick: method.onClick,
      isSelected: method.selected,
      key: method.methodKey
    };
  })) || [];
  var isPickupFromStore = selectedCourierName === ShippingModulesTypeEnum.PickUpFromStore;
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "col-12"
  }, /*#__PURE__*/React.createElement(CheckoutCourierInput, {
    options: courierOptions
  }), /*#__PURE__*/React.createElement(FormError, {
    errorRef: errorRef,
    errors: errors,
    fieldName: "courier"
  }), selectedCourierName && /*#__PURE__*/React.createElement(ScrollToOnRender, {
    key: selectedCourierName
  })), !isPickupFromStore && (deliveryOptions == null ? void 0 : deliveryOptions.length) > 1 && /*#__PURE__*/React.createElement("div", {
    className: "col-12"
  }, /*#__PURE__*/React.createElement(CheckoutTitle, {
    className: "my-4 pt-2",
    data: labels.deliveryType
  }), /*#__PURE__*/React.createElement(TabRadio, {
    tabStyle: "button",
    name: "adbook-deliveryType",
    options: deliveryOptions
  })), !isPickupFromStore && (deliveryOptions == null ? void 0 : deliveryOptions.length) < 2 && /*#__PURE__*/React.createElement("div", {
    className: "col-12"
  }, /*#__PURE__*/React.createElement(CheckoutTitle, {
    className: "mt-4 pt-2 mb-0 pb-0",
    data: labels.deliveryStepTitle
  })), isPickupFromStore && /*#__PURE__*/React.createElement(CheckoutPickupFromStore, null), !isPickupFromStore && selectedCourierName && /*#__PURE__*/React.createElement(CheckoutAddress, {
    selectedCourier: selectedCourierName,
    isOffice: selectedDeliveryType === 'Office'
  }), !selectedCourierName && /*#__PURE__*/React.createElement(NoCourierSelected, null, labels.noCourierSelectedLabel));
};
var NoCourierSelected = styled.div(_templateObject || (_templateObject = _taggedTemplateLiteralLoose(["\n  font-size: 1.6rem;\n"])));
//# sourceMappingURL=index.js.map