import { isRecord, visit } from '@takeshape/util';
// copied in case we update these in the latest version
function isBasicResolver(resolver) {
    return isRecord(resolver) && typeof resolver.name === 'string';
}
function isComposeResolver(resolver) {
    return Boolean(isRecord(resolver) &&
        Array.isArray(resolver.compose) &&
        resolver.compose.length &&
        resolver.compose.every(isBasicResolver));
}
function renameResolver(resolver) {
    // Replace ai:generateText with ai:chat unless history was disabled
    if (resolver.name === 'ai:generateText' && (resolver.options?.history ?? true)) {
        resolver.name = 'ai:chat';
    }
}
const migrate = async (_, projectSchema) => {
    /**
     * Function will visit each resolver in the schema and mutate them.
     */
    visit(projectSchema, ['resolver', '@resolver'], (maybeResolver) => {
        if (isComposeResolver(maybeResolver)) {
            maybeResolver.compose.forEach(renameResolver);
        }
        else if (isBasicResolver(maybeResolver)) {
            renameResolver(maybeResolver);
        }
    });
    return {
        ...projectSchema,
        schemaVersion: '3.46.0'
    };
};
export default migrate;
