/*
 * Copyright 2017 VMware, Inc. All rights reserved. VMware Confidential
 */
import { ExtensionEntityRegistration, ExtensionNavRegistration } from "../../models/extension";
import {
    ExtensionActions,
    ExtensionActionTypes,
    ExtensionEntityRegistrationAction,
    ExtensionNavRegistrationAction,
} from "./extensions.actions";

/**
 * Shape of the data structure that tracks registered extensions
 */
export interface AppExtensionState {
    navRegistrations: ExtensionNavRegistration[];
    entityRegistrations: ExtensionEntityRegistration[];
}

export const initialAppExtensionsState: AppExtensionState = {
    navRegistrations: [],
    entityRegistrations: [],
};

/**
 * Reducer that updates the registered extensions
 */
export function appExtensionsReducer(
    state: AppExtensionState = initialAppExtensionsState,
    action: ExtensionActions
): AppExtensionState {
    switch (action.type) {
        case ExtensionActionTypes.REGISTER_EXTENSION_NAV: {
            const registerNavAction = action as ExtensionNavRegistrationAction;
            return {
                navRegistrations: state.navRegistrations.concat(registerNavAction.payload),
                entityRegistrations: state.entityRegistrations,
            };
        }
        case ExtensionActionTypes.REGISTER_EXTENSION_ENTITY: {
            const registerEntityAction = action as ExtensionEntityRegistrationAction;
            return {
                navRegistrations: state.navRegistrations,
                entityRegistrations: state.entityRegistrations.concat(registerEntityAction.payload),
            };
        }
        default: {
            return state;
        }
    }
}
