Загрузка данных


import { getLocale, t } from '@src/translations';

export type TimeframeUnit = 'tick' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month';

type UnitForms = {
  one: string;
  few?: string;
  many?: string;
  other: string;
};

const pluralRulesCache = new Map<string, Intl.PluralRules>();

const unitForms: Record<TimeframeUnit, UnitForms> = {
  tick: {
    one: 'tick',
    few: 'tick_few',
    many: 'tick_many',
    other: 'ticks',
  },
  second: {
    one: 'second',
    few: 'second_few',
    many: 'second_many',
    other: 'seconds',
  },
  minute: {
    one: 'minute',
    few: 'minute_few',
    many: 'minute_many',
    other: 'minutes',
  },
  hour: {
    one: 'hour',
    few: 'hour_few',
    many: 'hour_many',
    other: 'hours',
  },
  day: {
    one: 'day',
    few: 'day_few',
    many: 'day_many',
    other: 'days',
  },
  week: {
    one: 'week',
    few: 'week_few',
    many: 'week_many',
    other: 'weeks',
  },
  month: {
    one: 'month',
    few: 'month_few',
    many: 'month_many',
    other: 'months',
  },
};

const getPluralRules = (): Intl.PluralRules => {
  const locale = getLocale();
  const cachedRules = pluralRulesCache.get(locale);

  if (cachedRules) {
    return cachedRules;
  }

  const rules = new Intl.PluralRules(locale);

  pluralRulesCache.set(locale, rules);

  return rules;
};

const getUnitTranslationKey = (value: number, unit: TimeframeUnit): string => {
  const form = getPluralRules().select(value);
  const forms = unitForms[unit];

  return forms[form as keyof UnitForms] ?? forms.other;
};

export const formatTimeframeLabel = (value: number, unit: TimeframeUnit): string => {
  return `${value} ${t(getUnitTranslationKey(value, unit))}`;
};



tick_few: 'тика',
tick_many: 'тиков',

second_few: 'секунды',
second_many: 'секунд',

minute_few: 'минуты',
minute_many: 'минут',

hour_few: 'часа',
hour_many: 'часов',

day_few: 'дня',
day_many: 'дней',

week_few: 'недели',
week_many: 'недель',

month_few: 'месяца',
month_many: 'месяцев',



import { formatTimeframeLabel, TimeframeUnit } from '@src/utils/formatTimeframeLabel';
import { Timeframes } from '@src/types/timeframes';

const timeframeOption = (value: Timeframes, amount: number, unit: TimeframeUnit) => ({
  value,
  label: formatTimeframeLabel(amount, unit),
});

export const timeframes = () => {
  return {
    ticks: [
      timeframeOption(Timeframes['1t'], 1, 'tick'),
      timeframeOption(Timeframes['10t'], 10, 'tick'),
      timeframeOption(Timeframes['100t'], 100, 'tick'),
      timeframeOption(Timeframes['1000t'], 1000, 'tick'),
    ],
    seconds: [
      timeframeOption(Timeframes['1s'], 1, 'second'),
      timeframeOption(Timeframes['5s'], 5, 'second'),
      timeframeOption(Timeframes['10s'], 10, 'second'),
      timeframeOption(Timeframes['15s'], 15, 'second'),
      timeframeOption(Timeframes['30s'], 30, 'second'),
      timeframeOption(Timeframes['45s'], 45, 'second'),
    ],
    minutes: [
      timeframeOption(Timeframes['1m'], 1, 'minute'),
      timeframeOption(Timeframes['2m'], 2, 'minute'),
      timeframeOption(Timeframes['3m'], 3, 'minute'),
      timeframeOption(Timeframes['5m'], 5, 'minute'),
      timeframeOption(Timeframes['10m'], 10, 'minute'),
      timeframeOption(Timeframes['15m'], 15, 'minute'),
      timeframeOption(Timeframes['30m'], 30, 'minute'),
      timeframeOption(Timeframes['45m'], 45, 'minute'),
    ],
    hours: [
      timeframeOption(Timeframes['1h'], 1, 'hour'),
      timeframeOption(Timeframes['2h'], 2, 'hour'),
      timeframeOption(Timeframes['3h'], 3, 'hour'),
      timeframeOption(Timeframes['4h'], 4, 'hour'),
      timeframeOption(Timeframes['8h'], 8, 'hour'),
      timeframeOption(Timeframes['12h'], 12, 'hour'),
    ],
    days: [
      timeframeOption(Timeframes['1d'], 1, 'day'),
      timeframeOption(Timeframes['3d'], 3, 'day'),
      timeframeOption(Timeframes['5d'], 5, 'day'),
    ],
    weeks: [timeframeOption(Timeframes['1w'], 1, 'week')],
    months: [
      timeframeOption(Timeframes['1М'], 1, 'month'),
      timeframeOption(Timeframes['3М'], 3, 'month'),
    ],
    Range: [],
  };
};

export const TIMEFRAMES = timeframes();