import fromPairs from 'lodash/fromPairs.js';
const migrate = async (context, projectSchema) => {
    const { decryptFn, encryptFn } = context;
    const migratedServices = [];
    for (const [serviceKey, serviceConfig] of Object.entries(projectSchema.services ?? {})) {
        const { authenticationType, authentication } = serviceConfig;
        // Migrate authentication, ensuring now required properties
        if (authentication && authenticationType !== 'unknown' && authenticationType !== 'none') {
            const decrypted = decryptFn(authentication);
            if (!decrypted) {
                throw new Error(`Service "${serviceKey}" authentication could not be decrypted. Please contact support.`);
            }
            let migrated;
            if (authenticationType === 'searchParams') {
                migrated = {
                    params: decrypted,
                    type: authenticationType
                };
            }
            if (authenticationType === 'basic') {
                migrated = {
                    username: '',
                    password: '',
                    ...decrypted,
                    type: authenticationType
                };
            }
            if (authenticationType === 'bearer') {
                migrated = {
                    token: '',
                    ...decrypted,
                    type: authenticationType
                };
            }
            if (authenticationType === 'oauth2Bearer') {
                migrated = {
                    token: '',
                    ...decrypted,
                    type: authenticationType
                };
            }
            if (authenticationType === 'oauth2') {
                migrated = {
                    grantType: 'clientCredentials',
                    clientId: '',
                    ...decrypted,
                    type: authenticationType
                };
            }
            if (authenticationType === 'aws') {
                migrated = {
                    awsAccessKeyId: '',
                    awsSecretAccessKey: '',
                    ...decrypted,
                    type: authenticationType
                };
            }
            if (authenticationType === 'custom') {
                migrated = {
                    ...decrypted,
                    type: authenticationType
                };
            }
            if (migrated) {
                const migratedServiceConfig = {
                    ...serviceConfig,
                    authentication: encryptFn(migrated)
                };
                migratedServices.push([serviceKey, migratedServiceConfig]);
            }
            else {
                migratedServices.push([serviceKey, serviceConfig]);
            }
        }
        else {
            migratedServices.push([serviceKey, serviceConfig]);
        }
    }
    return {
        ...projectSchema,
        services: fromPairs(migratedServices),
        schemaVersion: '3.11.0'
    };
};
export default migrate;
