<template>
	<div class="filter tags-container">
		<div class="filters-sort">
			<input
				v-model="search"
				type="search"
				placeholder="Filter tags"
				class="input input-inline filters-search"
			>

			<div
				v-show="order === 'priority'"
				class="filter-sort order noselect"
				@click="order = 'name'"
			>
				<Icon
					icon="star"
				/>
			</div>

			<div
				v-show="order === 'name'"
				class="filter-sort order noselect"
				@click="order = 'count'"
			>
				<Icon
					icon="sort-alpha-asc"
				/>
			</div>

			<div
				v-show="order === 'count'"
				class="filter-sort order noselect"
				@click="order = 'priority'"
			>
				<Icon
					icon="sort-numeric-desc"
				/>
			</div>
		</div>

		<ul
			v-for="(group, groupKey) in groupedtags"
			:key="groupKey"
			class="filter-items nolist"
			:class="groupKey"
		>
			<li
				v-for="(tag, index) in group"
				:key="`filter-tag-${tag.id}`"
				class="filter-item"
				:class="{ selected: filters.tags.includes(tag.slug), first: groupKey === 'available' && index === 0 && filters.tags.length > 0 }"
				@click="emit('update', 'tags', [tag.slug])"
			>
				<div
					class="filter-include"
					@click.stop="toggleTag(tag)"
				>
					<Icon
						icon="checkmark"
						class="filter-add"
					/>

					<Icon
						icon="cross2"
						class="filter-remove"
					/>
				</div>

				<span class="filter-name tag-name">{{ tag.name }}</span>

				<span class="tag-details">
					<span
						v-if="tag.count"
						class="filter-count"
					>{{ tag.count }}</span>
				</span>
			</li>
		</ul>
	</div>
</template>

<script setup>
import { ref, computed, inject } from 'vue';

const props = defineProps({
	filters: {
		type: Object,
		default: null,
	},
	tags: {
		type: Array,
		default: () => [],
	},
});

const emit = defineEmits(['update']);

const search = ref('');
const searchRegexp = computed(() => new RegExp(search.value, 'i'));
const order = ref('priority');

const { pageProps } = inject('pageContext');
const { tag: pageTag } = pageProps;

const priorityTags = [
	'anal',
	'gangbang',
	'blowbang',
	'transsexual',
	'airtight',
	'dp',
	'dap',
	'dvp',
	'triple-penetration',
	'tap',
	'tvp',
	'mfm',
	'fmf',
	'threesome',
	'bdsm',
	'deepthroat',
	'blowjob',
	'lesbian',
];

const groupedtags = computed(() => {
	const selected = props.tags.filter((tag) => props.filters.tags.includes(tag.slug));
	const filtered = props.tags.filter((tag) => !props.filters.tags.includes(tag.slug)
		&& tag.id !== pageTag?.id
		&& searchRegexp.value.test(tag.name));

	if (order.value === 'count') {
		const available = filtered.sort((tagA, tagB) => {
			if (order.value === 'count') {
				return tagB.count - tagA.count;
			}

			return 0;
		});

		return { selected, available };
	}

	if (order.value === 'priority') {
		return {
			selected,
			available: [
				...priorityTags.map((priorityTag) => filtered.find((tag) => tag.slug === priorityTag)).filter(Boolean),
				...filtered.filter((tag) => !priorityTags.includes(tag.slug)),
			],
		};
	}

	return {
		selected,
		available: filtered,
	};
});

function toggleTag(tag) {
	if (props.filters.tags.includes(tag.slug)) {
		emit('update', 'tags', props.filters.tags.filter((tagId) => tagId !== tag.slug));
		return;
	}

	emit('update', 'tags', props.filters.tags.concat(tag.slug));
}
</script>

<style scoped>
.filter-items {
	max-height: 15rem;
	overflow-y: auto;
}

.filter {
	padding: 0;
}
</style>
