"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NamedQueryApiImpl = void 0;
const tslib_1 = require("tslib");
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
const StringExtensions_1 = require("../../Utilities/Extensions/StringExtensions");
const NamedQueryInternalApi_1 = require("../Internal/NamedQueryInternalApi");
const ApiBase_1 = require("./ApiBase");
const NamedQueryRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/NamedQueryRedux"));
class NamedQueryApiImpl extends ApiBase_1.ApiBase {
    internalApi;
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new NamedQueryInternalApi_1.NamedQueryInternalApi(_adaptable);
    }
    addNamedQuery(namedQuery) {
        this.dispatchAction(NamedQueryRedux.NamedQueryAdd(namedQuery));
    }
    addNamedQueries(namedQueries) {
        this.dispatchAction(NamedQueryRedux.NamedQueriesAdd(namedQueries));
    }
    getNamedQueryState() {
        return this.getAdaptableState().NamedQuery;
    }
    getNamedQueries() {
        return this.getNamedQueryState().NamedQueries || [];
    }
    getNamedQueryByName(namedQueryName) {
        return this.getNamedQueries().find((se) => se.Name == namedQueryName);
    }
    isValidNamedQuery(namedQuery) {
        if ((0, StringExtensions_1.IsNullOrEmptyOrWhiteSpace)(namedQuery?.Name)) {
            return { valid: false, message: 'A name is required for the Named Query.' };
        }
        // check that there is no other existing named query with the same name
        const duplicate = this.getNamedQueries().find((q) => q.Name === namedQuery.Name && q.Uuid !== namedQuery.Uuid);
        if (duplicate) {
            return {
                valid: false,
                message: 'A Named Query with this name already exists.',
            };
        }
        return { valid: true, message: '' };
    }
    openNamedQuerySettingsPanel() {
        this.showModulePopup(ModuleConstants.NamedQueryModuleId);
    }
    runNamedQuery(namedQuery) {
        this.getGridFilterApi().setGridFilterExpression(namedQuery.Name);
    }
    runQueryByName(queryName) {
        const namedQuery = this.getNamedQueryByName(queryName);
        if (namedQuery) {
            this.runNamedQuery(namedQuery);
        }
    }
}
exports.NamedQueryApiImpl = NamedQueryApiImpl;
