import { useState } from "react";
import { IQueryValue } from "@ax/types";

const useSortedListStatus = () => {
	const sortedInitialState: {
		isAscending: boolean;
		sortedByTitle: boolean;
		sortedByURL: boolean;
		sortedByDate: boolean;
	} = {
		isAscending: false,
		sortedByTitle: false,
		sortedByURL: false,
		sortedByDate: false,
	};

	const [sortedListStatus, setSortedListStatus] = useState(sortedInitialState);

	return {
		sortedListStatus,
		setSortedListStatus,
	};
};

const useFilterQuery = (values?: Record<string, IQueryValue[]> | null) => {
	const initialQueryValues: Record<string, IQueryValue[]> = {
		type: [{ value: "all", label: "All" }],
		translated: [{ value: "all", label: "All" }],
		liveStatus: [{ value: "all", label: "All" }],
		order: [],
		categories: [{ value: "all", label: "All" }],
	};

	const getFilterQuery = (filterValues: Record<string, IQueryValue[]>): string => {
		const { type, translated, liveStatus, order, categories } = filterValues;
		let filterQuery = "";

		const currentQuery = (pointer: string, values: IQueryValue[]): string => {
			const stringValues =
				Array.isArray(values) && values.length
					? values.map((value) => (value.value !== "all" ? value.value : "")).join(",")
					: "";

			return !stringValues.length
				? filterQuery
				: filterQuery.length
					? filterQuery.concat(`&${pointer}=${stringValues}`)
					: `&${pointer}=${stringValues}`;
		};

		if (type) {
			filterQuery = currentQuery("type", type);
		}

		if (translated) {
			filterQuery = currentQuery("translated", translated);
		}

		if (liveStatus) {
			filterQuery = currentQuery("liveStatus", liveStatus);
		}

		if (order) {
			filterQuery = currentQuery("order", order);
		}

		if (categories) {
			filterQuery = currentQuery("categories", categories);
		}

		return filterQuery;
	};

	const initState = values ? values : initialQueryValues;
	const [state, setState] = useState(initState);

	const initQuery = values ? getFilterQuery(values) : "";
	const [query, setQuery] = useState(initQuery);

	const setFiltersSelection = (pointer: string, filter: IQueryValue[], isAscendent?: boolean) => {
		const { type, translated, liveStatus, order, categories } = state;
		const orderMethod = isAscendent ? "asc" : "desc";
		const filterValues: Record<string, IQueryValue[]> = {
			type: pointer === "type" ? filter : type,
			translated: pointer === "translated" ? filter : translated,
			liveStatus: pointer === "liveStatus" ? filter : liveStatus,
			order: pointer === "order" ? [{ value: `${filter[0].value}-${orderMethod}`, label: filter[0].label }] : order,
			categories: pointer === "categories" ? filter : categories,
		};

		setState(filterValues);
		const filterQuery = getFilterQuery(filterValues);
		setQuery(filterQuery);
	};

	const resetFilterQuery = () => {
		setState(initialQueryValues);
		setQuery("");
	};

	return {
		setFiltersSelection,
		resetFilterQuery,
		filterValues: state,
		query,
	};
};

export { useSortedListStatus, useFilterQuery };
