import { produce } from "immer";
import { SmartDesktopDesignerActions } from "../actions/smartDesktopDesignerActions";
import { ApplicationSetupType, AuthenticationType, } from "../types/smartDesktopDesignerTypes";
export const initialState = {
    askAboutConvertingAppD: true,
    isGettingStarted: false,
    previousViewId: null,
    currentViewId: null,
    apps: [],
    newAppId: null,
    currentAppId: null,
    currentAppSetupType: null,
    currentPreload: null,
    menus: {},
    preloads: {},
    appdServers: [],
    projectInfo: {
        applicationRoot: "",
        name: "",
        user: {
            firstName: "",
            lastName: "",
            company: "",
            email: "",
        },
        export: {
            author: "",
            companyName: "",
            description: "",
            toolbarIcon: "",
            installerIcon: "",
            taskbarIcon: "",
            systemTrayIcon: "",
            splashScreenImage: "",
        },
    },
    export: {
        uuid: null, // `${new Date().getTime()}-${Math.round(Math.random() * Math.pow(10, 10))}`,
        isPublishing: false,
        publishError: null,
        deployInfo: null,
    },
    authentication: {
        providers: [
            {
                name: "Google",
                type: AuthenticationType.OAUTH2,
                client_id: null,
                secret: null,
                enabled: false,
                redirect_uri: "",
                authorization_endpoint: "https://accounts.google.com/o/oauth2/v2/auth",
                scope: "openid",
                nonce: false,
            },
            {
                name: "Salesforce",
                type: AuthenticationType.OAUTH2,
                client_id: null,
                secret: null,
                enabled: false,
                redirect_uri: "",
                authorization_endpoint: "https://login.salesforce.com/services/oauth2/authorize",
                nonce: false,
            },
            {
                name: "Custom",
                type: AuthenticationType.OAUTH2,
                client_id: null,
                secret: null,
                enabled: false,
                authorization_endpoint: "",
                redirect_uri: "",
                scope: "openid",
                nonce: false,
            },
        ],
    },
    errors: [],
    isReviewingErrors: false,
    fsblVersion: "0.0.0",
};
export const smartDesktopDesignerReducer = produce((state, action) => {
    SmartDesktopDesignerActions.match(action, {
        change_conversion_ask_state: (newStatus) => {
            state.askAboutConvertingAppD = newStatus;
        },
        change_view: (payload) => {
            state.isGettingStarted = false;
            state.previousViewId = state.currentViewId;
            state.currentViewId = payload;
            state.currentAppId = null;
            state.currentAppSetupType = null;
        },
        update_apps: (payload) => {
            state.apps = payload;
        },
        new_app: (appId) => {
            state.currentAppId = state.newAppId = appId;
        },
        set_current_app_id: (currentAppId) => {
            if (currentAppId !== null) {
                let currentAppConfig = state.apps.find((app) => app.appId === currentAppId);
                if (!currentAppConfig || currentAppId === state.newAppId) {
                    state.currentAppSetupType = null;
                }
                else {
                    state.currentAppSetupType = ApplicationSetupType.APP_EDIT_ADVANCED;
                }
            }
            else {
                state.currentAppSetupType = null;
            }
            state.currentAppId = currentAppId;
        },
        update_app: (appConfig) => {
            if (!appConfig || !appConfig.appId) {
                return;
            }
            state.apps = state.apps.filter((app) => app.appId !== appConfig.appId);
            state.apps.push(appConfig);
        },
        update_menus: (menus) => {
            state.menus = menus;
        },
        delete_app: (appId) => {
            state.apps = state.apps.filter((app) => app.appId !== appId);
        },
        set_is_publishing: (isPublishing) => {
            state.export.isPublishing = isPublishing;
        },
        set_deploy_info: (value) => {
            state.export.deployInfo = value;
        },
        set_publish_error: (value) => {
            state.export.publishError = value;
        },
        set_project_info: (value) => {
            state.projectInfo = value;
        },
        set_fsbl_version: (value) => {
            state.fsblVersion = value;
        },
        set_is_getting_started: (value) => {
            state.isGettingStarted = value;
        },
        update_auth_provider: (value) => {
            const index = state.authentication.providers.findIndex((provider) => provider.name === value.name);
            if (index == -1) {
                state.authentication.providers.push(value);
            }
            else {
                state.authentication.providers[index] = value;
            }
        },
        enable_auth_provider: (value) => {
            state.authentication.providers.forEach((provider) => {
                provider.enabled = provider.name === value;
            });
        },
        set_project_error: (value) => {
            state.errors = state.errors.filter((error) => error.viewId !== value.viewId);
            state.errors.push(value);
        },
        delete_project_error: (value) => {
            state.errors = state.errors.filter((error) => error.viewId !== value.viewId);
        },
        set_reviewing_project_errors: (value) => {
            state.isReviewingErrors = value;
        },
        update_preloads: (preloads) => {
            state.preloads = preloads;
        },
        set_current_preload: (preload) => {
            state.currentPreload = preload;
        },
        add_appd_server: (server) => {
            if (!server || !server.url)
                return;
            state.appdServers.push(server);
        },
        update_appd_servers: (value) => {
            state.appdServers = value;
        },
        delete_appd_server: (url) => {
            state.appdServers = state.appdServers.filter((server) => server.url !== url);
        },
        default: () => state,
    });
}, initialState);
//# sourceMappingURL=smartDesktopDesignerReducer.js.map