import { DB_SQL_INITIAL_TEXT, EditorLanguage, FormComponentType, InputDataType, PluginResponseType, PluginType } from '../../types/index.js';
import { PARAMETERIZED_SQL_DESCRIPTION, PUBLIC_INTEGRATIONS_LOGO_URL, DOCS_BASE_URL } from './constants.js';
import { SqlOperations } from './shared/index.js';
import { CONNECTION_METHODS_AND_DISPLAY_NAMES, makeDropdownItem } from './shared/db.js';
const MariaDBPluginVersions = {
    V1: '0.0.1',
    V2: '0.0.2',
    V6: '0.0.6',
    V8: '0.0.8',
    V9: '0.0.9',
    V10: '0.0.10',
    V11: '0.0.11',
    V12: '0.0.12'
};
const BASE_HOST = {
    label: 'Host',
    name: 'endpoint.host',
    componentType: FormComponentType.INPUT_TEXT,
    placeholder: 'staging-database.amazonaws.com',
    rules: [{ required: true, message: 'Host is required' }]
};
const BASE_PORT = {
    label: 'Port',
    name: 'endpoint.port',
    componentType: FormComponentType.INPUT_TEXT,
    initialValue: '3306',
    rules: [{ required: true, message: 'Port is required' }]
};
const BASE_DATABASE_NAME = {
    label: 'Database name',
    name: 'authentication.custom.databaseName.value',
    componentType: FormComponentType.INPUT_TEXT
};
const BASE_DATABASE_USERNAME = {
    label: 'Database username',
    name: 'authentication.username',
    componentType: FormComponentType.INPUT_TEXT
};
const BASE_DATABASE_PASSWORD = {
    label: 'Database password',
    name: 'authentication.password',
    componentType: FormComponentType.INPUT_TEXT,
    dataType: InputDataType.PASSWORD
};
const BASE_ENABLE_SSL = {
    label: 'Enable SSL',
    name: 'connection.useSsl',
    initialValue: 'checked',
    componentType: FormComponentType.CHECKBOX
};
const BASE_SELFSIGNED_SSL = {
    label: 'Use a self-signed SSL certificate',
    name: 'connection.useSelfSignedSsl',
    componentType: FormComponentType.CHECKBOX,
    tooltip: {
        markdownText: 'Use a self-signed SSL certificate, e.g. when [connecting to Cloud SQL](https://cloud.google.com/sql/docs/postgres/connect-admin-ip#connect-ssl)'
    }
};
const BASE_SERVER_CA = {
    label: 'Server CA',
    name: 'connection.ca',
    componentType: FormComponentType.CODE_EDITOR,
    language: EditorLanguage.TEXT,
    placeholder: '-----BEGIN CERTIFICATE-----\n' +
        'MIIC/jCCAeYCCQDLwS4pIwJC3zANBgkqhkiG9w0BAQsFADBBMQswCQYDVQQGEwJV\n' +
        '...\n' +
        '-----END CERTIFICATE-----'
};
const BASE_CLIENT_KEY = {
    label: 'Client key',
    name: 'connection.key',
    componentType: FormComponentType.CODE_EDITOR,
    language: EditorLanguage.TEXT,
    placeholder: '-----BEGIN RSA PRIVATE KEY-----\n' +
        'BAoMBFRlc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCpvozN05Ou\n' +
        '...\n' +
        '-----END RSA PRIVATE KEY-----'
};
const BASE_CLIENT_CERT = {
    label: 'Client cert',
    name: 'connection.cert',
    componentType: FormComponentType.CODE_EDITOR,
    language: EditorLanguage.TEXT,
    placeholder: '-----BEGIN CERTIFICATE-----\n' +
        'Ur2LYWdrVjqlS/wJyVIze15FMf7sgl+RINsLbQDLwS4pIwJC3zANBgkqhkiG9w0B\n' +
        '...\n' +
        '-----END CERTIFICATE-----'
};
export const MariaDBPlugin = {
    id: 'mariadb',
    name: 'MariaDB',
    moduleName: 'MariaDBPlugin',
    modulePath: 'plugins/mariadb/MariaDBPlugin',
    iconLocation: `${PUBLIC_INTEGRATIONS_LOGO_URL}/mariadb.png`,
    docsUrl: `${DOCS_BASE_URL}/mariadb`,
    type: PluginType.DB,
    responseType: PluginResponseType.TABLE,
    hasRawRequest: true,
    hasMetadata: true,
    rawRequestName: 'Executed SQL',
    datasourceTemplate: {
        sections: [
            {
                name: 'main',
                items: [
                    {
                        label: 'Display name',
                        name: 'name',
                        startVersion: MariaDBPluginVersions.V1,
                        componentType: FormComponentType.INPUT_TEXT,
                        placeholder: 'ProdDB',
                        rules: [{ required: true, message: 'Display name is required' }]
                    },
                    {
                        label: 'Connection method',
                        name: 'connectionType',
                        startVersion: MariaDBPluginVersions.V11,
                        componentType: FormComponentType.DROPDOWN,
                        initialValue: 'fields',
                        rules: [{ required: true }],
                        options: Object.entries(CONNECTION_METHODS_AND_DISPLAY_NAMES).map(([value, displayName]) => makeDropdownItem(value, displayName))
                    },
                    {
                        label: 'URI',
                        name: 'connectionUrl',
                        startVersion: MariaDBPluginVersions.V11,
                        componentType: FormComponentType.DYNAMIC_INPUT_TEXT,
                        placeholder: `mariadb://[:username]:[:password]@[:host]:[:port]/[:database]`,
                        tooltip: {
                            markdownText: 'Learn more about [MariaDB connection options](https://github.com/mariadb-corporation/mariadb-connector-nodejs/blob/master/documentation/connection-options.md)'
                        },
                        rules: [{ required: true, message: 'URI is required' }],
                        display: {
                            show: {
                                connectionType: ['url']
                            }
                        }
                    },
                    // HOST
                    { ...BASE_HOST, startVersion: MariaDBPluginVersions.V1, endVersion: MariaDBPluginVersions.V10 },
                    {
                        ...BASE_HOST,
                        startVersion: MariaDBPluginVersions.V11,
                        display: {
                            show: {
                                connectionType: ['fields']
                            }
                        }
                    },
                    // PORT
                    { ...BASE_PORT, startVersion: MariaDBPluginVersions.V1, endVersion: MariaDBPluginVersions.V10 },
                    {
                        ...BASE_PORT,
                        startVersion: MariaDBPluginVersions.V11,
                        display: {
                            show: {
                                connectionType: ['fields']
                            }
                        }
                    },
                    // DATABASE NAME
                    { ...BASE_DATABASE_NAME, startVersion: MariaDBPluginVersions.V1, endVersion: MariaDBPluginVersions.V10 },
                    {
                        ...BASE_DATABASE_NAME,
                        startVersion: MariaDBPluginVersions.V11,
                        display: {
                            show: {
                                connectionType: ['fields']
                            }
                        }
                    },
                    // DATABASE USERNAME
                    { ...BASE_DATABASE_USERNAME, startVersion: MariaDBPluginVersions.V1, endVersion: MariaDBPluginVersions.V10 },
                    {
                        ...BASE_DATABASE_USERNAME,
                        startVersion: MariaDBPluginVersions.V11,
                        display: {
                            show: {
                                connectionType: ['fields']
                            }
                        }
                    },
                    // DATABASE PASSWORD
                    { ...BASE_DATABASE_PASSWORD, startVersion: MariaDBPluginVersions.V1, endVersion: MariaDBPluginVersions.V10 },
                    {
                        ...BASE_DATABASE_PASSWORD,
                        startVersion: MariaDBPluginVersions.V11,
                        display: {
                            show: {
                                connectionType: ['fields']
                            }
                        }
                    },
                    // ENABLE SSL
                    { ...BASE_ENABLE_SSL, startVersion: MariaDBPluginVersions.V1, endVersion: MariaDBPluginVersions.V10 },
                    {
                        ...BASE_ENABLE_SSL,
                        startVersion: MariaDBPluginVersions.V11,
                        display: {
                            show: {
                                connectionType: ['fields']
                            }
                        }
                    },
                    // USE SELF-SIGNED SSL
                    { ...BASE_SELFSIGNED_SSL, startVersion: MariaDBPluginVersions.V1, endVersion: MariaDBPluginVersions.V10 },
                    {
                        ...BASE_SELFSIGNED_SSL,
                        startVersion: MariaDBPluginVersions.V11,
                        display: {
                            show: {
                                connectionType: ['fields']
                            }
                        }
                    },
                    // SERVER CA
                    {
                        ...BASE_SERVER_CA,
                        startVersion: MariaDBPluginVersions.V1,
                        endVersion: MariaDBPluginVersions.V10,
                        display: {
                            show: {
                                'connection.useSelfSignedSsl': ['true']
                            }
                        }
                    },
                    {
                        ...BASE_SERVER_CA,
                        startVersion: MariaDBPluginVersions.V11,
                        display: {
                            show: {
                                'connection.useSelfSignedSsl': ['true'],
                                connectionType: ['fields']
                            }
                        }
                    },
                    // CLIENT KEY
                    {
                        ...BASE_CLIENT_KEY,
                        startVersion: MariaDBPluginVersions.V1,
                        endVersion: MariaDBPluginVersions.V10,
                        display: {
                            show: {
                                'connection.useSelfSignedSsl': ['true']
                            }
                        }
                    },
                    {
                        ...BASE_CLIENT_KEY,
                        startVersion: MariaDBPluginVersions.V11,
                        display: {
                            show: {
                                'connection.useSelfSignedSsl': ['true'],
                                connectionType: ['fields']
                            }
                        }
                    },
                    // CLIENT CERT
                    {
                        ...BASE_CLIENT_CERT,
                        startVersion: MariaDBPluginVersions.V1,
                        endVersion: MariaDBPluginVersions.V10,
                        display: {
                            show: {
                                'connection.useSelfSignedSsl': ['true']
                            }
                        }
                    },
                    {
                        ...BASE_CLIENT_CERT,
                        startVersion: MariaDBPluginVersions.V11,
                        display: {
                            show: {
                                'connection.useSelfSignedSsl': ['true'],
                                connectionType: ['fields']
                            }
                        }
                    }
                ]
            }
        ]
    },
    actionTemplate: {
        sections: [
            {
                name: 'main',
                items: [
                    {
                        label: 'Operation',
                        name: 'operation',
                        startVersion: MariaDBPluginVersions.V8,
                        endVersion: MariaDBPluginVersions.V9,
                        componentType: FormComponentType.DROPDOWN,
                        options: [
                            {
                                displayName: 'Run SQL',
                                key: SqlOperations.RUN_SQL,
                                value: SqlOperations.RUN_SQL
                            },
                            {
                                displayName: 'Update rows with form',
                                key: SqlOperations.UPDATE_ROWS,
                                value: SqlOperations.UPDATE_ROWS
                            }
                        ],
                        initialValue: SqlOperations.RUN_SQL
                    },
                    {
                        label: 'Operation',
                        name: 'operation',
                        startVersion: MariaDBPluginVersions.V10,
                        endVersion: MariaDBPluginVersions.V11,
                        componentType: FormComponentType.DROPDOWN,
                        options: [
                            {
                                displayName: 'Run SQL',
                                key: SqlOperations.RUN_SQL,
                                value: SqlOperations.RUN_SQL
                            },
                            {
                                displayName: 'Insert, update, or delete rows with form',
                                key: SqlOperations.UPDATE_ROWS,
                                value: SqlOperations.UPDATE_ROWS
                            }
                        ],
                        initialValue: SqlOperations.RUN_SQL
                    },
                    {
                        // Only show operation dropdown if UPDATE_ROWS was previously selected
                        // This effectively deprecates UPDATE_ROWS for new actions
                        label: 'Operation',
                        name: 'operation',
                        startVersion: MariaDBPluginVersions.V12,
                        componentType: FormComponentType.DROPDOWN,
                        options: [
                            {
                                displayName: 'Run SQL',
                                key: SqlOperations.RUN_SQL,
                                value: SqlOperations.RUN_SQL
                            },
                            {
                                displayName: 'Insert, update, or delete rows with form',
                                key: SqlOperations.UPDATE_ROWS,
                                value: SqlOperations.UPDATE_ROWS
                            }
                        ],
                        initialValue: SqlOperations.RUN_SQL,
                        display: {
                            show: {
                                operation: [SqlOperations.UPDATE_ROWS]
                            }
                        }
                    },
                    {
                        label: 'Parameters (?, ?, ...)',
                        name: 'parameters',
                        startVersion: MariaDBPluginVersions.V12,
                        ldFlag: 'clark.parameterized-apis.v2.enabled',
                        componentType: FormComponentType.DYNAMIC_INPUT_TEXT,
                        placeholder: '[param1, param2, ...]',
                        tooltip: {
                            markdownText: 'JavaScript expression that evaluates to an array of parameter values. Use `?` placeholders in your SQL query to reference these parameters.'
                        },
                        display: {
                            show: {
                                operation: ['undefined', SqlOperations.RUN_SQL]
                            }
                        }
                    },
                    {
                        label: '', // Query
                        name: 'body',
                        startVersion: MariaDBPluginVersions.V1,
                        componentType: FormComponentType.CODE_EDITOR,
                        language: EditorLanguage.SQL,
                        initialValue: DB_SQL_INITIAL_TEXT,
                        display: {
                            show: {
                                // Frontend is using string equality checks here
                                operation: ['undefined', SqlOperations.RUN_SQL]
                            }
                        }
                    },
                    {
                        label: 'Maria DB schema',
                        name: 'schema',
                        startVersion: MariaDBPluginVersions.V9,
                        componentType: FormComponentType.METADATA_DROPDOWN,
                        placeholder: 'Select a schema',
                        keyAccessor: 'name',
                        valueAccessor: 'name',
                        listAccessor: 'metadata.dbSchema.schemas',
                        displayNameAccessor: 'name',
                        clearDependentFieldsOnChange: ['table'],
                        defaultToFirstOption: true,
                        triggerGetMetadata: true,
                        display: {
                            show: {
                                operation: [SqlOperations.UPDATE_ROWS]
                            }
                        },
                        rules: [{ required: true, message: 'Schema is required' }],
                        showSearch: true,
                        optionFilterProp: 'value'
                    },
                    {
                        label: 'Maria DB table',
                        name: 'table',
                        startVersion: MariaDBPluginVersions.V8,
                        componentType: FormComponentType.METADATA_DROPDOWN,
                        placeholder: 'Select a table',
                        keyAccessor: 'name',
                        valueAccessor: 'name',
                        listAccessor: 'metadata.dbSchema.tables',
                        filterDependency: 'schema',
                        filterFieldName: 'schema',
                        displayNameAccessor: 'name',
                        triggerGetMetadata: true,
                        display: {
                            show: {
                                operation: [SqlOperations.UPDATE_ROWS]
                            }
                        },
                        rules: [{ required: true, message: 'Table is required' }],
                        showSearch: true,
                        optionFilterProp: 'value'
                    }
                ]
            },
            {
                name: 'main2',
                sectionHeader: 'Step 1 - Match rows',
                items: [
                    {
                        label: 'Row matching mode',
                        name: 'useAdvancedMatching',
                        startVersion: MariaDBPluginVersions.V8,
                        componentType: FormComponentType.DROPDOWN,
                        options: [
                            {
                                displayName: 'Automatically match against database table primary key',
                                subText: 'Table must have a primary key. Does not allow updates that modify primary key columns.',
                                key: 'auto',
                                value: 'auto'
                            },
                            {
                                displayName: 'Manually match against any column(s)',
                                subText: 'Use with any column. Allows updates that modify primary key columns.',
                                key: 'advanced',
                                value: 'advanced'
                            }
                        ],
                        initialValue: 'auto',
                        display: {
                            show: {
                                operation: [SqlOperations.UPDATE_ROWS]
                            }
                        }
                    },
                    {
                        label: 'Primary keys',
                        name: 'primaryKeyDisplay',
                        startVersion: MariaDBPluginVersions.V8,
                        componentType: FormComponentType.PRIMARY_KEY_DISPLAY,
                        display: {
                            show: {
                                operation: [SqlOperations.UPDATE_ROWS],
                                useAdvancedMatching: ['undefined', 'auto']
                            }
                        }
                    },
                    // Group
                    {
                        label: 'Array of original rows to be updated',
                        name: 'oldValues',
                        startVersion: MariaDBPluginVersions.V8,
                        componentType: FormComponentType.DYNAMIC_INPUT_TEXT,
                        initialValue: '',
                        subHeading: `E.g. for an editable Table, it will be \`{{Table1.editedRows.originalRows}}\``,
                        placeholder: '{{ [{name: “OriginalBilly”, email: “billy@joel.com”}, {name: “OriginalAlice”, email: “a@alice.com”}] }}',
                        tooltip: {
                            markdownText: `Used to identify the MariaDB rows that will be updated.`
                        },
                        rules: [{ required: true, message: 'Missing required value' }],
                        display: {
                            show: {
                                operation: [SqlOperations.UPDATE_ROWS],
                                useAdvancedMatching: ['advanced']
                            }
                        }
                    },
                    {
                        label: 'MariaDB columns to match on',
                        name: 'filterBy',
                        startVersion: MariaDBPluginVersions.V8,
                        componentType: FormComponentType.FILTER_COLUMNS,
                        tooltip: {
                            markdownText: `Specify which columns are used to find unique matches.`
                        },
                        rules: [{ required: true, message: 'At least one filter column is required' }],
                        display: {
                            show: {
                                operation: [SqlOperations.UPDATE_ROWS],
                                useAdvancedMatching: ['advanced']
                            }
                        }
                    }
                ]
            },
            {
                name: 'updates',
                sectionHeader: 'Step 2 - Update matched rows',
                items: [
                    {
                        label: 'Rows to insert',
                        name: 'insertedRows',
                        startVersion: MariaDBPluginVersions.V10,
                        componentType: FormComponentType.DYNAMIC_INPUT_TEXT,
                        initialValue: '',
                        placeholder: '{{ [{name: “Billy”, email: “billy@joel.com”}, {name: “Alice”, email: “a@alice.com”}] }}',
                        subHeading: `E.g for an editable Table, it will be \`{{Table1.editedRows.insertedRows}}\``,
                        tooltip: {
                            markdownText: 'These rows will be inserted'
                        },
                        display: {
                            show: {
                                operation: [SqlOperations.UPDATE_ROWS]
                            }
                        }
                    },
                    {
                        label: 'Rows to update',
                        name: 'newValues',
                        startVersion: MariaDBPluginVersions.V8,
                        endVersion: MariaDBPluginVersions.V9,
                        componentType: FormComponentType.DYNAMIC_INPUT_TEXT,
                        initialValue: '',
                        placeholder: '{{ [{name: “Billy”, email: “billy@joel.com”}, {name: “Alice”, email: “a@alice.com”}] }}',
                        rules: [{ required: true, message: 'Missing required value' }],
                        subHeading: `E.g for an editable Table, it will be \`{{Table1.editedRows.updatedRows}}\``,
                        tooltip: {
                            markdownText: 'Matched rows from Step 1 will be updated to these values'
                        },
                        display: {
                            show: {
                                operation: [SqlOperations.UPDATE_ROWS]
                            }
                        }
                    },
                    {
                        label: 'Rows to update',
                        name: 'newValues',
                        startVersion: MariaDBPluginVersions.V10,
                        componentType: FormComponentType.DYNAMIC_INPUT_TEXT,
                        initialValue: '',
                        placeholder: '{{ [{name: “Billy”, email: “billy@joel.com”}, {name: “Alice”, email: “a@alice.com”}] }}',
                        subHeading: `E.g for an editable Table, it will be \`{{Table1.editedRows.updatedRows}}\``,
                        tooltip: {
                            markdownText: 'Matched rows from Step 1 will be updated to these values'
                        },
                        display: {
                            show: {
                                operation: [SqlOperations.UPDATE_ROWS]
                            }
                        }
                    },
                    {
                        label: 'Rows to delete',
                        name: 'deletedRows',
                        startVersion: MariaDBPluginVersions.V10,
                        componentType: FormComponentType.DYNAMIC_INPUT_TEXT,
                        initialValue: '',
                        placeholder: '{{ [{name: “Billy”, email: “billy@joel.com”}, {name: “Alice”, email: “a@alice.com”}] }}',
                        subHeading: `E.g for an editable Table, it will be \`{{Table1.editedRows.deletedRows}}\``,
                        tooltip: {
                            markdownText: 'Matched rows from Step 1 will be deleted'
                        },
                        display: {
                            show: {
                                operation: [SqlOperations.UPDATE_ROWS]
                            }
                        }
                    }
                ]
            },
            {
                name: 'preview',
                sectionHeader: 'Step 3 - Match columns',
                items: [
                    {
                        label: 'Map JSON keys to MariaDB columns',
                        name: 'mappingSettingsDisplay',
                        startVersion: MariaDBPluginVersions.V8,
                        componentType: FormComponentType.KEY_MAPPING,
                        tooltip: {
                            markdownText: 'Used to map JSON keys from the previous steps to MariaDB columns'
                        },
                        display: {
                            show: {
                                operation: [SqlOperations.UPDATE_ROWS]
                            }
                        }
                    },
                    {
                        label: 'Preview',
                        name: 'preview',
                        startVersion: MariaDBPluginVersions.V8,
                        endVersion: MariaDBPluginVersions.V9,
                        componentType: FormComponentType.SQL_PREVIEW,
                        display: {
                            show: {
                                operation: [SqlOperations.UPDATE_ROWS]
                            }
                        }
                    },
                    {
                        label: 'Preview',
                        name: 'preview',
                        startVersion: MariaDBPluginVersions.V10,
                        componentType: FormComponentType.SQL_PREVIEW_WITH_INSERT_DELETE,
                        display: {
                            show: {
                                operation: [SqlOperations.UPDATE_ROWS]
                            }
                        }
                    }
                ]
            },
            {
                name: 'advanced:main',
                items: [
                    {
                        label: 'Use parameterized SQL',
                        name: 'usePreparedSql',
                        startVersion: MariaDBPluginVersions.V6,
                        endVersion: MariaDBPluginVersions.V11,
                        componentType: FormComponentType.SWITCH,
                        initialValue: true,
                        tooltip: {
                            markdownText: PARAMETERIZED_SQL_DESCRIPTION
                        },
                        display: {
                            show: {
                                operation: ['undefined', SqlOperations.RUN_SQL]
                            }
                        }
                    },
                    {
                        label: 'Use parameterized SQL',
                        name: 'usePreparedSql',
                        startVersion: MariaDBPluginVersions.V12,
                        componentType: FormComponentType.SWITCH,
                        initialValue: true,
                        tooltip: {
                            markdownText: PARAMETERIZED_SQL_DESCRIPTION
                        },
                        display: {
                            show: {
                                operation: [SqlOperations.RUN_SQL],
                                parameters: ['undefined']
                            }
                        }
                    }
                ]
            }
        ]
    }
};
//# sourceMappingURL=mariadb.js.map