"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ThemeReducer = exports.ThemeDelete = exports.ThemeAdd = exports.ThemeEdit = exports.ThemeReady = exports.ThemeSelect = exports.ThemeSetUserThemes = exports.ThemeSetSystemThemes = exports.THEME_DELETE = exports.THEME_ADD = exports.THEME_EDIT = exports.THEME_READY = exports.THEME_SELECT = exports.THEME_SET_USER_THEMES = exports.THEME_SET_SYSTEM_THEMES = void 0;
const GeneralConstants_1 = require("../../Utilities/Constants/GeneralConstants");
const ReduxConstants_1 = require("../../Utilities/Constants/ReduxConstants");
/**
 * @ReduxAction System Themes have been set
 */
exports.THEME_SET_SYSTEM_THEMES = 'THEME_SET_SYSTEM_THEMES';
/**
 * @ReduxAction User Themes have been set
 */
exports.THEME_SET_USER_THEMES = 'THEME_SET_USER_THEMES';
/**
 * @ReduxAction A theme has been selected
 */
exports.THEME_SELECT = 'THEME_SELECT';
/**
 * @ReduxAction Theme Module is ready
 */
exports.THEME_READY = 'THEME_READY';
/**
 * @ReduxAction Theme Module is edited
 */
exports.THEME_EDIT = 'THEME_EDIT';
/**
 * @ReduxAction A new theme is added
 */
exports.THEME_ADD = 'THEME_ADD';
/**
 * @ReduxAction A theme is deleted
 */
exports.THEME_DELETE = 'THEME_DELETE';
const ThemeSetSystemThemes = (SystemThemes) => ({
    type: exports.THEME_SET_SYSTEM_THEMES,
    SystemThemes,
});
exports.ThemeSetSystemThemes = ThemeSetSystemThemes;
const ThemeSetUserThemes = (UserThemes) => ({
    type: exports.THEME_SET_USER_THEMES,
    UserThemes,
});
exports.ThemeSetUserThemes = ThemeSetUserThemes;
const ThemeSelect = (Theme) => ({
    type: exports.THEME_SELECT,
    Theme,
});
exports.ThemeSelect = ThemeSelect;
const ThemeReady = (themeState) => ({
    type: exports.THEME_READY,
    themeState,
});
exports.ThemeReady = ThemeReady;
const ThemeEdit = (theme) => ({
    type: exports.THEME_EDIT,
    theme,
});
exports.ThemeEdit = ThemeEdit;
const ThemeAdd = (theme) => ({
    type: exports.THEME_ADD,
    theme,
});
exports.ThemeAdd = ThemeAdd;
const ThemeDelete = (theme) => ({
    type: exports.THEME_DELETE,
    theme,
});
exports.ThemeDelete = ThemeDelete;
const initialState = {
    CurrentTheme: ReduxConstants_1.THEME_DEFAULT_CURRENT_THEME,
    SystemThemes: ReduxConstants_1.SYSTEM_THEMES,
    UserThemes: GeneralConstants_1.EMPTY_ARRAY,
};
const ThemeReducer = (state = initialState, action) => {
    switch (action.type) {
        case exports.THEME_SET_SYSTEM_THEMES:
            return Object.assign({}, state, {
                SystemThemes: action.SystemThemes,
            });
        case exports.THEME_SET_USER_THEMES:
            return Object.assign({}, state, {
                UserThemes: action.UserThemes,
            });
        case exports.THEME_SELECT:
            return Object.assign({}, state, {
                CurrentTheme: action.Theme,
            });
        case exports.THEME_EDIT:
            let newThemes = null;
            // find by UUID, or by name
            const themeToBeReplaced = action.theme;
            let replaceIndex = state.UserThemes.findIndex((theme) => themeToBeReplaced.Uuid === theme.Uuid);
            // try by name
            if (replaceIndex === -1) {
                replaceIndex = state.UserThemes.findIndex((theme) => themeToBeReplaced.Name === theme.Name);
            }
            // fond one, either by name or by uuid
            if (replaceIndex !== -1) {
                newThemes = [...state.UserThemes];
                newThemes[replaceIndex] = themeToBeReplaced;
            }
            if (newThemes === null) {
                return state;
            }
            // if name changed, need to update the current name
            let currentTheme = state.CurrentTheme;
            if (currentTheme !== themeToBeReplaced.Name) {
                // need to update the current theme if it name has changed
                currentTheme = themeToBeReplaced.Name;
            }
            return Object.assign({}, state, {
                CurrentTheme: currentTheme,
                UserThemes: newThemes,
            });
        case exports.THEME_ADD:
            return Object.assign({}, state, {
                UserThemes: [...state.UserThemes, action.theme],
            });
        case exports.THEME_DELETE:
            return Object.assign({}, state, {
                UserThemes: state.UserThemes.filter((theme) => theme.Uuid !== action.theme.Uuid),
            });
        default:
            return state;
    }
};
exports.ThemeReducer = ThemeReducer;
