import { create } from '@gravityforms/react-utils';

const defaultInitialState = {
	activeItem: {},
	hide: false,
	listItems: {},
	open: false,
	reveal: false,
	selectedItem: {},
	triggerRef: null,
	popoverRef: null,
	listRef: null,
	searchRef: null,
	baseElRef: null,
	pillsRef: null,
	searchValue: '',
	chars: '',
	charTimeoutId: 0,
	triggerHeight: 0,
	initialTriggerHeight: 0,
};

const storeActions = ( set ) => ( {
	setActiveItem: ( activeItem ) => set( { activeItem } ),
	setHide: ( hide ) => set( { hide } ),
	setListItems: ( listItems ) => set( { listItems } ),
	setOpen: ( open ) => set( { open } ),
	setReveal: ( reveal ) => set( { reveal } ),
	setSelectedItem: ( selectedItem ) => set( { selectedItem } ),
	setTriggerRef: ( triggerRef ) => set( { triggerRef } ),
	setPopoverRef: ( popoverRef ) => set( { popoverRef } ),
	setListRef: ( listRef ) => set( { listRef } ),
	setSearchRef: ( searchRef ) => set( { searchRef } ),
	setBaseElRef: ( baseElRef ) => set( { baseElRef } ),
	setPillsRef: ( pillsRef ) => set( { pillsRef } ),
	setSearchValue: ( searchValue ) => set( { searchValue } ),
	setChars: ( chars ) => set( { chars } ),
	setCharTimeoutId: ( charTimeoutId ) => set( { charTimeoutId } ),
	setTriggerHeight: ( triggerHeight ) => set( { triggerHeight } ),
	setInitialTriggerHeight: ( initialTriggerHeight ) => set( { initialTriggerHeight } ),
} );

const createStore = ( initialState = {} ) => create(
	{
		...defaultInitialState,
		...initialState,
	},
	storeActions,
);

export default createStore;
