<template>
	<MkSpacer :content-max="800">
		<div v-if="$i">
			<div v-if="state == 'waiting'" class="waiting _section">
				<div class="_content">
					<MkLoading />
				</div>
			</div>
			<div v-if="state == 'denied'" class="denied _section">
				<div class="_content">
					<p>{{ i18n.ts._auth.denied }}</p>
				</div>
			</div>
			<div v-else-if="state == 'accepted'" class="accepted _section">
				<div class="_content">
					<p v-if="callback">
						{{ i18n.ts._auth.callback }}<MkEllipsis />
					</p>
					<p v-else>{{ i18n.ts._auth.pleaseGoBack }}</p>
				</div>
			</div>
			<div v-else class="_section">
				<div v-if="name" class="_title">
					{{ i18n.t("_auth.shareAccess", { name: name }) }}
				</div>
				<div v-else class="_title">
					{{ i18n.ts._auth.shareAccessAsk }}
				</div>
				<div class="_content">
					<p>{{ i18n.ts._auth.permissionAsk }}</p>
					<div
						v-if="_permissions.length === 32"
						:class="[$style.permissions]"
					>
						<div
							:class="[$style.permission]"
							style="
								background-color: var(--error);
								color: var(--fgOnAccent);
							"
						>
							<i
								class="ph-shield-warning ph-bold ph-xl"
								style="margin-right: 0.5rem"
							></i>
							{{ i18n.ts._permissions.allPermissions }}
						</div>
					</div>
					<div v-else :class="[$style.permissions]">
						<div
							v-for="p in _permissions"
							:key="p"
							:class="[$style.permission]"
						>
							<i
								:class="[`ph-${getIcon(p)}`]"
								class="ph-bold ph-xl"
								style="margin-right: 0.5rem"
							></i>
							{{ i18n.t(`_permissions.${p}`) }}
						</div>
					</div>
				</div>
				<div class="_footer">
					<MkButton inline @click="deny">{{
						i18n.ts.cancel
					}}</MkButton>
					<MkButton inline primary @click="accept">{{
						i18n.ts.accept
					}}</MkButton>
				</div>
			</div>
		</div>
		<div v-else class="signin">
			<MkSignin @login="onLogin" />
		</div>
	</MkSpacer>
</template>

<script lang="ts" setup>
import { ref } from 'vue'

import {} from "vue";
import MkSignin from "@/components/MkSignin.vue";
import MkButton from "@/components/MkButton.vue";
import * as os from "@/os";
import { $i, login } from "@/account";
import { appendQuery, query } from "@/scripts/url";
import { i18n } from "@/i18n";

const props = defineProps<{
	session: string;
	callback?: string;
	name: string;
	icon: string;
	permission: string; // コンマ区切り
}>();

const _permissions = props.permission.split(",");

let state = ref<string | null>(null);

function getIcon(p: string) {
	return p.includes("write") ? "pencil-simple" : "eye";
}

async function accept(): Promise<void> {
	state.value = "waiting";
	await os.api("miauth/gen-token", {
		session: props.session,
		name: props.name,
		iconUrl: props.icon,
		permission: _permissions,
	});

	state.value = "accepted";
	if (props.callback) {
		const cbUrl = new URL(props.callback);
		if (
			["javascript:", "file:", "data:", "mailto:", "tel:"].includes(
				cbUrl.protocol,
			)
		)
			throw new Error("invalid url");
		location.href = appendQuery(
			props.callback,
			query({
				session: props.session,
			}),
		);
	}
}

function deny(): void {
	state.value = "denied";
}

function onLogin(res): void {
	login(res.i);
}
</script>

<style lang="scss" module>
.permissions {
	display: flex;
	flex-wrap: wrap;
	gap: 1rem;
	margin-bottom: 2rem;
}

.permission {
	display: inline-flex;
	padding: 0.5rem 1rem;
	border-radius: var(--radius);
	background-color: var(--buttonBg);
	color: var(--fg);
}
</style>
