"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Collect = void 0;
const core_1 = require("@flyde/core");
const common_1 = require("../../Timing/common");
function getDisplayName(config) {
    if (config.strategy === "time") {
        if (config.time.mode === "static") {
            return `Collect over ${(0, common_1.timeToString)(config.time.value)}`;
        }
        else {
            return "Collect over time";
        }
    }
    else if (config.strategy === "count") {
        if (config.count.mode === "static") {
            return `Collect ${config.count.value} values`;
        }
        else {
            return "Collect values";
        }
    }
    else {
        return "Collect on trigger";
    }
}
function getDescription(config) {
    if (config.strategy === "time") {
        if (config.time.mode === "static") {
            return `Emits a list of all values received, from the first value and until ${(0, common_1.timeToString)(config.time.value)} pass.`;
        }
        else {
            return "Emits a list of all values received, from the first value and until the specified amount of time passes.";
        }
    }
    else if (config.strategy === "count") {
        if (config.count.mode === "static") {
            return `Collect ${config.count.value} values and emit a list of them.`;
        }
        else {
            return "Collect a specified amount of values and emit a list of them.";
        }
    }
    else {
        return "Emits a list of all values received up until any value is received to the 'trigger' input.";
    }
}
exports.Collect = {
    id: "Collect",
    namespace: "Lists",
    mode: "advanced",
    defaultStyle: {
        icon: "bucket",
    },
    menuDisplayName: "Collect",
    menuDescription: "Collects values into a list. Over time, count, or trigger.",
    displayName: (config) => getDisplayName(config),
    description: (config) => getDescription(config),
    defaultConfig: { strategy: "count", count: { mode: "static", value: 3 } },
    inputs: (config) => {
        const inputs = {
            value: { description: "Value to collect" },
        };
        if (config.strategy === "time" && config.time.mode === "dynamic") {
            inputs["time"] = { description: "Time in milliseconds" };
        }
        else if (config.strategy === "count" && config.count.mode === "dynamic") {
            inputs["count"] = { description: "Number of values to collect" };
        }
        else if (config.strategy === "trigger") {
            inputs["trigger"] = {
                mode: "optional",
                description: "Emit a value here to output a list out of collected values",
            };
        }
        return inputs;
    },
    reactiveInputs: (config) => [
        "value",
        ...(config.strategy === "trigger" ? ["trigger"] : []),
    ],
    outputs: () => ({
        list: { description: "Collected values" },
    }),
    completionOutputs: () => ["list"],
    run: async (inputs, outputs, adv) => {
        const { value, time, count, trigger } = inputs;
        const config = adv.context.config;
        const list = adv.state.get("list") || [];
        if ((0, core_1.isDefined)(value)) {
            list.push(value);
            adv.state.set("list", list);
        }
        if (config.strategy === "time") {
            const timeValue = config.time.mode === "dynamic" ? time : config.time.value;
            const timer = adv.state.get("timer");
            if (!timer) {
                const newTimer = setTimeout(() => {
                    outputs.list.next(list);
                    adv.state.set("list", []);
                }, timeValue);
                adv.state.set("timer", newTimer);
            }
        }
        else if (config.strategy === "count") {
            const countValue = config.count.mode === "dynamic" ? count : config.count.value;
            if (list.length >= countValue) {
                outputs.list.next(list);
                adv.state.set("list", []);
            }
        }
        else if (config.strategy === "trigger") {
            if ((0, core_1.isDefined)(trigger)) {
                outputs.list.next(list);
                adv.state.set("list", []);
            }
        }
    },
    editorConfig: {
        type: "custom",
        editorComponentBundlePath: "../../../dist/ui/Collect.js",
    },
};
