"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNativeOpenApiPlugin = exports.oauth2BringYourOwnPluginIds = void 0;
const lodash_1 = require("lodash");
const index_js_1 = require("../../../types/index.js");
const constants_js_1 = require("../constants.js");
const auth_js_1 = require("./auth.js");
const restapiintegration_js_1 = require("./restapiintegration.js");
// Automatically populated when getNativeOpenApiPlugin is called with
// authMethods.oauth2BringYourOwn. Used by the server to validate that
// PLUGIN_REVOCATION_CONFIG covers all known oauth2BringYourOwn plugins.
exports.oauth2BringYourOwnPluginIds = new Set();
const getNativeOpenApiPlugin = ({ id, name, serverURL, authMethods, docsURL, logoURL, openApiSpecURL, headers, tenantInput, isStreamable }) => {
    if (authMethods.oauth2BringYourOwn) {
        exports.oauth2BringYourOwnPluginIds.add(id);
    }
    const mainSectionItems = [
        {
            label: 'Display name',
            name: 'name',
            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V1,
            componentType: index_js_1.FormComponentType.INPUT_TEXT,
            placeholder: 'Example API',
            rules: [{ required: true, message: 'Display name is required' }]
        },
        {
            name: 'urlBase',
            label: 'Base URL',
            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V1,
            componentType: index_js_1.FormComponentType.INPUT_TEXT,
            rules: [{ required: true, message: 'Base URL is required' }],
            initialValue: serverURL,
            hidden: true
        }
    ];
    if (!(0, lodash_1.isEmpty)(serverURL) && serverURL.includes(index_js_1.SUPERBLOCKS_OPENAPI_TENANT_KEYWORD)) {
        mainSectionItems.push({
            name: index_js_1.RestApiFields.OPENAPI_TENANT_NAME,
            label: tenantInput?.label ?? 'Tenant',
            subtitle: tenantInput?.subtitle,
            placeholder: tenantInput?.placeholder ?? 'my-tenant',
            initialValue: tenantInput?.initialValue,
            // TODO: The plugin needs to be able to give a good runtime error saying
            // that it isn't supported. Need to think about this versioning story
            // more.
            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V1, // TODO: Should really be a REST version bump.
            componentType: index_js_1.FormComponentType.DYNAMIC_INPUT_TEXT,
            rules: [{ required: true, message: '' }]
        });
    }
    if (!(0, lodash_1.isEmpty)(headers)) {
        mainSectionItems.push({
            name: restapiintegration_js_1.RestApiIntegrationPluginMergedFieldNames.HEADERS,
            label: '',
            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V1,
            componentType: index_js_1.FormComponentType.FIELD_LIST,
            initialValue: headers,
            hidden: true
        });
    }
    return {
        id,
        name,
        moduleName: 'RestApiIntegrationPlugin',
        modulePath: 'plugins/restApiIntegration/RestApiIntegrationPlugin',
        iconLocation: logoURL ?? `${constants_js_1.PUBLIC_INTEGRATIONS_LOGO_URL}/${id}.png`,
        openApiSpecRef: openApiSpecURL ?? `${constants_js_1.PUBLIC_INTEGRATIONS_CDN_URL}/openapi/${id}.yaml`,
        docsUrl: docsURL ?? `${constants_js_1.DOCS_BASE_URL}/${id}`,
        type: index_js_1.PluginType.API,
        responseType: index_js_1.PluginResponseType.JSON,
        isStreamable,
        hasRawRequest: true,
        hasTest: true,
        datasourceTemplate: {
            sections: [
                {
                    name: 'main',
                    items: mainSectionItems
                },
                ...(0, auth_js_1.authSections)({
                    startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V1,
                    pluginId: id,
                    pluginName: name,
                    enabledMethods: authMethods,
                    allowNone: false
                })
            ]
        },
        actionTemplate: {
            sections: [
                {
                    name: 'main',
                    items: [
                        {
                            name: 'openApiAction',
                            label: 'Action',
                            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V8,
                            componentType: index_js_1.FormComponentType.OPENAPI_ACTION_DROPDOWN,
                            initialValue: index_js_1.GENERIC_HTTP_REQUEST
                        },
                        {
                            gridCss: {
                                gridTemplateColumns: '120px calc(100% - 130px)'
                            },
                            rowItems: [
                                {
                                    name: index_js_1.RestApiFields.HTTP_METHOD,
                                    label: '',
                                    startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V1,
                                    componentType: index_js_1.FormComponentType.DROPDOWN,
                                    initialValue: index_js_1.HttpMethod.GET,
                                    rules: [{ required: true }],
                                    renderSelectedOptionWithStyles: true,
                                    options: [
                                        {
                                            key: index_js_1.HttpMethod.GET,
                                            value: index_js_1.HttpMethod.GET,
                                            displayName: index_js_1.HttpMethod.GET,
                                            textColor: (0, index_js_1.getMethodColor)(index_js_1.HttpMethod.GET)
                                        },
                                        {
                                            key: index_js_1.HttpMethod.POST,
                                            value: index_js_1.HttpMethod.POST,
                                            displayName: index_js_1.HttpMethod.POST,
                                            textColor: (0, index_js_1.getMethodColor)(index_js_1.HttpMethod.POST)
                                        },
                                        {
                                            key: index_js_1.HttpMethod.PUT,
                                            value: index_js_1.HttpMethod.PUT,
                                            displayName: index_js_1.HttpMethod.PUT,
                                            textColor: (0, index_js_1.getMethodColor)(index_js_1.HttpMethod.PUT)
                                        },
                                        {
                                            key: index_js_1.HttpMethod.DELETE,
                                            value: index_js_1.HttpMethod.DELETE,
                                            displayName: index_js_1.HttpMethod.DELETE,
                                            textColor: (0, index_js_1.getMethodColor)(index_js_1.HttpMethod.DELETE)
                                        },
                                        {
                                            key: index_js_1.HttpMethod.PATCH,
                                            value: index_js_1.HttpMethod.PATCH,
                                            displayName: index_js_1.HttpMethod.PATCH,
                                            textColor: (0, index_js_1.getMethodColor)(index_js_1.HttpMethod.PATCH)
                                        }
                                    ]
                                },
                                {
                                    name: index_js_1.RestApiFields.URL_PATH,
                                    label: '',
                                    startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V1,
                                    componentType: index_js_1.FormComponentType.URL_INPUT_TEXT,
                                    placeholder: '/v1/users'
                                }
                            ]
                        }
                    ]
                },
                {
                    name: 'tabs',
                    layout: index_js_1.FormSectionLayout.TABS,
                    items: [
                        {
                            name: restapiintegration_js_1.RestApiIntegrationPluginMergedFieldNames.HEADERS,
                            label: 'Headers',
                            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V1,
                            componentType: index_js_1.FormComponentType.FIELD_LIST,
                            collapseValue: 'Hide auto-generated headers'
                        },
                        {
                            name: restapiintegration_js_1.RestApiIntegrationPluginMergedFieldNames.PARAMS,
                            label: 'Query parameters',
                            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V1,
                            componentType: index_js_1.FormComponentType.FIELD_LIST
                        },
                        {
                            name: index_js_1.RestApiFields.BODY_TYPE,
                            label: (0, index_js_1.getRestApiFieldDisplayName)(index_js_1.RestApiFields.BODY_TYPE),
                            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V8,
                            componentType: index_js_1.FormComponentType.DROPDOWN,
                            initialValue: index_js_1.RestApiBodyDataType.JSON,
                            options: Object.values(index_js_1.RestApiBodyDataType).map((bodyType) => ({
                                displayName: (0, index_js_1.getRestApiDataTypeDisplayName)(bodyType),
                                value: bodyType,
                                key: bodyType
                            }))
                        },
                        {
                            name: index_js_1.RestApiFields.BODY,
                            label: (0, index_js_1.getRestApiBodyLabel)(index_js_1.RestApiBodyDataType.JSON),
                            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V8,
                            componentType: index_js_1.FormComponentType.CODE_EDITOR,
                            language: index_js_1.EditorLanguage.JSON,
                            display: {
                                show: {
                                    bodyType: [index_js_1.RestApiBodyDataType.JSON]
                                }
                            }
                        },
                        {
                            name: index_js_1.RestApiFields.BODY,
                            label: (0, index_js_1.getRestApiBodyLabel)(index_js_1.RestApiBodyDataType.RAW),
                            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V8,
                            componentType: index_js_1.FormComponentType.CODE_EDITOR,
                            language: index_js_1.EditorLanguage.TEXT,
                            display: {
                                show: {
                                    bodyType: [index_js_1.RestApiBodyDataType.RAW]
                                }
                            }
                        },
                        {
                            name: index_js_1.RestApiFields.FORM_DATA,
                            label: (0, index_js_1.getRestApiBodyLabel)(index_js_1.RestApiBodyDataType.FORM),
                            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V8,
                            componentType: index_js_1.FormComponentType.FIELD_LIST_FORM,
                            display: {
                                show: {
                                    bodyType: [index_js_1.RestApiBodyDataType.FORM]
                                }
                            }
                        },
                        {
                            name: index_js_1.RestApiFields.FILE_FORM_KEY,
                            label: (0, index_js_1.getRestApiFieldDisplayName)(index_js_1.RestApiFields.FILE_FORM_KEY),
                            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V8,
                            componentType: index_js_1.FormComponentType.INPUT_TEXT,
                            initialValue: 'file',
                            display: {
                                show: {
                                    bodyType: [index_js_1.RestApiBodyDataType.FILE_FORM]
                                }
                            }
                        },
                        {
                            name: index_js_1.RestApiFields.FILE_NAME,
                            label: (0, index_js_1.getRestApiFieldDisplayName)(index_js_1.RestApiFields.FILE_NAME),
                            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V8,
                            componentType: index_js_1.FormComponentType.INPUT_TEXT,
                            display: {
                                show: {
                                    bodyType: [index_js_1.RestApiBodyDataType.FILE_FORM]
                                }
                            }
                        },
                        {
                            name: index_js_1.RestApiFields.BODY,
                            label: (0, index_js_1.getRestApiBodyLabel)(index_js_1.RestApiBodyDataType.FILE_FORM),
                            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V8,
                            componentType: index_js_1.FormComponentType.CODE_EDITOR,
                            language: index_js_1.EditorLanguage.TEXT,
                            display: {
                                show: {
                                    bodyType: [index_js_1.RestApiBodyDataType.FILE_FORM]
                                }
                            }
                        }
                    ]
                },
                {
                    name: 'bottom',
                    items: [
                        {
                            name: index_js_1.RestApiFields.RESPONSE_TYPE,
                            label: (0, index_js_1.getRestApiFieldDisplayName)(index_js_1.RestApiFields.RESPONSE_TYPE),
                            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V1,
                            componentType: index_js_1.FormComponentType.DROPDOWN,
                            initialValue: index_js_1.ActionResponseType.AUTO,
                            rules: [{ required: true }],
                            options: [
                                {
                                    key: index_js_1.ActionResponseType.AUTO,
                                    value: index_js_1.ActionResponseType.AUTO,
                                    displayName: 'Auto'
                                },
                                {
                                    key: index_js_1.ActionResponseType.JSON,
                                    value: index_js_1.ActionResponseType.JSON,
                                    displayName: 'JSON'
                                },
                                {
                                    key: index_js_1.ActionResponseType.TEXT,
                                    value: index_js_1.ActionResponseType.TEXT,
                                    displayName: 'Text'
                                },
                                {
                                    key: index_js_1.ActionResponseType.BINARY,
                                    value: index_js_1.ActionResponseType.BINARY,
                                    displayName: 'Binary'
                                },
                                {
                                    key: index_js_1.ActionResponseType.RAW,
                                    value: index_js_1.ActionResponseType.RAW,
                                    displayName: 'Raw'
                                }
                            ]
                        }
                    ]
                },
                {
                    name: 'additionalSettings',
                    sectionHeader: 'Additional Settings',
                    isCollapsible: true,
                    defaultCollapsed: true,
                    asTitle: false,
                    items: [
                        {
                            name: index_js_1.RestApiFields.VERBOSE_HTTP_OUTPUT,
                            label: (0, index_js_1.getRestApiFieldDisplayName)(index_js_1.RestApiFields.VERBOSE_HTTP_OUTPUT),
                            lightLabelText: true,
                            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V14,
                            componentType: index_js_1.FormComponentType.SWITCH,
                            initialValue: false,
                            showConfirm: false,
                            renderIconFirst: true
                        },
                        {
                            name: index_js_1.RestApiFields.DO_NOT_FAIL_ON_REQUEST_ERROR,
                            label: (0, index_js_1.getRestApiFieldDisplayName)(index_js_1.RestApiFields.DO_NOT_FAIL_ON_REQUEST_ERROR),
                            lightLabelText: true,
                            startVersion: restapiintegration_js_1.RestApiIntegrationPluginVersions.V14,
                            componentType: index_js_1.FormComponentType.SWITCH,
                            initialValue: false,
                            showConfirm: false,
                            renderIconFirst: true,
                            inverseValue: true
                        }
                    ]
                }
            ]
        }
    };
};
exports.getNativeOpenApiPlugin = getNativeOpenApiPlugin;
//# sourceMappingURL=openapi.js.map