/**
 * Helper function to flatten fieldsets into a flat array of input fields.
 * Used to extract all input fields from nested fieldset structure for field selection.
 */
function flattenRootFieldset(rootFieldset) {
    const result = [];
    function processItem(item) {
        if (item.type === "input_field") {
            result.push(item);
        }
        else if (item.type === "fieldset") {
            // Recursively process fields in the fieldset
            for (const field of item.fields) {
                processItem(field);
            }
        }
        // Skip info fields as they're not input fields
    }
    for (const item of rootFieldset) {
        processItem(item);
    }
    return result;
}
export const inputFieldKeyResolver = {
    type: "dynamic",
    depends: ["app", "action", "actionType", "connection"],
    fetch: async (sdk, resolvedParams) => {
        const fieldsResponse = await sdk.listInputFields({
            app: resolvedParams.app,
            action: resolvedParams.action,
            actionType: resolvedParams.actionType,
            connection: resolvedParams.connection,
            inputs: resolvedParams.inputs,
        });
        // Flatten the fieldset structure for field selection
        return flattenRootFieldset(fieldsResponse.data);
    },
    prompt: (fields) => ({
        type: "list",
        name: "inputField",
        message: "Select input field:",
        choices: fields.map((field) => ({
            name: field.title || field.key,
            hint: [
                field.value_type,
                field.is_required ? "required" : "optional",
            ].filter(Boolean),
            value: field.key,
        })),
    }),
};
