import { redirect } from 'vike/abort'; /* eslint-disable-line import/extensions */
import { match } from 'path-to-regexp';

const path = '/user/:username/:section?/:domain?';
const urlMatch = match(path, { decode: decodeURIComponent });

export default (pageContext) => {
	const matched = urlMatch(pageContext.urlPathname);

	if (matched) {
		if (![undefined, 'stashes'].includes(matched.params.domain) && pageContext.user?.username !== matched.params.username) {
			throw redirect(`/user/${matched.params.username}`);
		}

		return {
			routeParams: {
				username: matched.params.username,
				section: matched.params.section || 'stashes',
				domain: matched.params.domain || 'scenes',
			},
		};
	}

	return false;
};