import { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.shared-runtime'

export function getParam(key: string) {
	const url = new URL(window.location.href)
	return url.searchParams.get(key)
}

export function getParams() {
	const url = new URL(window.location.href)
	const params: Record<string, any> = {
		length: url.searchParams.size
	}
	url.searchParams.forEach((value, key) => {
		params[key] = value
	})
	return params
}

export function addParam(router: AppRouterInstance, key: string, value: any) {
	const url = new URL(window.location.href)
	url.searchParams.set(key, value)
	router.push(url.toString())
}

export function removeParam(router: AppRouterInstance, key: string) {
	const url = new URL(window.location.href)
	url.searchParams.delete(key)
	router.push(url.toString())
}
