export function sortObjectKeys(source) {
	return Object.keys(source)
		.sort()
		.reduce((acc, key) => {
			acc[key] = source[key]
			return acc
		}, {})
}

export function buildQueryString(record) {
	const segments = []
	for (const key in record) {
		if (!Object.prototype.hasOwnProperty.call(record, key)) continue
		const encodedKey = encodeURIComponent(key)
		const rawValue = record[key]
		const encodedValue = encodedKey === 'scene' ? rawValue : encodeURIComponent(rawValue)
		segments.push(`${encodedKey}=${encodedValue}`)
	}
	return segments.join('&')
}

export function parseQueryString(urlOrQuery) {
	const result = {}
	const queryPart = urlOrQuery.includes('?')
		? urlOrQuery.split('?')[1]
		: urlOrQuery
	const pairs = queryPart.split('&').filter(Boolean)
	for (const pair of pairs) {
		const [k, v = ''] = pair.split('=')
		if (k) result[k] = v
	}
	return result
}
