'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { useSearchParams } from 'next/navigation.js';
import * as qs from 'qs-esm';
import React, { createContext, use, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { parseSearchParams } from '../../utilities/parseSearchParams.js';
import { useAuth } from '../Auth/index.js';
import { useListQuery } from '../ListQuery/index.js';
import { useLocale } from '../Locale/index.js';
export var SelectAllStatus = /*#__PURE__*/ function(SelectAllStatus) {
    SelectAllStatus["AllAvailable"] = "allAvailable";
    SelectAllStatus["AllInPage"] = "allInPage";
    SelectAllStatus["None"] = "none";
    SelectAllStatus["Some"] = "some";
    return SelectAllStatus;
}({});
/**
   * Selects all rows on the current page within the current query.
   * If `allAvailable` is true, does not select specific IDs so that the query itself affects all rows across all pages.
   */ const Context = /*#__PURE__*/ createContext({
    count: undefined,
    getQueryParams: (additionalParams)=>'',
    getSelectedIds: ()=>[],
    selectAll: undefined,
    selected: new Map(),
    selectedIDs: [],
    setSelection: (id)=>{},
    toggleAll: (toggleAll)=>{},
    totalDocs: undefined
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const reduceActiveSelections = (selected)=>{
    const ids = [];
    for (const [key, value] of selected){
        if (value) {
            ids.push(key);
        }
    }
    return ids;
};
export const SelectionProvider = ({ children, docs = [], totalDocs })=>{
    const contextRef = useRef({});
    const { user } = useAuth();
    const { code: locale } = useLocale();
    const [selected, setSelected] = useState(()=>{
        const rows = new Map();
        docs.forEach(({ id })=>{
            rows.set(id, false);
        });
        return rows;
    });
    const [selectAll, setSelectAll] = useState("none");
    const [count, setCount] = useState(0);
    const searchParams = useSearchParams();
    const { query } = useListQuery();
    const toggleAll = useCallback((allAvailable = false)=>{
        const rows = new Map();
        if (allAvailable) {
            setSelectAll("allAvailable");
            docs.forEach(({ id, _isLocked, _userEditing })=>{
                if (!_isLocked || _userEditing?.id === user?.id) {
                    rows.set(id, true);
                }
            });
        } else if (// Reset back to `None` if we previously had any type of selection
        selectAll === "allAvailable" || selectAll === "allInPage") {
            setSelectAll("none");
        } else {
            const shouldSelect = selectAll !== "some";
            docs.forEach(({ id, _isLocked, _userEditing })=>{
                if (!_isLocked || _userEditing?.id === user?.id) {
                    rows.set(id, shouldSelect);
                }
            });
            // Set selectAll synchronously to avoid race conditions with useEffect
            if (shouldSelect && rows.size > 0) {
                if (rows.size === docs.length) {
                    setSelectAll("allInPage");
                } else {
                    setSelectAll("some");
                }
            } else {
                setSelectAll("none");
            }
        }
        setSelected(rows);
    }, [
        docs,
        selectAll,
        user?.id
    ]);
    const setSelection = useCallback((id)=>{
        const doc = docs.find((doc)=>doc.id === id);
        if (doc?._isLocked && user?.id !== doc?._userEditing.id) {
            return;
        }
        // Prevent selection if the document is locked
        const existingValue = selected.get(id);
        const isSelected = typeof existingValue === 'boolean' ? !existingValue : true;
        const newMap = new Map(selected.set(id, isSelected));
        // If previously selected all and now deselecting, adjust status
        if (selectAll === "allAvailable" && !isSelected) {
            setSelectAll("some");
        }
        setSelected(newMap);
    }, [
        selected,
        docs,
        selectAll,
        user?.id
    ]);
    const getQueryParams = useCallback((additionalWhereParams)=>{
        let where;
        if (selectAll === "allAvailable") {
            const params = parseSearchParams(searchParams)?.where;
            where = params || {
                id: {
                    not_equals: ''
                }
            };
        } else {
            const ids = [];
            for (const [key, value] of selected){
                if (value) {
                    ids.push(key);
                }
            }
            where = {
                id: {
                    in: ids
                }
            };
        }
        if (additionalWhereParams) {
            where = {
                and: [
                    {
                        ...additionalWhereParams
                    },
                    where
                ]
            };
        }
        return qs.stringify({
            locale,
            where
        }, {
            addQueryPrefix: true
        });
    }, [
        selectAll,
        selected,
        locale,
        searchParams
    ]);
    const getSelectedIds = useCallback(()=>reduceActiveSelections(selected), [
        selected
    ]);
    useEffect(()=>{
        if (selectAll === "allAvailable") {
            return;
        }
        let some = false;
        let all = true;
        if (!selected.size) {
            all = false;
            some = false;
        } else {
            for (const [_, value] of selected){
                all = all && value;
                some = some || value;
            }
        }
        if (all && selected.size === docs.length) {
            setSelectAll("allInPage");
        } else if (some) {
            setSelectAll("some");
        } else {
            setSelectAll("none");
        }
    }, [
        selectAll,
        selected,
        totalDocs,
        docs
    ]);
    useEffect(()=>{
        let newCount = 0;
        if (selectAll === "allAvailable") {
            newCount = totalDocs;
        } else {
            for (const [_, value] of selected){
                if (value) {
                    newCount++;
                }
            }
        }
        setCount(newCount);
    }, [
        selectAll,
        selected,
        totalDocs
    ]);
    useEffect(()=>{
        setSelectAll("none");
        setSelected(new Map());
    }, [
        query
    ]);
    const selectedIDs = useMemo(()=>reduceActiveSelections(selected), [
        selected
    ]);
    contextRef.current = {
        count,
        getQueryParams,
        getSelectedIds,
        selectAll,
        selected,
        selectedIDs,
        setSelection,
        toggleAll,
        totalDocs
    };
    return /*#__PURE__*/ _jsxDEV(Context, {
        value: contextRef.current,
        children: children
    }, void 0, false, {
        fileName: "src/providers/Selection/index.tsx",
        lineNumber: 264,
        columnNumber: 10
    }, this);
};
export const useSelection = ()=>use(Context);

//# sourceMappingURL=index.js.map