import ObjectFactory from '../../Utilities/ObjectFactory';
import { ApiBase } from '../Implementation/ApiBase';
export class NamedQueryInternalApi extends ApiBase {
    /**
     * Returns the name of all the referenced Named Queries as an array of strings
     * @param expression query string
     */
    getReferencedNamedQueryNames(expression = '') {
        try {
            return this.getAdaptableInternalApi()
                .getQueryLanguageService()
                .getNamedQueryNamesFromExpression(expression);
        }
        catch (error) {
            // defensive programing, this should happen only if someone botched Initial Adaptable State
            this.logWarn(`QueryApi.getReferencedNamedQueryNames received an invalid expression: ${expression}
                  Error: ${error}`);
            return [];
        }
    }
    /**
     * Returns all the references in the AdaptableState of the NamedQuery with the given name
     * @param namedQueryName namedQuery name
     */
    getNamedQueryModuleReferences(namedQueryName) {
        const references = [];
        this.getAdaptableInternalApi()
            .getModules()
            .forEach((module) => {
            if (module
                .getModuleNamedQueryReferences()
                .some((moduleReference) => moduleReference === namedQueryName)) {
                references.push(module.moduleInfo.FriendlyName);
            }
        });
        return references;
    }
    validateDeletedNamedQuery(namedQuery) {
        const namedQueryReferences = this.getNamedQueryModuleReferences(namedQuery);
        if (namedQueryReferences.length) {
            const alert = {
                alertType: 'generic',
                header: 'Named Query',
                message: `This Named Query cannot be deleted. It is still referenced by: ${namedQueryReferences.join(', ')}.`,
                alertDefinition: ObjectFactory.CreateInternalAlertDefinitionForMessages('Error'),
            };
            this.getAlertApi().displayAdaptableAlertNotification(alert);
            return false;
        }
        else {
            return true;
        }
    }
    validateRenamedNamedQuery(namedQuery) {
        const namedQueryReferences = this.getNamedQueryModuleReferences(namedQuery);
        if (namedQueryReferences.length) {
            const alert = {
                alertType: 'generic',
                header: 'Named Query',
                message: `This Named Query cannot be renamed. It is currently referenced by: ${namedQueryReferences.join(', ')}.`,
                alertDefinition: ObjectFactory.CreateInternalAlertDefinitionForMessages('Error'),
            };
            this.getAlertApi().displayAdaptableAlertNotification(alert);
            return false;
        }
        else {
            return true;
        }
    }
}
