"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NamedQueryInternalApi = void 0;
const tslib_1 = require("tslib");
const ObjectFactory_1 = tslib_1.__importDefault(require("../../Utilities/ObjectFactory"));
const ApiBase_1 = require("../Implementation/ApiBase");
class NamedQueryInternalApi extends ApiBase_1.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 could not be deleted',
                message: `It is still referenced in the following modules: ${namedQueryReferences.join(', ')}`,
                alertDefinition: ObjectFactory_1.default.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 could not be renamed',
                message: `It is currently referenced in the following modules: ${namedQueryReferences.join(', ')}`,
                alertDefinition: ObjectFactory_1.default.CreateInternalAlertDefinitionForMessages('Error'),
            };
            this.getAlertApi().displayAdaptableAlertNotification(alert);
            return false;
        }
        else {
            return true;
        }
    }
}
exports.NamedQueryInternalApi = NamedQueryInternalApi;
