import { isSearchParamsAuthentication, isServiceAuthentication } from "../../types/index.js";
const migrate = async ({ decryptFn, encryptFn }, projectSchema) => {
    /**
     * Move unencrypted searchParams out of the authentication object.
     */
    for (const serviceConfig of Object.values(projectSchema.services ?? {})) {
        if (serviceConfig.authentication) {
            const authentication = decryptFn(serviceConfig.authentication);
            if (isServiceAuthentication(authentication) && isSearchParamsAuthentication(authentication)) {
                if (serviceConfig.provider === 'judgeMe') {
                    const shopDomain = authentication.params.find((param) => param.name === 'shop_domain');
                    if (shopDomain) {
                        serviceConfig.options = {
                            ...serviceConfig.options,
                            searchParams: {
                                shop_domain: shopDomain.value
                            }
                        };
                        serviceConfig.options.shopDomain = undefined;
                    }
                    authentication.params = authentication.params.filter((param) => param.name !== 'shop_domain');
                }
                if (serviceConfig.provider === 'reviewsIo') {
                    const storeId = authentication.params.find((param) => param.name === 'store');
                    if (storeId) {
                        serviceConfig.options = {
                            ...serviceConfig.options,
                            searchParams: {
                                store: storeId.value
                            }
                        };
                        serviceConfig.options.storeId = undefined;
                    }
                    authentication.params = authentication.params.filter((param) => param.name !== 'store');
                }
                serviceConfig.authentication = encryptFn(authentication);
            }
        }
    }
    return {
        ...projectSchema,
        schemaVersion: '3.39.0'
    };
};
export default migrate;
