import { s__, n__, sprintf } from '~/locale';
import {
  ALL_ENVIRONMENTS_NAME,
  ROLLOUT_STRATEGY_ALL_USERS,
  ROLLOUT_STRATEGY_PERCENT_ROLLOUT,
  ROLLOUT_STRATEGY_USER_ID,
  ROLLOUT_STRATEGY_GITLAB_USER_LIST,
} from './constants';

const badgeTextByType = {
  [ROLLOUT_STRATEGY_ALL_USERS]: {
    name: s__('FeatureFlags|All Users'),
    parameters: null,
  },
  [ROLLOUT_STRATEGY_PERCENT_ROLLOUT]: {
    name: s__('FeatureFlags|Percent of users'),
    parameters: ({ parameters: { percentage } }) => `${percentage}%`,
  },
  [ROLLOUT_STRATEGY_USER_ID]: {
    name: s__('FeatureFlags|User IDs'),
    parameters: ({ parameters: { userIds } }) =>
      sprintf(n__('FeatureFlags|%d user', 'FeatureFlags|%d users', userIds.split(',').length)),
  },
  [ROLLOUT_STRATEGY_GITLAB_USER_LIST]: {
    name: s__('FeatureFlags|User List'),
    parameters: ({ user_list: { name } }) => name,
  },
};

const scopeName = ({ environment_scope: scope }) =>
  scope === ALL_ENVIRONMENTS_NAME ? s__('FeatureFlags|All Environments') : scope;

export default strategy => {
  const { name, parameters } = badgeTextByType[strategy.name];

  if (parameters) {
    return sprintf('%{name} - %{parameters}: %{scopes}', {
      name,
      parameters: parameters(strategy),
      scopes: strategy.scopes.map(scopeName).join(', '),
    });
  }

  return sprintf('%{name}: %{scopes}', {
    name,
    scopes: strategy.scopes.map(scopeName).join(', '),
  });
};
