"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BigQueryPlugin = void 0;
const index_js_1 = require("../../types/index.js");
const constants_js_1 = require("./constants.js");
const db_js_1 = require("./shared/db.js");
const BigQueryPluginVersions = {
    V1: '0.0.1',
    V2: '0.0.2',
    V6: '0.0.6',
    V7: '0.0.7',
    V8: '0.0.8',
    V9: '0.0.9'
};
var ConnectionType;
(function (ConnectionType) {
    ConnectionType["SERVICE_ACCOUNT_KEY"] = "service-account-key";
    ConnectionType["WORKFORCE_IDENTITY_FEDERATION"] = "oauth-token-exchange";
})(ConnectionType || (ConnectionType = {}));
const CONNECTION_METHODS_AND_DISPLAY_NAMES = {
    [ConnectionType.SERVICE_ACCOUNT_KEY]: 'Service Account Key',
    [ConnectionType.WORKFORCE_IDENTITY_FEDERATION]: 'Workforce Identity Federation'
};
const BASE_SUBJECT_TOKEN_SOURCE = {
    label: 'Subject token source',
    name: 'authConfig.subjectTokenSource',
    componentType: index_js_1.FormComponentType.DROPDOWN,
    initialValue: 'SUBJECT_TOKEN_SOURCE_LOGIN_IDENTITY_PROVIDER',
    rules: [{ required: true, message: 'Subject token source is required' }],
    options: [
        (0, db_js_1.makeDropdownItem)('SUBJECT_TOKEN_SOURCE_LOGIN_IDENTITY_PROVIDER', 'Login Identity Provider'),
        (0, db_js_1.makeDropdownItem)('SUBJECT_TOKEN_SOURCE_STATIC_TOKEN', 'Static Token')
    ]
};
const BASE_STATIC_TOKEN = {
    label: 'Subject token',
    name: 'authConfig.subjectTokenSourceStaticToken',
    componentType: index_js_1.FormComponentType.INPUT_TEXT,
    dataType: index_js_1.InputDataType.PASSWORD,
    rules: [{ required: true, message: 'Static token is required when using static token source' }]
};
const BASE_WORKFORCE_POOL_ID = {
    label: 'Workforce Pool ID',
    name: 'authConfig.workforcePoolId',
    componentType: index_js_1.FormComponentType.INPUT_TEXT,
    placeholder: 'my-workforce-pool',
    rules: [{ required: true, message: 'Workforce Pool ID is required' }],
    tooltip: {
        markdownText: 'The workforce identity pool ID (POOL_ID)'
    }
};
const BASE_WORKFORCE_PROVIDER_ID = {
    label: 'Workforce Provider ID',
    name: 'authConfig.workforceProviderId',
    componentType: index_js_1.FormComponentType.INPUT_TEXT,
    placeholder: 'my-provider',
    rules: [{ required: true, message: 'Workforce Provider ID is required' }],
    tooltip: {
        markdownText: 'The workforce identity pool provider ID (PROVIDER_ID)'
    }
};
const BASE_PROJECT_ID = {
    label: 'Project ID',
    name: 'authConfig.projectId',
    componentType: index_js_1.FormComponentType.INPUT_TEXT,
    placeholder: 'my-gcp-project',
    rules: [{ required: true, message: 'Project ID is required' }],
    tooltip: {
        markdownText: 'The GCP project ID where BigQuery queries will run'
    }
};
const BASE_BILLING_PROJECT_NUMBER = {
    label: 'Billing Project Number',
    name: 'authConfig.billingProjectNumber',
    componentType: index_js_1.FormComponentType.INPUT_TEXT,
    placeholder: '123456789012',
    rules: [{ required: false }],
    tooltip: {
        markdownText: 'Optional. The project number used for quota attribution during token exchange'
    }
};
const HIDDEN_TOKEN_URL = {
    label: 'Token URL',
    name: 'authConfig.tokenUrl',
    componentType: index_js_1.FormComponentType.INPUT_TEXT,
    hidden: true,
    initialValue: 'https://sts.googleapis.com/v1/token',
    rules: [{ required: false }]
};
const HIDDEN_SCOPE = {
    label: 'Scope',
    name: 'authConfig.scope',
    componentType: index_js_1.FormComponentType.INPUT_TEXT,
    hidden: true,
    initialValue: 'https://www.googleapis.com/auth/bigquery',
    rules: [{ required: false }]
};
const HIDDEN_SUBJECT_TOKEN_TYPE = {
    label: 'Subject token type',
    name: 'authConfig.subjectTokenType',
    componentType: index_js_1.FormComponentType.INPUT_TEXT,
    hidden: true,
    initialValue: 'urn:ietf:params:oauth:token-type:id_token',
    rules: [{ required: false }]
};
const COMPUTED_AUDIENCE = {
    label: 'Audience',
    name: 'authConfig.audience',
    componentType: index_js_1.FormComponentType.INPUT_TEXT,
    hidden: true,
    rules: [{ required: false }]
};
exports.BigQueryPlugin = {
    id: 'bigquery',
    name: 'Google BigQuery',
    moduleName: 'BigqueryPlugin',
    modulePath: 'plugins/bigquery/BigqueryPlugin',
    iconLocation: `${constants_js_1.PUBLIC_INTEGRATIONS_LOGO_URL}/bigquery.png`,
    docsUrl: `${constants_js_1.DOCS_BASE_URL}/google-bigquery`,
    type: index_js_1.PluginType.DB,
    responseType: index_js_1.PluginResponseType.TABLE,
    hasRawRequest: true,
    hasMetadata: true,
    rawRequestName: 'Executed SQL',
    datasourceTemplate: {
        sections: [
            {
                name: 'main',
                items: [
                    {
                        label: 'Display name',
                        name: 'name',
                        startVersion: BigQueryPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.INPUT_TEXT,
                        placeholder: 'BigQuery Production',
                        rules: [{ required: true, message: 'Display name is required' }]
                    },
                    {
                        label: 'Auth Type Field',
                        name: 'authTypeField',
                        hidden: true,
                        componentType: index_js_1.FormComponentType.INPUT_TEXT,
                        startVersion: BigQueryPluginVersions.V9,
                        initialValue: 'connectionType',
                        rules: [{ required: false }]
                    },
                    {
                        label: 'Authentication method',
                        name: 'connectionType',
                        startVersion: BigQueryPluginVersions.V9,
                        componentType: index_js_1.FormComponentType.DROPDOWN,
                        initialValue: ConnectionType.SERVICE_ACCOUNT_KEY,
                        rules: [{ required: true }],
                        options: Object.entries(CONNECTION_METHODS_AND_DISPLAY_NAMES).map(([value, displayName]) => (0, db_js_1.makeDropdownItem)(value, displayName))
                    },
                    {
                        label: 'Service account key',
                        name: 'authentication.custom.googleServiceAccount.value',
                        startVersion: BigQueryPluginVersions.V1,
                        endVersion: BigQueryPluginVersions.V8,
                        // INPUT_AREA is broken (cannot be udpated after unfocus). Using
                        // code editor for now.
                        componentType: index_js_1.FormComponentType.CODE_EDITOR,
                        language: index_js_1.EditorLanguage.JSON,
                        placeholder: constants_js_1.SERVICE_ACCOUNT_GHOST_TEXT,
                        rules: [{ required: true, message: 'Service account key is required' }]
                    },
                    {
                        label: 'Service account key',
                        name: 'authentication.custom.googleServiceAccount.value',
                        startVersion: BigQueryPluginVersions.V9,
                        componentType: index_js_1.FormComponentType.CODE_EDITOR,
                        language: index_js_1.EditorLanguage.JSON,
                        placeholder: constants_js_1.SERVICE_ACCOUNT_GHOST_TEXT,
                        rules: [{ required: true, message: 'Service account key is required' }],
                        display: {
                            show: {
                                connectionType: [ConnectionType.SERVICE_ACCOUNT_KEY]
                            }
                        }
                    },
                    {
                        ...BASE_SUBJECT_TOKEN_SOURCE,
                        startVersion: BigQueryPluginVersions.V9,
                        display: {
                            show: {
                                connectionType: [ConnectionType.WORKFORCE_IDENTITY_FEDERATION]
                            }
                        }
                    },
                    {
                        ...BASE_STATIC_TOKEN,
                        startVersion: BigQueryPluginVersions.V9,
                        display: {
                            show: {
                                connectionType: [ConnectionType.WORKFORCE_IDENTITY_FEDERATION],
                                'authConfig.subjectTokenSource': ['SUBJECT_TOKEN_SOURCE_STATIC_TOKEN']
                            }
                        }
                    },
                    {
                        ...BASE_WORKFORCE_POOL_ID,
                        startVersion: BigQueryPluginVersions.V9,
                        display: {
                            show: {
                                connectionType: [ConnectionType.WORKFORCE_IDENTITY_FEDERATION]
                            }
                        }
                    },
                    {
                        ...BASE_WORKFORCE_PROVIDER_ID,
                        startVersion: BigQueryPluginVersions.V9,
                        display: {
                            show: {
                                connectionType: [ConnectionType.WORKFORCE_IDENTITY_FEDERATION]
                            }
                        }
                    },
                    {
                        ...BASE_PROJECT_ID,
                        startVersion: BigQueryPluginVersions.V9,
                        display: {
                            show: {
                                connectionType: [ConnectionType.WORKFORCE_IDENTITY_FEDERATION]
                            }
                        }
                    },
                    {
                        ...BASE_BILLING_PROJECT_NUMBER,
                        startVersion: BigQueryPluginVersions.V9,
                        display: {
                            show: {
                                connectionType: [ConnectionType.WORKFORCE_IDENTITY_FEDERATION]
                            }
                        }
                    },
                    {
                        ...HIDDEN_TOKEN_URL,
                        startVersion: BigQueryPluginVersions.V9,
                        display: {
                            show: {
                                connectionType: [ConnectionType.WORKFORCE_IDENTITY_FEDERATION]
                            }
                        }
                    },
                    {
                        ...HIDDEN_SCOPE,
                        startVersion: BigQueryPluginVersions.V9,
                        display: {
                            show: {
                                connectionType: [ConnectionType.WORKFORCE_IDENTITY_FEDERATION]
                            }
                        }
                    },
                    {
                        ...HIDDEN_SUBJECT_TOKEN_TYPE,
                        startVersion: BigQueryPluginVersions.V9,
                        display: {
                            show: {
                                connectionType: [ConnectionType.WORKFORCE_IDENTITY_FEDERATION]
                            }
                        }
                    },
                    {
                        ...COMPUTED_AUDIENCE,
                        startVersion: BigQueryPluginVersions.V9,
                        display: {
                            show: {
                                connectionType: [ConnectionType.WORKFORCE_IDENTITY_FEDERATION]
                            }
                        }
                    }
                ]
            }
        ]
    },
    actionTemplate: {
        sections: [
            {
                name: 'main',
                items: [
                    {
                        label: 'Parameters (?, ?, ...)',
                        name: 'parameters',
                        startVersion: BigQueryPluginVersions.V8,
                        ldFlag: 'clark.parameterized-apis.v2.enabled',
                        componentType: index_js_1.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 positionally.'
                        }
                    },
                    {
                        label: '', // Query
                        name: 'body',
                        startVersion: BigQueryPluginVersions.V1,
                        componentType: index_js_1.FormComponentType.CODE_EDITOR,
                        language: index_js_1.EditorLanguage.SQL,
                        initialValue: constants_js_1.SQL_INITIAL_TEXT
                    }
                ]
            },
            {
                name: 'advanced:main',
                items: [
                    {
                        label: 'Use parameterized SQL',
                        name: 'usePreparedSql',
                        startVersion: BigQueryPluginVersions.V6,
                        endVersion: BigQueryPluginVersions.V7,
                        componentType: index_js_1.FormComponentType.SWITCH,
                        initialValue: true,
                        tooltip: {
                            markdownText: constants_js_1.PARAMETERIZED_SQL_DESCRIPTION
                        }
                    },
                    {
                        label: 'Use parameterized SQL',
                        name: 'usePreparedSql',
                        startVersion: BigQueryPluginVersions.V8,
                        componentType: index_js_1.FormComponentType.SWITCH,
                        initialValue: true,
                        tooltip: {
                            markdownText: constants_js_1.PARAMETERIZED_SQL_DESCRIPTION
                        },
                        display: {
                            show: {
                                parameters: ['undefined']
                            }
                        }
                    }
                ]
            }
        ]
    }
};
//# sourceMappingURL=bigquery.js.map