"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CosmosDbPlugin = void 0;
const index_js_1 = require("../../types/index.js");
const constants_js_1 = require("./constants.js");
const CosmosDbPluginVersions = {
    V1: '0.0.1'
};
// const CONNECTION_METHODS_AND_DISPLAY_NAMES: Record<string, string> = {
//   fields: 'Fields'
// };
const AUTH_METHODS_AND_DISPLAY_NAMES = {
    key: 'Azure key'
};
const ACTION_DROPDOWN = [
    {
        label: 'Query with SQL',
        value: 'sql',
        children: [{ label: 'SQL', value: 'singleton' }]
    },
    // NOTE: (joey) we need to be careful with verbage here.
    // I believe if a container has multiple items with an undefined partition key and the same id, it will affect all of them.
    {
        label: 'Single Object Actions',
        value: 'pointOperation',
        children: [
            { label: 'Create an object', value: 'create' },
            { label: 'Read an object', value: 'read' },
            { label: 'Upsert an object', value: 'upsert' },
            { label: 'Replace an object', value: 'replace' },
            { label: 'Delete an object', value: 'delete' }
        ]
    }
];
const makeDropdownItem = (value, displayName, subText) => {
    return {
        key: value,
        value: value,
        displayName: displayName ?? value,
        subText: subText ?? ''
    };
};
exports.CosmosDbPlugin = {
    id: 'cosmosdb',
    name: 'Azure Cosmos DB',
    moduleName: 'CosmosDbPlugin',
    modulePath: 'plugins/cosmosdb/CosmosDbPlugin',
    iconLocation: `${constants_js_1.PUBLIC_INTEGRATIONS_LOGO_URL}/cosmosdb.png`,
    docsUrl: `${constants_js_1.DOCS_BASE_URL}/cosmosdb`,
    type: index_js_1.PluginType.DB,
    responseType: index_js_1.PluginResponseType.JSON,
    hasRawRequest: false,
    hasMetadata: true,
    datasourceTemplate: {
        sections: [
            {
                name: 'main',
                items: [
                    {
                        label: 'Display name',
                        name: 'name',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.INPUT_TEXT,
                        placeholder: 'Example CosmosDB Integration',
                        rules: [{ required: true, message: 'Display name is required' }]
                    },
                    {
                        label: 'Auth type',
                        name: 'connection.auth.config.case',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.DROPDOWN,
                        initialValue: 'key',
                        hidden: true,
                        rules: [{ required: true }],
                        options: Object.entries(AUTH_METHODS_AND_DISPLAY_NAMES).map(([value, displayName]) => makeDropdownItem(value, displayName))
                    },
                    // TODO: (joey) only show these fields when 'fields' is selected
                    {
                        label: 'Host',
                        name: 'connection.host',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.DYNAMIC_INPUT_TEXT,
                        placeholder: 'https://my-org.documents.azure.com',
                        rules: [{ required: true, message: 'Host is required' }]
                    },
                    {
                        label: 'Port',
                        name: 'connection.port',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.INPUT_TEXT,
                        dataType: index_js_1.InputDataType.NUMBER,
                        initialValue: 443,
                        placeholder: '443',
                        rules: [{ required: true, message: 'Port is required' }]
                    },
                    {
                        label: 'Database ID',
                        name: 'connection.databaseId',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.DYNAMIC_INPUT_TEXT,
                        subtitle: 'Select **Browse** in the CosmosDB panel to see your collections and database IDs.',
                        rules: [{ required: true, message: 'Database ID is required' }]
                    },
                    {
                        label: 'Master key',
                        name: 'connection.auth.config.value.masterKey',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.INPUT_TEXT,
                        dataType: index_js_1.InputDataType.PASSWORD,
                        rules: [{ required: true, message: 'Master key is required' }]
                    }
                ]
            }
        ]
    },
    actionTemplate: {
        sections: [
            {
                name: 'main',
                items: [
                    {
                        label: 'Action',
                        name: 'cosmosdbAction.value.action.case',
                        secondaryName: 'cosmosdbAction.case',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.ONEOF_ACTION_DROPDOWN,
                        actionOptions: ACTION_DROPDOWN,
                        initialValue: 'singleton',
                        secondaryInitialValue: 'sql'
                    },
                    // container id for sql
                    {
                        label: 'Container ID',
                        name: 'cosmosdbAction.value.action.value.containerId',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.DYNAMIC_INPUT_TEXT,
                        placeholder: 'my-table',
                        rules: [{ required: true, message: 'Container ID is required' }],
                        display: {
                            show: {
                                'cosmosdbAction.value.action.case': ['singleton'],
                                'cosmosdbAction.case': ['sql']
                            }
                        }
                    },
                    // container id for point operations
                    {
                        label: 'Container ID',
                        name: 'cosmosdbAction.value.containerId',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.DYNAMIC_INPUT_TEXT,
                        rules: [{ required: true, message: 'Container ID is required' }],
                        display: {
                            show: {
                                'cosmosdbAction.value.action.case': ['read', 'replace', 'upsert', 'delete', 'create'],
                                'cosmosdbAction.case': ['pointOperation']
                            }
                        }
                    },
                    {
                        label: 'Cross partition',
                        name: 'cosmosdbAction.value.action.value.crossPartition',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.CHECKBOX,
                        initialValue: true,
                        display: {
                            show: {
                                'cosmosdbAction.value.action.case': ['singleton'],
                                'cosmosdbAction.case': ['sql']
                            }
                        }
                    },
                    {
                        label: 'Item partition key (optional)',
                        name: 'cosmosdbAction.value.action.value.partitionKey',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.DYNAMIC_INPUT_TEXT,
                        placeholder: 'tenant-1',
                        display: {
                            show: {
                                'cosmosdbAction.value.action.value.crossPartition': ['false'],
                                'cosmosdbAction.value.action.case': ['singleton'],
                                'cosmosdbAction.case': ['sql']
                            }
                        }
                    },
                    {
                        label: 'Query',
                        name: 'cosmosdbAction.value.action.value.query',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.CODE_EDITOR,
                        language: index_js_1.EditorLanguage.SQL,
                        initialValue: '',
                        display: {
                            show: {
                                'cosmosdbAction.value.action.case': ['singleton'],
                                'cosmosdbAction.case': ['sql']
                            }
                        }
                    },
                    // Point Operations
                    {
                        label: 'Item ID',
                        name: 'cosmosdbAction.value.action.value.id',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.DYNAMIC_INPUT_TEXT,
                        rules: [{ required: true, message: 'Item ID is required' }],
                        display: {
                            show: {
                                'cosmosdbAction.value.action.case': ['read', 'delete'],
                                'cosmosdbAction.case': ['pointOperation']
                            }
                        }
                    },
                    {
                        label: 'Item partition key',
                        name: 'cosmosdbAction.value.action.value.partitionKey',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.DYNAMIC_INPUT_TEXT,
                        placeholder: 'tenant-1',
                        tooltip: {
                            markdownText: 'Used to match on a specific item. Leave blank to match only on items without an explicitly assigned partition key'
                        },
                        display: {
                            show: {
                                // NOTE: (joey) this field *can* be used for UPSERT and CREATE
                                // we are ommiting them for now for form simplicity
                                'cosmosdbAction.value.action.case': ['read', 'delete', 'replace'],
                                'cosmosdbAction.case': ['pointOperation']
                            }
                        }
                    },
                    {
                        label: 'Item body',
                        name: 'cosmosdbAction.value.action.value.body',
                        startVersion: CosmosDbPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.CODE_EDITOR,
                        language: index_js_1.EditorLanguage.JSON,
                        display: {
                            show: {
                                'cosmosdbAction.value.action.case': ['create', 'replace', 'upsert'],
                                'cosmosdbAction.case': ['pointOperation']
                            }
                        },
                        placeholder: '{"id": "12345", "name": "John Smith", "age": 25}',
                        rules: [{ required: true, message: 'Item body is required' }]
                    }
                ]
            }
        ]
    }
};
//# sourceMappingURL=cosmosdb.js.map