<template>
	<div class="filter-bar noselect">
		<span class="sort">
			<router-link
				:to="{ params: { range: 'latest', pageNumber: 1 } }"
				:class="{ active: $route.name === 'latest' || range === 'latest' }"
				class="range range-button"
			>Latest</router-link>

			<router-link
				:to="{ params: { range: 'upcoming', pageNumber: 1 } }"
				:class="{ active: $route.name === 'upcoming' || range === 'upcoming' }"
				class="range-button"
			>Upcoming</router-link>

			<router-link
				:to="{ params: { range: 'new', pageNumber: 1 } }"
				:class="{ active: $route.name === 'new' || range === 'new' }"
				class="range-button"
			>New</router-link>
		</span>

		<Filters
			class="filters"
			:filter="filter"
			:available-tags="availableTags"
			@set-filter="setFilter"
		/>
	</div>
</template>

<script>
import { mapState } from 'vuex';
import Filters from './filters.vue';

function filter(state) {
	return state.ui.filter;
}

function range() {
	return this.$route.params.range;
}

function batch(state) {
	return state.ui.batch;
}

async function setFilter(newFilter) {
	this.$store.dispatch('setFilter', newFilter);

	await this.fetchReleases();
}

async function setRange(newRange) {
	this.$store.dispatch('setRange', newRange);

	await this.fetchReleases();
}

async function setBatch(newBatch) {
	this.$store.dispatch('setBatch', newBatch);

	await this.fetchReleases();
}

export default {
	components: {
		Filters,
	},
	props: {
		fetchReleases: {
			type: Function,
			default: null,
		},
		isHome: {
			type: Boolean,
			default: false,
		},
		itemsTotal: {
			type: Number,
			default: 0,
		},
		itemsPerPage: {
			type: Number,
			default: 10,
		},
		availableTags: {
			type: Array,
			default: () => [],
		},
	},
	computed: {
		...mapState({
			filter,
			range,
			batch,
		}),
	},
	methods: {
		setFilter,
		setRange,
		setBatch,
	},
};
</script>

<style lang="scss" scoped>
@import 'theme';

.filter-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: .5rem 1rem 0 1rem;
    z-index: 1;
	background: var(--background-dim);
    font-size: 0;

    .icon {
        margin: 0 .5rem 0 0;
        fill: var(--shadow);
    }
}

.sort {
    display: flex;
    align-items: center;
}

.range-button {
    color: var(--shadow);
    display: inline-block;
    padding: .8rem 1rem;
    border: none;
	position: relative;
    font-size: .8rem;
    font-weight: bold;
    text-decoration: none;
	border: solid 1px transparent;
	border-bottom: none;

    &:hover:not(.active) {
        color: var(--shadow-strong);
        cursor: pointer;
    }

    &.active {
        color: var(--primary);
		background: var(--background-soft);
		border-color: var(--crease);

		&::after {
			/* hide grey border for tab effect, negative margin shows grey crease on mobile */
			content: '';
			width: 100%;
			height: 2px;
			background: var(--background-soft);
			position: absolute;
			left: 0;
			bottom: -1px;
		}
    }
}

.filters {
    display: inline-block;
	flex-shrink: 0;
}
</style>
