import { match } from 'path-to-regexp';
import { redirect } from 'vike/abort';

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

export default (pageContext) => {
	const matched = urlMatch(pageContext.urlPathname);
	const isSelf = pageContext.user?.username === matched.params?.username;

	if (matched) {
		if (!isSelf && matched.params.section !== 'stashes') {
			throw redirect(`/user/${matched.params.username}/stashes`);
		}

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

	return false;
};
