{"size": 1492, "ext": "js", "max_stars_repo_path": "assets/store.js", "max_stars_repo_name": "alessandrotesoro/wp-reactive-options", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "assets/store.js", "max_issues_repo_name": "alessandrotesoro/wp-reactive-options", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "assets/store.js", "max_forks_repo_name": "alessandrotesoro/wp-reactive-options", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.1621621622, "max_line_length": 71, "alphanum_fraction": 0.6608579088}
/**
 * External dependencies
 */
import {
	Store,
	useStoreState,
} from "pullstate";

/**
 * Internal dependencies
 */
import { getFirstTab, getTabSections, getTab } from "./utils";

/**
 * Options page store
 */
export const OptionsPageStore = new Store({
	active_tab: getFirstTab(),
	sections: getTabSections(getFirstTab()),
	page_title: getTab(getFirstTab())?.name,
	notice: {
		type: 'success',
		message: '',
	}
});

/**
 * React Hook that helps reading and managing the options page store.
 *
 * @returns {object}
 */
export const useOptionsPage = () => {
	const active_tab = useStoreState(OptionsPageStore, s => s.active_tab);
	const sections = useStoreState(OptionsPageStore, s => s.sections);
	const page_title = useStoreState(OptionsPageStore, s => s.page_title);
	const notice = useStoreState(OptionsPageStore, s => s.notice);

	/**
	 * Activate a specific tab given it's slug (name).
	 *
	 * @param {string} tab
	 */
	function activateTab(tab) {
		const tabDetails = getTab(tab)
		OptionsPageStore.update(s => {
			s.active_tab = tab,
			s.sections = getTabSections(tab),
			s.page_title = tabDetails?.name
		});
	}

	const setNotice = ( type, message ) => {
		OptionsPageStore.update(s => {
			s.notice = { type: type, message: message }
		});
	}

	const deleteNotice = () => {
		OptionsPageStore.update(s => {
			s.notice = { type: 'success', message: '' }
		});
	}

	return {
		active_tab,
		sections,
		page_title,
		notice,
		activateTab,
		setNotice,
		deleteNotice,
	};
}
