<!--
SPDX-FileCopyrightText: syuilo and other misskey contributors
SPDX-License-Identifier: AGPL-3.0-only
-->

<template>
<XColumn :menu="menu" :column="column" :isStacked="isStacked">
	<template #header>
		<i v-if="column.tl === 'home'" class="ph-house ph-bold ph-lg"></i>
		<i v-else-if="column.tl === 'local'" class="ph-planet ph-bold pg-lg"></i>
		<i v-else-if="column.tl === 'social'" class="ph-rocket-launch ph-bold pg-lg"></i>
		<i v-else-if="column.tl === 'global'" class="ph-globe-hemisphere-west ph-bold ph-lg"></i>
		<span style="margin-left: 8px;">{{ column.name }}</span>
	</template>

	<div v-if="(((column.tl === 'local' || column.tl === 'social') && !isLocalTimelineAvailable) || (column.tl === 'global' && !isGlobalTimelineAvailable))" :class="$style.disabled">
		<p :class="$style.disabledTitle">
			<i class="ph-minus-circle ph-bold ph-lg"></i>
			{{ i18n.ts._disabledTimeline.title }}
		</p>
		<p :class="$style.disabledDescription">{{ i18n.ts._disabledTimeline.description }}</p>
	</div>
	<MkTimeline
		v-else-if="column.tl"
		ref="timeline"
		:key="column.tl + withRenotes + withReplies + onlyFiles"
		:src="column.tl"
		:withRenotes="withRenotes"
		:withReplies="withReplies"
		:onlyFiles="onlyFiles"
	/>
</XColumn>
</template>

<script lang="ts" setup>
import { onMounted, watch } from 'vue';
import XColumn from './column.vue';
import { removeColumn, updateColumn, Column } from './deck-store.js';
import MkTimeline from '@/components/MkTimeline.vue';
import * as os from '@/os.js';
import { $i } from '@/account.js';
import { i18n } from '@/i18n.js';
import { instance } from '@/instance.js';

const props = defineProps<{
	column: Column;
	isStacked: boolean;
}>();

let disabled = $ref(false);

const isLocalTimelineAvailable = (($i == null && instance.policies.ltlAvailable) || ($i != null && $i.policies.ltlAvailable));
const isGlobalTimelineAvailable = (($i == null && instance.policies.gtlAvailable) || ($i != null && $i.policies.gtlAvailable));
const withRenotes = $ref(props.column.withRenotes ?? true);
const withReplies = $ref(props.column.withReplies ?? false);
const onlyFiles = $ref(props.column.onlyFiles ?? false);

watch($$(withRenotes), v => {
	updateColumn(props.column.id, {
		withRenotes: v,
	});
});

watch($$(onlyFiles), v => {
	updateColumn(props.column.id, {
		onlyFiles: v,
	});
});

onMounted(() => {
	if (props.column.tl == null) {
		setType();
	} else if ($i) {
		disabled = (
			(!((instance.policies.ltlAvailable) || ($i.policies.ltlAvailable)) && ['local', 'social'].includes(props.column.tl)) ||
			(!((instance.policies.gtlAvailable) || ($i.policies.gtlAvailable)) && ['global'].includes(props.column.tl)));
	}
});

async function setType() {
	const { canceled, result: src } = await os.select({
		title: i18n.ts.timeline,
		items: [{
			value: 'home' as const, text: i18n.ts._timelines.home,
		}, {
			value: 'local' as const, text: i18n.ts._timelines.local,
		}, {
			value: 'social' as const, text: i18n.ts._timelines.social,
		}, {
			value: 'global' as const, text: i18n.ts._timelines.global,
		}],
	});
	if (canceled) {
		if (props.column.tl == null) {
			removeColumn(props.column.id);
		}
		return;
	}
	updateColumn(props.column.id, {
		tl: src,
	});
}

const menu = [{
	icon: 'ph-pencil ph-bold ph-lg',
	text: i18n.ts.timeline,
	action: setType,
}, {
	type: 'switch',
	text: i18n.ts.showRenotes,
	ref: $$(withRenotes),
}, props.column.tl === 'local' || props.column.tl === 'social' ? {
	type: 'switch',
	text: i18n.ts.showRepliesToOthersInTimeline,
	ref: $$(withReplies),
} : undefined, {
	type: 'switch',
	text: i18n.ts.fileAttachedOnly,
	ref: $$(onlyFiles),
}];
</script>

<style lang="scss" module>
.disabled {
	text-align: center;
}

.disabledTitle {
	margin: 16px;
}

.disabledDescription {
	font-size: 90%;
}
</style>
