"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecretSelector = SecretSelector;
const react_1 = __importStar(require("react"));
const ui_1 = require("@flyde/ui");
function SecretSelector({ value, onChange, ports, typeEditorData, }) {
    const [secrets, setSecrets] = (0, react_1.useState)([]);
    const [isAddingSecret, setIsAddingSecret] = (0, react_1.useState)(false);
    const [newSecretKey, setNewSecretKey] = (0, react_1.useState)("");
    const [newSecretValue, setNewSecretValue] = (0, react_1.useState)("");
    const [isLoading, setIsLoading] = (0, react_1.useState)(true);
    (0, react_1.useEffect)(() => {
        const fetchSecrets = async () => {
            setIsLoading(true);
            try {
                const availableSecrets = await ports.getAvailableSecrets();
                setSecrets(availableSecrets);
            }
            catch (error) {
                console.error("Failed to fetch secrets:", error);
            }
            finally {
                setIsLoading(false);
            }
        };
        fetchSecrets();
    }, [ports]);
    const handleAddSecret = async () => {
        if (!newSecretKey || !newSecretValue) {
            return;
        }
        try {
            const updatedSecrets = await ports.addNewSecret({
                key: newSecretKey,
                value: newSecretValue,
            });
            setSecrets(updatedSecrets);
            onChange(newSecretKey);
            setIsAddingSecret(false);
            setNewSecretKey("");
            setNewSecretValue("");
        }
        catch (error) {
            console.error("Failed to add secret:", error);
        }
    };
    const startAddingSecret = () => {
        setIsAddingSecret(true);
        if (typeEditorData === null || typeEditorData === void 0 ? void 0 : typeEditorData.defaultName) {
            setNewSecretKey(typeEditorData.defaultName);
        }
    };
    const handleValueChange = (newValue) => {
        if (newValue === "__add_new_secret") {
            startAddingSecret();
        }
        else {
            onChange(newValue);
        }
    };
    if (isAddingSecret) {
        return (react_1.default.createElement("div", { className: "flex flex-col gap-2" },
            react_1.default.createElement("div", { className: "flex gap-2" },
                react_1.default.createElement(ui_1.Input, { placeholder: "Secret key", value: newSecretKey, size: 12, onChange: (e) => setNewSecretKey(e.target.value) }),
                react_1.default.createElement(ui_1.Input, { placeholder: "Secret value", type: "password", size: 12, value: newSecretValue, onChange: (e) => setNewSecretValue(e.target.value) })),
            react_1.default.createElement("div", { className: "flex gap-2 justify-end" },
                react_1.default.createElement(ui_1.Button, { variant: "ghost", size: "sm", onClick: () => setIsAddingSecret(false) }, "Cancel"),
                react_1.default.createElement(ui_1.Button, { variant: "default", size: "sm", onClick: handleAddSecret }, "Add"))));
    }
    if (!isLoading && secrets.length === 0) {
        return (react_1.default.createElement("div", { className: "flex flex-col gap-2 p-2 border rounded-md bg-gray-50 dark:bg-gray-900" },
            react_1.default.createElement("p", { className: "text-sm text-gray-600 dark:text-gray-400" }, "No secrets available."),
            react_1.default.createElement(ui_1.Button, { variant: "outline", size: "sm", onClick: startAddingSecret }, "Create a new secret")));
    }
    return (react_1.default.createElement(ui_1.Select, { value: value, onValueChange: handleValueChange },
        react_1.default.createElement(ui_1.SelectTrigger, { className: "w-full" },
            react_1.default.createElement(ui_1.SelectValue, { placeholder: "Select or create a secret" })),
        react_1.default.createElement(ui_1.SelectContent, null, secrets.length > 0 ? (react_1.default.createElement(react_1.default.Fragment, null,
            secrets.map((secret) => (react_1.default.createElement(ui_1.SelectItem, { key: secret, value: secret }, secret))),
            react_1.default.createElement(ui_1.SelectItem, { value: "__add_new_secret" }, "+ Add new secret"))) : (react_1.default.createElement(ui_1.SelectItem, { value: "__add_new_secret" }, "+ Create your first secret")))));
}
