import { translate } from '@stack-spot/portal-translate';
import { errorDictionary } from './error-dictionary.js';
const regex = /^[\w-]+$/;
export async function validateGeneratedConnection(value, input, language, formName, isConnectionInterfaceNameAvailable, generatedConnectionsManager) {
    if (input.type !== 'generated-connection' || !input.connectionInterfaceType)
        return;
    const key = `${formName}.${input.name}`;
    if (!regex.test(value))
        return translate(errorDictionary, language).invalidConnectionNamePattern;
    let isValid = true;
    if (generatedConnectionsManager.getByType(input.connectionInterfaceType).includes(value)) {
        isValid = false;
    }
    else {
        isValid = await isConnectionInterfaceNameAvailable(value);
    }
    if (isValid) {
        generatedConnectionsManager.set(key, input.connectionInterfaceType, value);
    }
    else {
        generatedConnectionsManager.unset(key);
        return translate(errorDictionary, language).alreadyInUse;
    }
}
//# sourceMappingURL=validate-generated-connection.js.map