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 { makeDropdownItem } from './shared/db.js';
export const LakebasePluginVersions = {
    V1: '0.0.1'
};
// These match the proto enum values
var ConnectionType;
(function (ConnectionType) {
    ConnectionType["USERNAME_PASSWORD"] = "1";
    ConnectionType["OAUTH_M2M"] = "2";
    ConnectionType["OAUTH_TOKEN_FEDERATION"] = "3";
})(ConnectionType || (ConnectionType = {}));
const CONNECTION_METHODS_AND_DISPLAY_NAMES = {
    [ConnectionType.OAUTH_M2M]: 'OAuth Machine-to-Machine',
    [ConnectionType.OAUTH_TOKEN_FEDERATION]: 'OAuth Token Federation',
    [ConnectionType.USERNAME_PASSWORD]: 'Username/Password'
};
const BASE_HOST = {
    label: 'Host',
    name: 'connection.host',
    componentType: FormComponentType.INPUT_TEXT,
    placeholder: 'instance-abc123.database.cloud.databricks.com',
    rules: [{ required: true, message: 'Host is required' }]
};
const BASE_PORT = {
    label: 'Port',
    name: 'connection.port',
    componentType: FormComponentType.INPUT_TEXT,
    dataType: InputDataType.NUMBER,
    initialValue: 5432,
    rules: [{ required: true, message: 'Port is required' }]
};
const BASE_DATABASE_NAME = {
    label: 'Database name',
    name: 'connection.databaseName',
    componentType: FormComponentType.INPUT_TEXT,
    placeholder: 'databricks_postgres',
    rules: [{ required: true, message: 'Database name is required' }]
};
// OAuth M2M fields
const BASE_OAUTH_CLIENT_ID = {
    label: 'Client ID',
    name: 'connection.oauthClientId',
    componentType: FormComponentType.INPUT_TEXT,
    rules: [{ required: true, message: 'Client ID is required' }]
};
const BASE_OAUTH_CLIENT_SECRET = {
    label: 'Client secret',
    name: 'connection.oauthClientSecret',
    componentType: FormComponentType.INPUT_TEXT,
    dataType: InputDataType.PASSWORD,
    rules: [{ required: true, message: 'Client secret is required' }]
};
const BASE_OAUTH_WORKSPACE_URL = {
    label: 'Workspace URL',
    name: 'connection.oauthWorkspaceUrl',
    componentType: FormComponentType.INPUT_TEXT,
    placeholder: 'https://dbc-a1b2345c-d6e7.cloud.databricks.com',
    tooltip: {
        markdownText: 'Databricks workspace URL used for OAuth token endpoint.'
    },
    rules: [{ required: true, message: 'Workspace URL is required for OAuth' }]
};
// OAuth Token Federation fields
const BASE_SUBJECT_TOKEN_SOURCE = {
    label: 'Subject token source',
    name: 'authConfig.subjectTokenSource',
    componentType: FormComponentType.DROPDOWN,
    initialValue: 'SUBJECT_TOKEN_SOURCE_LOGIN_IDENTITY_PROVIDER',
    rules: [{ required: true, message: 'Subject token source is required' }],
    options: [
        makeDropdownItem('SUBJECT_TOKEN_SOURCE_LOGIN_IDENTITY_PROVIDER', 'Login Identity Provider'),
        makeDropdownItem('SUBJECT_TOKEN_SOURCE_STATIC_TOKEN', 'Static Token')
    ]
};
const BASE_TOKEN_URL = {
    label: 'Token URL',
    name: 'authConfig.tokenUrl',
    componentType: FormComponentType.INPUT_TEXT,
    placeholder: 'https://your-databricks-instance.cloud.databricks.com/oidc/v1/token',
    rules: [{ required: true, message: 'Token URL is required' }]
};
const BASE_STATIC_TOKEN = {
    label: 'Static Token',
    name: 'authConfig.subjectTokenSourceStaticToken',
    componentType: FormComponentType.INPUT_TEXT,
    dataType: InputDataType.PASSWORD,
    rules: [{ required: true, message: 'Static token is required when using static token source' }]
};
// Username/Password fields
const BASE_USERNAME = {
    label: 'Username',
    name: 'connection.username',
    componentType: FormComponentType.INPUT_TEXT,
    rules: [{ required: true, message: 'Username is required' }]
};
const BASE_PASSWORD = {
    label: 'Password',
    name: 'connection.password',
    componentType: FormComponentType.INPUT_TEXT,
    dataType: InputDataType.PASSWORD,
    rules: [{ required: true, message: 'Password is required' }]
};
export const LakebasePlugin = {
    id: 'lakebase',
    name: 'Databricks Lakebase',
    moduleName: 'LakebasePlugin',
    modulePath: 'plugins/lakebase/LakebasePlugin',
    iconLocation: `${PUBLIC_INTEGRATIONS_LOGO_URL}/databricks.png`,
    docsUrl: `${DOCS_BASE_URL}/lakebase`,
    type: PluginType.DB,
    responseType: PluginResponseType.TABLE,
    hasRawRequest: true,
    hasMetadata: true,
    rawRequestName: 'Executed SQL',
    datasourceTemplate: {
        sections: [
            {
                name: 'main',
                items: [
                    // Display name
                    {
                        label: 'Display name',
                        name: 'name',
                        startVersion: LakebasePluginVersions.V1,
                        componentType: FormComponentType.INPUT_TEXT,
                        placeholder: 'Lakebase Production',
                        rules: [{ required: true, message: 'Display name is required' }]
                    },
                    // Hidden fields for OAuth Token Exchange integration
                    {
                        label: 'Auth Type Field',
                        name: 'authTypeField',
                        hidden: true,
                        componentType: FormComponentType.INPUT_TEXT,
                        startVersion: LakebasePluginVersions.V1,
                        initialValue: 'oauthType',
                        rules: [{ required: false }],
                        display: {
                            show: {
                                'connection.connectionType': [ConnectionType.OAUTH_TOKEN_FEDERATION]
                            }
                        }
                    },
                    {
                        label: 'OAuth Type',
                        name: 'oauthType',
                        hidden: true,
                        componentType: FormComponentType.INPUT_TEXT,
                        startVersion: LakebasePluginVersions.V1,
                        initialValue: 'oauth-token-exchange',
                        rules: [{ required: false }],
                        display: {
                            show: {
                                'connection.connectionType': [ConnectionType.OAUTH_TOKEN_FEDERATION]
                            }
                        }
                    },
                    {
                        label: 'Subject Token Type',
                        name: 'authConfig.subjectTokenType',
                        hidden: true,
                        componentType: FormComponentType.INPUT_TEXT,
                        startVersion: LakebasePluginVersions.V1,
                        initialValue: 'urn:ietf:params:oauth:token-type:jwt',
                        rules: [{ required: false }],
                        display: {
                            show: {
                                'connection.connectionType': [ConnectionType.OAUTH_TOKEN_FEDERATION]
                            }
                        }
                    },
                    {
                        label: 'Scope',
                        name: 'authConfig.scope',
                        hidden: true,
                        componentType: FormComponentType.INPUT_TEXT,
                        startVersion: LakebasePluginVersions.V1,
                        initialValue: 'all-apis',
                        rules: [{ required: false }],
                        display: {
                            show: {
                                'connection.connectionType': [ConnectionType.OAUTH_TOKEN_FEDERATION]
                            }
                        }
                    },
                    // Connection fields - always shown
                    {
                        ...BASE_HOST,
                        startVersion: LakebasePluginVersions.V1
                    },
                    {
                        ...BASE_PORT,
                        startVersion: LakebasePluginVersions.V1
                    },
                    {
                        ...BASE_DATABASE_NAME,
                        startVersion: LakebasePluginVersions.V1
                    },
                    // Authentication dropdown
                    {
                        label: 'Authentication',
                        name: 'connection.connectionType',
                        startVersion: LakebasePluginVersions.V1,
                        componentType: FormComponentType.DROPDOWN,
                        initialValue: ConnectionType.OAUTH_M2M,
                        rules: [{ required: true }],
                        options: Object.entries(CONNECTION_METHODS_AND_DISPLAY_NAMES).map(([value, displayName]) => makeDropdownItem(value, displayName))
                    },
                    // OAuth M2M fields
                    {
                        ...BASE_OAUTH_WORKSPACE_URL,
                        startVersion: LakebasePluginVersions.V1,
                        display: {
                            show: {
                                'connection.connectionType': [ConnectionType.OAUTH_M2M]
                            }
                        }
                    },
                    {
                        ...BASE_OAUTH_CLIENT_ID,
                        startVersion: LakebasePluginVersions.V1,
                        display: {
                            show: {
                                'connection.connectionType': [ConnectionType.OAUTH_M2M]
                            }
                        }
                    },
                    {
                        ...BASE_OAUTH_CLIENT_SECRET,
                        startVersion: LakebasePluginVersions.V1,
                        display: {
                            show: {
                                'connection.connectionType': [ConnectionType.OAUTH_M2M]
                            }
                        }
                    },
                    // OAuth Token Federation fields
                    {
                        ...BASE_SUBJECT_TOKEN_SOURCE,
                        startVersion: LakebasePluginVersions.V1,
                        display: {
                            show: {
                                'connection.connectionType': [ConnectionType.OAUTH_TOKEN_FEDERATION]
                            }
                        }
                    },
                    {
                        ...BASE_STATIC_TOKEN,
                        startVersion: LakebasePluginVersions.V1,
                        display: {
                            show: {
                                'connection.connectionType': [ConnectionType.OAUTH_TOKEN_FEDERATION],
                                'authConfig.subjectTokenSource': ['SUBJECT_TOKEN_SOURCE_STATIC_TOKEN']
                            }
                        }
                    },
                    {
                        ...BASE_TOKEN_URL,
                        startVersion: LakebasePluginVersions.V1,
                        display: {
                            show: {
                                'connection.connectionType': [ConnectionType.OAUTH_TOKEN_FEDERATION]
                            }
                        }
                    },
                    // Username/Password fields
                    {
                        ...BASE_USERNAME,
                        startVersion: LakebasePluginVersions.V1,
                        display: {
                            show: {
                                'connection.connectionType': [ConnectionType.USERNAME_PASSWORD]
                            }
                        }
                    },
                    {
                        ...BASE_PASSWORD,
                        startVersion: LakebasePluginVersions.V1,
                        display: {
                            show: {
                                'connection.connectionType': [ConnectionType.USERNAME_PASSWORD]
                            }
                        }
                    }
                ]
            }
        ]
    },
    actionTemplate: {
        sections: [
            {
                name: 'main',
                items: [
                    {
                        label: 'Parameters ($1, $2, ...)',
                        name: 'parameters',
                        startVersion: LakebasePluginVersions.V1,
                        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 `$1`, `$2`, etc. in your SQL query to reference these parameters.'
                        }
                    },
                    {
                        label: '', // Query
                        name: 'body',
                        startVersion: LakebasePluginVersions.V1,
                        componentType: FormComponentType.CODE_EDITOR,
                        language: EditorLanguage.SQL,
                        initialValue: DB_SQL_INITIAL_TEXT
                    }
                ]
            },
            {
                name: 'advanced:main',
                items: [
                    {
                        label: 'Use parameterized SQL',
                        name: 'usePreparedSql',
                        startVersion: LakebasePluginVersions.V1,
                        componentType: FormComponentType.SWITCH,
                        initialValue: true,
                        tooltip: {
                            markdownText: PARAMETERIZED_SQL_DESCRIPTION
                        },
                        display: {
                            show: {
                                parameters: ['undefined']
                            }
                        }
                    }
                ]
            }
        ]
    }
};
//# sourceMappingURL=lakebase.js.map