import { jsx } from 'react/jsx-runtime';
import { toDays, toHours, toMinutes, toSeconds } from 'duration-fns';
import { getLangFromLocale } from '@transferwise/components';
import 'react';
import { createIntlCache, createIntl } from 'react-intl';
import { c as commonMessages } from './index-2f4810ab.js';

const hasDuration = deliveryEstimation => !!(deliveryEstimation && deliveryEstimation.duration && (deliveryEstimation.duration.min || deliveryEstimation.duration.max));
function isDeliveryEstimationCollectedForAllProviders(quotes) {
  return quotes.filter(quote => quote.deliveryEstimation.providerGivesEstimate && !hasDuration(quote.deliveryEstimation)).length === 0;
}

// This is optional but highly recommended
// since it prevents memory leak
const cache = createIntlCache();
var translate = ((translationKey, locale, parameters = {}) => {
  const lang = getLangFromLocale(locale) === 'zh' ? locale : getLangFromLocale(locale);
  const intl = createIntl({
    locale: lang,
    messages: {
      ...commonMessages.en,
      ...commonMessages[lang]
    } || commonMessages.en
  }, cache);
  return translationKey && intl.formatMessage({
    id: translationKey
  }, parameters);
});

const DAYS = 'days';
const HOURS = 'hours';
const MINUTES = 'minutes';
const SECONDS = 'seconds';
const PREFIX_WITHIN = 'within';
const PREFIX_IN = 'in';

function getTranslationKey(unit, isBusiness, isRange, prefix) {
    const prefixValue = `${prefix ? `${prefix}-` : ''}`;
    const isBusinessValue = `${isBusiness ? 'business-' : ''}`;
    const isRangeValue = `${isRange ? '-range' : ''}`;
    if (unit === SECONDS) {
        return `${prefix && prefix === PREFIX_IN ? `${prefixValue}${unit}` : unit}`;
    }
    if (unit === MINUTES) {
        return `${prefixValue}${unit}${isRangeValue}`;
    }
    if (unit === HOURS) {
        return `${prefixValue}${unit}${isRangeValue}`;
    }
    if (unit === DAYS) {
        return `${prefixValue}${isBusinessValue}${unit}${isRangeValue}`;
    }
    return 'unavailable';
}

function convertDurationToMaximumUnits(duration) {
    const numberDays = toDays(duration);
    const numberHours = toHours(duration);
    if (numberDays >= 1 || numberHours > 23) {
        return { total: Math.ceil(numberDays), units: DAYS };
    }
    const numberMinutes = toMinutes(duration);
    if (numberHours >= 1 || numberMinutes > 59) {
        return { total: Math.ceil(numberHours), units: HOURS };
    }
    const numberSeconds = toSeconds(duration);
    if (numberMinutes >= 1 || numberSeconds > 59) {
        return { total: Math.ceil(numberMinutes), units: MINUTES };
    }
    return { total: Math.ceil(numberSeconds), units: SECONDS };
}
function hasAbsoluteDeliveryEstimate(deliveryEstimation) {
    const duration = deliveryEstimation.duration || {};
    if (duration.min && duration.max) {
        const min = convertDurationToMaximumUnits(duration.min);
        const max = convertDurationToMaximumUnits(duration.max);
        if (min.units === max.units && min.total === max.total) {
            return true;
        }
    }
    return false;
}
function getTranslation(durationString, isBusiness, isRange, prefix) {
    const durationObject = convertDurationToMaximumUnits(durationString);
    const translationKey = getTranslationKey(durationObject.units, isBusiness, isRange, prefix);
    return { key: translationKey, params: { [durationObject.units]: durationObject.total } };
}
function getTranslationForRange(duration, isBusiness, prefix) {
    const min = convertDurationToMaximumUnits(duration.min);
    const max = convertDurationToMaximumUnits(duration.max);
    if (max.units === min.units) {
        return {
            key: getTranslationKey(max.units, isBusiness, true, prefix),
            params: { min: min.total, max: max.total },
        };
    }
    return getTranslation(duration.max, isBusiness, false, PREFIX_WITHIN);
}
const formatDeliveryEstimation = (deliveryEstimation, { lang = 'en', translationOptions = {}, }, { showAsBusiness = false, includePrefix = true, isSpeedFormat = false } = {}) => {
    let translation;
    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
    const isBusiness = showAsBusiness &&
        (!hasDuration(deliveryEstimation) || (deliveryEstimation === null || deliveryEstimation === void 0 ? void 0 : deliveryEstimation.durationType) === 'BUSINESS');
    if (!(deliveryEstimation === null || deliveryEstimation === void 0 ? void 0 : deliveryEstimation.providerGivesEstimate)) {
        translation = {
            key: isSpeedFormat ? 'unavailable' : 'delivery-estimation.provider-does-not-give-estimate',
            params: {},
        };
    }
    else if (!hasDuration(deliveryEstimation)) {
        translation = { key: 'delivery-estimation.not-yet-collected', params: {} };
    }
    else if (hasAbsoluteDeliveryEstimate(deliveryEstimation)) {
        translation = getTranslation(deliveryEstimation.duration.min, isBusiness, false, includePrefix ? PREFIX_IN : undefined);
    }
    else if (deliveryEstimation.duration.min && deliveryEstimation.duration.max) {
        translation = getTranslationForRange(deliveryEstimation.duration, isBusiness, includePrefix ? PREFIX_IN : undefined);
    }
    else if (deliveryEstimation.duration.max) {
        translation = getTranslation(deliveryEstimation.duration.max, isBusiness, false, PREFIX_WITHIN);
        // eslint-disable-next-line sonarjs/no-duplicated-branches
    }
    else {
        translation = getTranslation(deliveryEstimation.duration.min, isBusiness, false, includePrefix ? PREFIX_IN : undefined);
    }
    if (translationOptions.keySuffix) {
        translation = Object.assign(Object.assign({}, translation), { key: `${translation.key}.${translationOptions.keySuffix}` });
    }
    const translationParams = translationOptions.params || {};
    return translate(translation.key, lang, Object.assign(Object.assign(Object.assign({}, translation.params), translationParams), { span: (chunks) => (jsx("span", Object.assign({ className: translationParams.estimationClassNames }, { children: chunks }))) }));
};

export { DAYS as D, HOURS as H, MINUTES as M, SECONDS as S, convertDurationToMaximumUnits as c, formatDeliveryEstimation as f, hasDuration as h, isDeliveryEstimationCollectedForAllProviders as i, translate as t };
//# sourceMappingURL=deliveryEstimationFormatter-9fc5c3af.js.map
