import { INumberPredictionItemOption } from '@livelike/javascript'; import { createStore } from './store'; export type INumberPredictionState = { numberOptions: INumberPredictionItemOption[]; }; export type NumberPredictionStoreValue = Record; const initialNumberPredictionStoreValue: NumberPredictionStoreValue = {}; export const numberPredictionWidgetStore = createStore( initialNumberPredictionStoreValue ); export type IUpdateNumberPredictionOptionActionArg = { widgetId: string; numberOption: INumberPredictionItemOption; optionIndex: number; }; export const numberPredictionWidgetStoreActions = { updateNumberPredictionOptionAction({ widgetId, numberOption, optionIndex, }: IUpdateNumberPredictionOptionActionArg) { const numberPredictionState = numberPredictionWidgetStore.get()[widgetId]; if (!numberPredictionState) { return numberPredictionWidgetStore.set({ ...numberPredictionWidgetStore.get(), [widgetId]: { numberOptions: [numberOption], }, }); } const newNumberOptions = [...numberPredictionState.numberOptions]; newNumberOptions.splice(optionIndex, 1, numberOption); numberPredictionWidgetStore.set({ ...numberPredictionWidgetStore.get(), [widgetId]: { ...numberPredictionState, numberOptions: newNumberOptions, }, }); }, };