"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SalesforcePlugin = exports.SalesforcePluginVersions = void 0;
const index_js_1 = require("../../types/index.js");
const constants_js_1 = require("./constants.js");
const index_js_2 = require("./shared/index.js");
const index_js_3 = require("./shared/index.js");
exports.SalesforcePluginVersions = {
    V1: '0.0.1',
    V2: '0.0.2'
};
const CONNECTION_TYPE_FIELD = 'connection.auth.method.case';
const ACTION_DROPDOWN = [
    {
        label: 'Query with SOQL',
        value: 'soql',
        children: [{ label: 'SOQL', value: '1' }]
    },
    {
        label: 'Single object actions',
        value: 'crud',
        children: [
            { label: 'Create a single object', value: '1' },
            { label: 'Update a single object', value: '2' },
            { label: 'Delete a single object', value: '3' },
            { label: 'Read a single object', value: '4' }
        ]
    },
    {
        label: 'Bulk actions',
        value: 'bulk',
        children: [
            { label: 'Create several objects', value: '1' },
            { label: 'Update several objects', value: '2' },
            { label: 'Delete several objects', value: '3' },
            { label: 'Upsert (update or insert) several objects', value: '4' }
        ]
    }
];
exports.SalesforcePlugin = {
    id: 'salesforce',
    name: 'Salesforce',
    moduleName: 'SalesforcePlugin',
    modulePath: 'plugins/salesforce/SalesforcePlugin',
    iconLocation: `${constants_js_1.PUBLIC_INTEGRATIONS_LOGO_URL}/salesforce.png`,
    docsUrl: `${constants_js_1.DOCS_BASE_URL}/salesforce`,
    type: index_js_1.PluginType.API,
    responseType: index_js_1.PluginResponseType.JSON,
    hasRawRequest: true,
    hasMetadata: true,
    datasourceTemplate: {
        sections: [
            {
                name: 'main',
                items: [
                    {
                        label: 'Display name',
                        name: 'name',
                        startVersion: exports.SalesforcePluginVersions.V1,
                        componentType: index_js_1.FormComponentType.INPUT_TEXT,
                        rules: [{ required: true, message: 'Display name is required' }]
                    },
                    {
                        label: 'Instance URL',
                        name: 'connection.instanceUrl',
                        startVersion: exports.SalesforcePluginVersions.V1,
                        componentType: index_js_1.FormComponentType.INPUT_TEXT,
                        placeholder: 'https://DOMAIN.my.salesforce.com',
                        rules: [{ required: true, message: 'Instance URL is required' }]
                    },
                    {
                        // it could be removed if we provide a way to pass token to plugin.metadata and plugin.test
                        // plugin.execute have access to execution context, which already contain token
                        label: 'Token',
                        name: 'authConfig.authToken',
                        startVersion: exports.SalesforcePluginVersions.V1,
                        componentType: index_js_1.FormComponentType.INPUT_TEXT,
                        initialValue: '{{oauth.token}}',
                        hidden: true,
                        display: {
                            show: {
                                [CONNECTION_TYPE_FIELD]: ['clientCredentialsFlow']
                            }
                        }
                    },
                    {
                        name: 'connection.auth.method.case',
                        label: 'Authentication',
                        startVersion: exports.SalesforcePluginVersions.V2,
                        componentType: index_js_1.FormComponentType.DROPDOWN,
                        initialValue: 'clientCredentialsFlow',
                        options: [
                            {
                                displayName: 'OAuth 2.0 Client Credentials Grant',
                                value: 'clientCredentialsFlow',
                                key: 'clientCredentialsFlow'
                            },
                            {
                                displayName: 'OAuth 2.0 Token Exchange',
                                value: index_js_1.IntegrationAuthType.OAUTH2_TOKEN_EXCHANGE_PROTO,
                                key: index_js_1.IntegrationAuthType.OAUTH2_TOKEN_EXCHANGE_PROTO
                            }
                        ]
                    },
                    // CLIENT CREDENTIALS FLOW
                    {
                        label: 'Token URL',
                        name: 'connection.auth.method.value.tokenUrl',
                        startVersion: exports.SalesforcePluginVersions.V2,
                        componentType: index_js_1.FormComponentType.DYNAMIC_INPUT_TEXT,
                        rules: [{ required: true, message: 'Token URL is required' }],
                        tooltip: {
                            markdownText: `The endpoint for authorization server. This is used to get the access token.`
                        },
                        singleLine: true,
                        display: {
                            show: {
                                'connection.auth.method.case': ['clientCredentialsFlow']
                            }
                        },
                        placeholder: 'https://login.salesforce.com/services/oauth2/token'
                    },
                    {
                        label: 'Consumer key',
                        name: 'connection.auth.method.value.clientId',
                        startVersion: exports.SalesforcePluginVersions.V2,
                        componentType: index_js_1.FormComponentType.DYNAMIC_INPUT_TEXT,
                        placeholder: `my consumer key`,
                        rules: [{ required: true, message: 'Consumer Key is required' }],
                        tooltip: {
                            markdownText: `A public identifier used to identify this Salesforce consumer
    to the authorization/token servers.`
                        },
                        singleLine: true,
                        display: {
                            show: {
                                'connection.auth.method.case': ['clientCredentialsFlow']
                            }
                        }
                    },
                    {
                        label: 'Consumer secret',
                        name: 'connection.auth.method.value.clientSecret',
                        startVersion: exports.SalesforcePluginVersions.V2,
                        componentType: index_js_1.FormComponentType.INPUT_TEXT,
                        rules: [{ required: true, message: 'Consumer secret is required' }],
                        placeholder: `my consumer secret`,
                        dataType: index_js_1.InputDataType.PASSWORD,
                        tooltip: {
                            markdownText: `A secret shared between the Salesforce consumer and the
    authorizing/token servers to verify the Consumer Key.`
                        },
                        singleLine: false,
                        display: {
                            show: {
                                'connection.auth.method.case': ['clientCredentialsFlow']
                            }
                        }
                    },
                    // OAUTH2 TOKEN EXCHANGE
                    // This is a field that will always be hidden to users.
                    // It exists to tell the agent WHERE to look on the datasource configuration for the authType.
                    // For more info, see addTokenIfNeeded.go in the orchestrator.
                    {
                        label: '',
                        name: 'authTypeField',
                        hidden: true,
                        initialValue: 'authTypeOnBehalfOfToken',
                        componentType: index_js_1.FormComponentType.INPUT_TEXT,
                        rules: [{ required: false }],
                        startVersion: exports.SalesforcePluginVersions.V2,
                        // This is a newer convention in the agent that allows us to specify the field to look at
                        // when determining authType. We only include this for OAUTH2_TOKEN_EXCHANGE so the
                        // existing clientCredentialsFlow works as-is.
                        // See: https://github.com/superblocksteam/orchestrator/blob/main/internal/auth/add_token_if_needed.go
                        display: {
                            show: {
                                [CONNECTION_TYPE_FIELD]: [index_js_1.IntegrationAuthType.OAUTH2_TOKEN_EXCHANGE_PROTO]
                            }
                        }
                    },
                    // Another field to send a value to the orchestrator. We need this because our current
                    // templates make it difficult/impossible to change the value of a field programmatically.
                    // Ideally the value for authType would be dependent upon which connection type the user
                    // has selected.
                    {
                        label: '',
                        name: 'authTypeOnBehalfOfToken',
                        hidden: true,
                        initialValue: 'oauth-token-exchange',
                        componentType: index_js_1.FormComponentType.INPUT_TEXT,
                        startVersion: exports.SalesforcePluginVersions.V2,
                        display: {
                            show: {
                                [CONNECTION_TYPE_FIELD]: [index_js_1.IntegrationAuthType.OAUTH2_TOKEN_EXCHANGE_PROTO]
                            }
                        }
                    },
                    {
                        ...index_js_2.BASE_SUBJECT_TOKEN_SOURCE,
                        startVersion: exports.SalesforcePluginVersions.V2,
                        display: {
                            show: {
                                [CONNECTION_TYPE_FIELD]: [index_js_1.IntegrationAuthType.OAUTH2_TOKEN_EXCHANGE_PROTO]
                            }
                        }
                    },
                    {
                        ...index_js_2.BASE_SUBJECT_TOKEN_STATIC_TOKEN,
                        startVersion: exports.SalesforcePluginVersions.V2,
                        display: {
                            show: {
                                [CONNECTION_TYPE_FIELD]: [index_js_1.IntegrationAuthType.OAUTH2_TOKEN_EXCHANGE_PROTO],
                                'authConfig.subjectTokenSource': ['SUBJECT_TOKEN_SOURCE_STATIC_TOKEN']
                            }
                        }
                    },
                    {
                        ...index_js_2.BASE_SUBJECT_TOKEN_TYPE,
                        startVersion: exports.SalesforcePluginVersions.V2,
                        placeholder: 'urn:ietf:params:oauth:token-type:access_token',
                        display: {
                            show: {
                                [CONNECTION_TYPE_FIELD]: [index_js_1.IntegrationAuthType.OAUTH2_TOKEN_EXCHANGE_PROTO]
                            }
                        }
                    },
                    {
                        ...index_js_2.BASE_TOKEN_URL,
                        startVersion: exports.SalesforcePluginVersions.V2,
                        display: {
                            show: {
                                [CONNECTION_TYPE_FIELD]: [index_js_1.IntegrationAuthType.OAUTH2_TOKEN_EXCHANGE_PROTO]
                            }
                        }
                    },
                    {
                        ...index_js_2.BASE_CLIENT_ID,
                        startVersion: exports.SalesforcePluginVersions.V2,
                        rules: [{ required: false }],
                        display: {
                            show: {
                                [CONNECTION_TYPE_FIELD]: [index_js_1.IntegrationAuthType.OAUTH2_TOKEN_EXCHANGE_PROTO]
                            }
                        }
                    },
                    {
                        ...index_js_2.BASE_CLIENT_SECRET,
                        startVersion: exports.SalesforcePluginVersions.V2,
                        rules: [{ required: false }],
                        display: {
                            show: {
                                [CONNECTION_TYPE_FIELD]: [index_js_1.IntegrationAuthType.OAUTH2_TOKEN_EXCHANGE_PROTO]
                            }
                        }
                    },
                    {
                        ...index_js_2.BASE_SCOPE,
                        startVersion: exports.SalesforcePluginVersions.V2,
                        display: {
                            show: {
                                [CONNECTION_TYPE_FIELD]: [index_js_1.IntegrationAuthType.OAUTH2_TOKEN_EXCHANGE_PROTO]
                            }
                        }
                    }
                ]
            },
            ...(0, index_js_3.authSections)({
                startVersion: exports.SalesforcePluginVersions.V1,
                endVersion: exports.SalesforcePluginVersions.V1,
                pluginId: index_js_1.ExtendedIntegrationPluginId.SALESFORCE,
                pluginName: 'Salesforce',
                defaultMethod: 'clientCredentialsFlow',
                enabledMethods: {
                    clientCredentialsFlow: true
                },
                allowNone: false,
                fieldNamesToHide: [],
                isProto: true
            })
        ]
    },
    actionTemplate: {
        sections: [
            {
                name: 'main',
                items: [
                    {
                        label: 'Action',
                        name: 'salesforceAction.value.action', // expects .value.action that is a enum
                        secondaryName: 'salesforceAction.case', // expects proto top level oneof case
                        startVersion: exports.SalesforcePluginVersions.V1,
                        componentType: index_js_1.FormComponentType.ONEOF_ACTION_DROPDOWN,
                        actionOptions: ACTION_DROPDOWN,
                        initialValue: '1',
                        secondaryInitialValue: 'soql'
                    },
                    {
                        label: '', // Query
                        name: 'salesforceAction.value.sqlBody',
                        startVersion: exports.SalesforcePluginVersions.V1,
                        componentType: index_js_1.FormComponentType.CODE_EDITOR,
                        language: index_js_1.EditorLanguage.SQL,
                        initialValue: '',
                        rules: [{ required: true, message: 'SOQL Query is required' }],
                        display: {
                            show: {
                                'salesforceAction.value.action': ['1'],
                                'salesforceAction.case': ['soql']
                            }
                        }
                    },
                    {
                        label: 'Resource type',
                        name: 'salesforceAction.value.resourceType',
                        startVersion: exports.SalesforcePluginVersions.V1,
                        componentType: index_js_1.FormComponentType.DYNAMIC_INPUT_TEXT,
                        initialValue: '',
                        rules: [{ required: true, message: 'Resource type is required' }],
                        display: {
                            show: {
                                'salesforceAction.value.action': ['1', '2', '3', '4'],
                                'salesforceAction.case': ['crud']
                            }
                        }
                    },
                    {
                        label: 'Resource ID',
                        name: 'salesforceAction.value.resourceId',
                        startVersion: exports.SalesforcePluginVersions.V1,
                        componentType: index_js_1.FormComponentType.DYNAMIC_INPUT_TEXT,
                        rules: [{ required: true, message: 'Resource ID is required' }],
                        display: {
                            show: {
                                'salesforceAction.value.action': ['2', '3', '4'],
                                'salesforceAction.case': ['crud']
                            }
                        }
                    },
                    {
                        label: 'Resource body',
                        name: 'salesforceAction.value.resourceBody',
                        startVersion: exports.SalesforcePluginVersions.V1,
                        componentType: index_js_1.FormComponentType.CODE_EDITOR,
                        language: index_js_1.EditorLanguage.JSON,
                        rules: [{ required: true, message: 'Resource body is required' }],
                        display: {
                            show: {
                                'salesforceAction.value.action': ['1', '2'],
                                'salesforceAction.case': ['crud']
                            }
                        },
                        placeholder: '{"Name": "Example Company Name", "Industry": "Health Care", "Website": "www.example.com"}'
                    },
                    {
                        label: 'Resource type',
                        name: 'salesforceAction.value.resourceType',
                        startVersion: exports.SalesforcePluginVersions.V1,
                        componentType: index_js_1.FormComponentType.DYNAMIC_INPUT_TEXT,
                        rules: [{ required: true, message: 'Resource Type is required' }],
                        display: {
                            show: {
                                'salesforceAction.value.action': ['1', '2', '3', '4'],
                                'salesforceAction.case': ['bulk']
                            }
                        }
                    },
                    {
                        label: 'External ID (optional)',
                        name: 'salesforceAction.value.externalId',
                        startVersion: exports.SalesforcePluginVersions.V1,
                        componentType: index_js_1.FormComponentType.DYNAMIC_INPUT_TEXT,
                        tooltip: {
                            markdownText: 'External ID is a field that uniquely identifies records outside of Salesforce. The upsert command will use this field to determine if a record should be created or updated'
                        },
                        placeholder: 'id',
                        display: {
                            show: {
                                'salesforceAction.value.action': ['4'], // upsert
                                'salesforceAction.case': ['bulk']
                            }
                        }
                    },
                    {
                        label: 'Bulk body',
                        name: 'salesforceAction.value.resourceBody',
                        startVersion: exports.SalesforcePluginVersions.V1,
                        componentType: index_js_1.FormComponentType.CODE_EDITOR,
                        language: index_js_1.EditorLanguage.JSON,
                        rules: [{ required: true, message: 'Bulk body is required' }],
                        display: {
                            show: {
                                'salesforceAction.value.action': ['1', '2', '3', '4'],
                                'salesforceAction.case': ['bulk']
                            }
                        },
                        placeholder: '[{"Name": "Example Company Name1", "Website": "www.example.com"}, {"Name": "Example Company Name2", "Website": "www.example2.com"}]'
                    }
                ]
            }
        ]
    }
};
//# sourceMappingURL=salesforce.js.map