import {
	SET_ORIGINAL_MENUS,
	SET_ORIGINAL_MENU,
	SET_EDITOR_MENU,
	SET_ITEM,
	SET_MENU_FORM_DATA,
	SET_TOTAL_ITEMS,
	SET_CURRENT_TYPE,
} from "./constants";

import { MenuActionsCreators } from "./interfaces";
import { IMenuItem, IMenuForm, IMenu } from "@ax/types";

export interface IMenuState {
	savedMenus: IMenu[] | null;
	savedMenu: IMenu | null;
	editorMenu: IMenu | null;
	item: IMenuItem | null;
	form: IMenuForm;
	totalItems: number;
	currentType: string;
}

const initialState = {
	savedMenus: [],
	savedMenu: null,
	editorMenu: null,
	item: null,
	form: {
		url: null,
		label: "",
		type: "link",
		headerStyle: "",
		footerStyle: "",
	},
	totalItems: 0,
	currentType: "",
};

function reducer(state = initialState, action: MenuActionsCreators): IMenuState {
	switch (action.type) {
		case SET_ORIGINAL_MENUS:
		case SET_ORIGINAL_MENU:
		case SET_EDITOR_MENU:
		case SET_ITEM:
		case SET_MENU_FORM_DATA:
		case SET_TOTAL_ITEMS:
		case SET_CURRENT_TYPE:
			return { ...state, ...action.payload };
		default:
			return state;
	}
}

export { initialState as menuInitialState, reducer as menuReducer };
