"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAnimatedGraphLayout = void 0;
var react_1 = require("react");
var core_1 = require("@react-sigma/core");
var animate_1 = require("sigma/utils/animate");
var WORKER_LAYOUT_ANIMATION_DURATION = 5000;
var SYNC_LAYOUT_ANIMATION_DURATION = 1000;
var useAnimatedGraphLayout = function (_a) {
    var graph = _a.graph, syncLayout = _a.syncLayout, workerLayout = _a.workerLayout, layoutSettings = _a.layoutSettings, _b = _a.initialCameraRatio, initialCameraRatio = _b === void 0 ? 1.2 : _b, animationDuration = _a.animationDuration, relayoutTrigger = _a.relayoutTrigger;
    var sigma = (0, core_1.useSigma)();
    var loadGraph = (0, core_1.useLoadGraph)();
    (0, react_1.useEffect)(function () {
        sigma.getCamera().setState({ ratio: initialCameraRatio });
    }, [graph, sigma, initialCameraRatio]);
    (0, react_1.useEffect)(function () {
        loadGraph(graph);
        if (syncLayout) {
            var positions = syncLayout(sigma.getGraph(), layoutSettings);
            (0, animate_1.animateNodes)(sigma.getGraph(), positions, { duration: animationDuration || SYNC_LAYOUT_ANIMATION_DURATION });
        }
        if (workerLayout) {
            var worker_1 = new workerLayout(sigma.getGraph(), layoutSettings);
            worker_1.start();
            var timer_1 = setTimeout(function () {
                worker_1.stop();
            }, animationDuration || WORKER_LAYOUT_ANIMATION_DURATION);
            return function () {
                clearTimeout(timer_1);
                worker_1.kill();
            };
        }
    }, [graph, layoutSettings, loadGraph, sigma, syncLayout, workerLayout, animationDuration, relayoutTrigger]);
};
exports.useAnimatedGraphLayout = useAnimatedGraphLayout;
