"use strict";

exports.__esModule = true;
exports.createRtfFormatter = void 0;
const shortExpressions = {
  en: {
    s: '1s',
    ss: '%ds',
    m: '1m',
    mm: '%dm',
    h: '1h',
    hh: '%dh',
    d: '1d',
    dd: '%dd'
  },
  nl: {
    s: '1sec.',
    ss: '%dsec.',
    m: '1min.',
    mm: '%dmin.',
    h: '1u.',
    hh: '%du.'
  },
  cs: {
    s: '1sek',
    ss: '%dsek',
    m: '1min',
    mm: '%dmin',
    h: '1hod',
    hh: '%dhod'
  },
  da: {
    s: '1s',
    ss: '%ds',
    m: '1m',
    mm: '%dm',
    h: '1t',
    hh: '%dt'
  },
  de: {
    s: '1Sek.',
    ss: '%dSek.',
    m: '1Min.',
    mm: '%dMin.',
    h: '1Std.',
    hh: '%dStd.'
  },
  fr: {
    s: '1s',
    ss: '%ds',
    m: '1min',
    mm: '%dmin',
    h: '1h',
    hh: '%dh'
  },
  hi: {
    s: '1से',
    ss: '%dसे',
    m: '1मि',
    mm: '%dमि',
    h: '1घंटा',
    hh: '%dघंटा'
  },
  it: {
    s: '1sec',
    ss: '%dsec',
    m: '1min',
    mm: '%dmin',
    h: '1ora',
    hh: '%dore'
  },
  ja: {
    s: '1秒',
    ss: '%d秒',
    m: '1分',
    mm: '%d分',
    h: '1時間',
    hh: '%d時間'
  },
  ko: {
    s: '1초',
    ss: '%d초',
    m: '1분',
    mm: '%d분',
    h: '1시간',
    hh: '%d시간'
  },
  lt: {
    s: '1s',
    ss: '%ds',
    m: '1min',
    mm: '%dmin',
    h: '1val',
    hh: '%dval'
  },
  nb: {
    s: '1sek',
    ss: '%dsek',
    m: '1min',
    mm: '%dmin',
    h: '1time',
    hh: '%dtimer'
  },
  pl: {
    s: '1s',
    ss: '%ds',
    m: '1min',
    mm: '%dmin',
    h: '1h',
    hh: '%dh'
  },
  pt: {
    s: '1s',
    ss: '%ds',
    m: '1min',
    mm: '%dmin',
    h: '1h',
    hh: '%dh'
  },
  es: {
    s: '1seg',
    ss: '%dseg',
    m: '1min',
    mm: '%dmin',
    h: '1h',
    hh: '%dhs'
  },
  sv: {
    s: '1s',
    ss: '%ds',
    m: '1min',
    mm: '%dmin',
    h: '1h',
    hh: '%dh'
  },
  th: {
    s: '1วินาที',
    ss: '%dวินาที',
    m: '1นาที',
    mm: '%dนาที',
    h: '1ชั่วโมง',
    hh: '%dชั่วโมง'
  },
  tr: {
    s: '1sn.',
    ss: '%dsn.',
    m: '1dk.',
    mm: '%ddk.',
    h: '1sa.',
    hh: '%dsa.'
  },
  ru: {
    s: '1сек.',
    ss: '%dсек.',
    m: '1dмин.',
    mm: '%dмин.',
    h: '1ч.',
    hh: '%dч.'
  },
  'zh-cn': {
    s: '1秒',
    ss: '%d秒',
    m: '1d分鐘',
    mm: '%d分鐘',
    h: '1小時',
    hh: '%d小時'
  },
  uk: {
    s: '1с.',
    ss: '%dс.',
    m: '1dхв.',
    mm: '%dхв.',
    h: '1г.',
    hh: '%dг.'
  }
};
var TIME_UNIT = /*#__PURE__*/function (TIME_UNIT) {
  TIME_UNIT["SECONDS"] = "second";
  TIME_UNIT["MINUTES"] = "minute";
  TIME_UNIT["HOURS"] = "hour";
  TIME_UNIT["DAYS"] = "day";
  return TIME_UNIT;
}(TIME_UNIT || {});
const applyShortenedExpression = (language, timeUnit, value) => {
  const placeholder = '%d';
  const unitToShorthand = {
    [TIME_UNIT.SECONDS]: 'ss',
    [TIME_UNIT.MINUTES]: 'mm',
    [TIME_UNIT.HOURS]: 'hh',
    [TIME_UNIT.DAYS]: 'dd'
  };
  const shorthand = value === -1 ? unitToShorthand[timeUnit][0] : unitToShorthand[timeUnit];
  const expression = shortExpressions[language] ? shortExpressions[language][shorthand] : null;
  if (expression) {
    return expression.replace(placeholder, `${-value}`);
  }
  return null;
};
const getTimeUnit = time => {
  const timeAdjusted = time < 0 ? -time : time;
  switch (true) {
    case timeAdjusted < 1000 * 60:
      return TIME_UNIT.SECONDS;
    case timeAdjusted < 1000 * 60 * 60:
      return TIME_UNIT.MINUTES;
    case timeAdjusted < 1000 * 60 * 60 * 24:
      return TIME_UNIT.HOURS;
    default:
      return TIME_UNIT.DAYS;
  }
};
const getRelativeTime = (time, unit, floorSeconds) => {
  let returnTime;
  switch (unit) {
    case TIME_UNIT.SECONDS:
      if (floorSeconds) {
        returnTime = 1;
      } else {
        returnTime = time / 1000;
      }
      break;
    case TIME_UNIT.MINUTES:
      returnTime = time / 1000 / 60;
      break;
    case TIME_UNIT.HOURS:
      returnTime = time / 1000 / 60 / 60;
      break;
    case TIME_UNIT.DAYS:
      returnTime = time / 1000 / 60 / 60 / 24;
      break;
    default:
      returnTime = time;
  }
  return -Math.round(returnTime);
};
const isLibAvailable = () => typeof Intl.RelativeTimeFormat !== 'undefined';
const createRtfFormatter = (language, {
  floorSeconds = false,
  extraShortExpression = false
} = {}, options = {}) => {
  let rtf;
  return time => {
    if (!rtf && isLibAvailable()) {
      rtf = new Intl.RelativeTimeFormat(language, {
        ...options
      });
    }
    let timeDiff = new Date().getTime() - new Date(time).getTime();
    if (isNaN(timeDiff)) {
      console.error(`[@wix/wix-i18n-config/relative-time-formatter] received invalid time: ${time}`);
      timeDiff = 1000;
    }
    const unit = getTimeUnit(timeDiff);
    const relativeTime = getRelativeTime(timeDiff, unit, floorSeconds);
    const shortExpression = extraShortExpression || !rtf ? applyShortenedExpression(language, unit, relativeTime) : null;
    if (shortExpression) {
      return shortExpression;
    }
    if (rtf) {
      return rtf.format(relativeTime, unit);
    }
    return time;
  };
};
exports.createRtfFormatter = createRtfFormatter;
//# sourceMappingURL=relative-time-formatting.js.map