import { LAST_RESPONSE_LIFE_TIME } from '../../constants/constants';
import Locator from '../../services/Locator';
import { getLatestSpinKey, getLatestSpinTimeKey } from '../../utils/utils';
import OutcomesView from '../helpers/OutcomesView';
import { BalancePolicy } from './BalancePolicy';
import { FeatureMultiplierCalculator } from './FeatureMultiplierCalculator';
import { GameDataFactory } from './GameDataFactory';
import { ModelBootstrap } from './ModelBootstrap';
import { ModesFactory } from './ModesFactory';
import { OutcomeRestorer } from './OutcomeRestorer';

let singleton: ModelBootstrap | null = null;

export function buildBootstrap(): ModelBootstrap
{
    if (singleton) return singleton;

    singleton = new ModelBootstrap({
        outcomeRestorer: new OutcomeRestorer({
            storage: { get: (k) => Locator.storage.get(k) },
            maxAgeMs: LAST_RESPONSE_LIFE_TIME,
            getSpinKey: getLatestSpinKey,
            getSpinTimeKey: getLatestSpinTimeKey,
            now: () => Date.now(),
        }),
        multiplierCalc: new FeatureMultiplierCalculator(),
        gameDataFactory: new GameDataFactory(),
        modesFactory: new ModesFactory(),
        balancePolicy: new BalancePolicy(),
        OutcomesViewCtor: OutcomesView,
    });

    return singleton;
}
