import { LexoRank } from "lexorank";
import { useStatusesContext } from "@/app/deals/contexts/StatusesContext";
import { DealSchema } from "@/lib/client";
import { sortByLexorank } from "@/utils/lexorank/sort";
import { getNewLexorank } from "@/utils/lexorank/generation";

type NewRankGetters = {
    getNewRankForSameStatus: (
        statusDeals: DealSchema[],
        overDealIndex: number,
        activeDealId: number
    ) => string;
    getNewRankForAnotherStatus: (
        statusDeals: DealSchema[],
        overDealIndex: number
    ) => string;
    getNewStatusRank: (
        activeStatusId: number,
        overStatusId: number
    ) => string | null;
};

const useGetNewRank = (): NewRankGetters => {
    const { statuses } = useStatusesContext();

    const getNewRankForSameStatus = (
        statusDeals: DealSchema[],
        overDealIndex: number,
        activeDealId: number
    ): string => {
        const activeDealIndex = statusDeals.findIndex(
            deal => deal.id === activeDealId
        );
        const [leftIndex, rightIndex] =
            overDealIndex < activeDealIndex
                ? [overDealIndex - 1, overDealIndex]
                : [overDealIndex, overDealIndex + 1];

        const leftLexorank =
            leftIndex >= 0
                ? LexoRank.parse(statusDeals[leftIndex].lexorank)
                : null;
        const rightLexorank =
            rightIndex < statusDeals.length
                ? LexoRank.parse(statusDeals[rightIndex].lexorank)
                : null;

        return getNewLexorank(leftLexorank, rightLexorank).toString();
    };

    const getNewRankForAnotherStatus = (
        statusDeals: DealSchema[],
        overDealIndex: number
    ): string => {
        const leftLexorank =
            overDealIndex > 0
                ? LexoRank.parse(statusDeals[overDealIndex - 1].lexorank)
                : null;
        const rightLexorank = LexoRank.parse(
            statusDeals[overDealIndex].lexorank
        );

        return getNewLexorank(leftLexorank, rightLexorank).toString();
    };

    const getNewStatusRank = (
        activeStatusId: number,
        overStatusId: number
    ): string | null => {
        const sortedStatusList = sortByLexorank(statuses);
        const overIndex = sortedStatusList.findIndex(
            s => s.id === overStatusId
        );
        const activeIndex = sortedStatusList.findIndex(
            s => s.id === activeStatusId
        );

        if (overIndex === -1 || activeIndex === -1) return null;

        const [leftIndex, rightIndex] =
            overIndex < activeIndex
                ? [overIndex - 1, overIndex]
                : [overIndex, overIndex + 1];

        const leftLexorank =
            leftIndex >= 0
                ? LexoRank.parse(statuses[leftIndex].lexorank)
                : null;
        const rightLexorank =
            rightIndex < statuses.length
                ? LexoRank.parse(statuses[rightIndex].lexorank)
                : null;

        return getNewLexorank(leftLexorank, rightLexorank).toString();
    };

    return {
        getNewRankForSameStatus,
        getNewRankForAnotherStatus,
        getNewStatusRank,
    };
};

export default useGetNewRank;
