'use client';
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useEffect } from 'react';
import { createPortal } from 'react-dom';
import { FeatureRowActions } from './feature-row-actions.js';
/**
 * Manages React portals for FeatureRowActions into Tabulator's action column cells.
 *
 * Discovers portal target containers by querying [data-feature-id] elements within
 * the table container, and renders a FeatureRowActions component into each one via
 * createPortal. Reconciles when the tableContainer reference changes (triggered by
 * Tabulator's renderComplete/tableBuilt events via onTableRender callback).
 */
export function FeatureRowActionsManager({ tableContainer, renderTick, features, inFlightIds, onStart, onStop, onRetry, onReview, onArchive, onUnarchive, onDelete, }) {
    const [portalContainers, setPortalContainers] = useState(new Map());
    // Re-discover portal targets whenever the container changes OR Tabulator re-renders (renderTick).
    // Tabulator destroys and recreates row DOM elements on tree expand/collapse, so the old
    // portal target divs become stale. renderTick is a monotonic counter incremented on every
    // Tabulator renderComplete/tableBuilt event to force re-discovery even when the container
    // element reference stays the same.
    useEffect(() => {
        if (!tableContainer) {
            setPortalContainers(new Map());
            return;
        }
        const elements = tableContainer.querySelectorAll('[data-feature-id]');
        const nextMap = new Map();
        elements.forEach((el) => {
            const featureId = el.getAttribute('data-feature-id');
            if (featureId) {
                nextMap.set(featureId, el);
            }
        });
        setPortalContainers((prev) => {
            if (prev.size !== nextMap.size)
                return nextMap;
            for (const [id, el] of nextMap) {
                if (prev.get(id) !== el)
                    return nextMap;
            }
            return prev;
        });
    }, [tableContainer, renderTick]);
    // Build a lookup map for feature data by ID
    const featureById = new Map();
    for (const f of features) {
        featureById.set(f.id, f);
    }
    // Also look through _children for grouped/tree data
    function collectFeatures(rows) {
        for (const row of rows) {
            if (!row._isGroupHeader && !row._isRepoGroup) {
                featureById.set(row.id, row);
            }
            if (row._children) {
                collectFeatures(row._children);
            }
        }
    }
    collectFeatures(features);
    const portals = [];
    for (const [featureId, container] of portalContainers) {
        const feature = featureById.get(featureId);
        if (!feature?.nodeState)
            continue;
        portals.push(createPortal(_jsx(FeatureRowActions, { featureId: featureId, featureName: feature.name, nodeState: feature.nodeState, hasChildren: feature.hasChildren ?? false, hasOpenPr: feature.hasOpenPr ?? false, isLoading: inFlightIds.has(featureId), onStart: onStart, onStop: onStop, onRetry: onRetry, onReview: onReview, onArchive: onArchive, onUnarchive: onUnarchive, onDelete: onDelete }), container, featureId));
    }
    if (portals.length === 0)
        return null;
    return (_jsxs(_Fragment, { children: [null, portals] }));
}
