import Locator from '../services/Locator';
import { capitalizeFirstLetter } from '../utils/utils';

import type { SlotModel } from './types';

/** View model that exposes localized paytable descriptions per symbol. */
export default class PaytableViewModel
{
    private readonly model: SlotModel;

    constructor(model: SlotModel)
    {
        this.model = model;
        for (const key of model.paytable.symbols)
        {
            Object.defineProperty(this as Record<string, unknown>, key, {
                get: () => this.getPaytableText(key, this.model.bet)
            });
        }
    }

    get isRoundStarted(): boolean
    {
        return this.model.isRoundStarted();
    }

    getPaytableText(key: string, bet: number): string
    {
        if (this.model.paytable.scatterSymbols.includes(key))
        {
            return capitalizeFirstLetter(Locator.locales.get('scatter'));
        }

        if (this.model.paytable.wildSymbols.includes(key))
        {
            return capitalizeFirstLetter(Locator.locales.get('wild'));
        }

        const multipliers = this.model.paytable.get(key);
        const paytableText = multipliers.reduce((result: string[], multiplier, index) =>
        {
            const calculatedValue = (multiplier * bet) / this.model.paytable.betDivider;

            if (calculatedValue !== 0)
            {
                result.push(`${index + 1}: ${this.model.currency.getFormattedValue(calculatedValue)}`);
            }

            return result;
        }, [] as string[]);

        return paytableText.join('\n');
    }
}
