"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSearchProvider = void 0;
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var MainSearchProvider = /** @class */ (function () {
    function MainSearchProvider(_a) {
        var _b = _a.data, query = _b.query, sortField = _b.sortField, sortOrder = _b.sortOrder, tenant = _a.tenant, searchTenant = _a.searchTenant, apiPath = _a.apiPath, dtssPath = _a.dtssPath, globalSearchRequestOptions = _a.globalSearchRequestOptions;
        var _this = this;
        this.isDT = function () { return _this.tenant !== _this.searchTenant; };
        this.getQueryFilter = function () { return ({ rawFilter: _this.query }); };
        this.getEntities = function (filters, options) {
            if (filters === void 0) { filters = []; }
            if (options === void 0) { options = {}; }
            var requestFilters = __spreadArray([_this.getQueryFilter()], filters, true);
            var requestOptions = __assign(__assign(__assign({}, _this.globalSearchRequestOptions), { sort: _this.sortField, order: _this.sortOrder, select: 'uri,label,type', scoreEnabled: false }), options);
            return _this.isDT()
                ? (0, mdm_sdk_1.getFilteredEntitiesFromDataTenant)({
                    filters: requestFilters,
                    options: requestOptions,
                    customerTenant: _this.tenant,
                    dataTenant: _this.searchTenant,
                    dtssPath: _this.dtssPath
                })
                : (0, mdm_sdk_1.getFilteredEntities)({ filters: requestFilters, options: requestOptions });
        };
        this.checkEntity = function (uri) {
            return _this.getEntities([
                {
                    filter: 'equals',
                    fieldName: 'uri',
                    values: [uri]
                }
            ], { offset: 0, max: 1 }).then(function (entities) {
                return entities && entities.length > 0 && entities[0].uri === uri;
            });
        };
        this.requestEntities = function (max, offset) {
            return _this.getEntities([], { offset: offset, max: max });
        };
        this.requestTotal = function () {
            var filters = [_this.getQueryFilter()];
            var options = _this.globalSearchRequestOptions;
            return (_this.isDT()
                ? (0, mdm_sdk_1.getTotalFromDataTenant)({
                    filters: filters,
                    options: options,
                    customerTenant: _this.tenant,
                    dataTenant: _this.searchTenant,
                    dtssPath: _this.dtssPath
                })
                : (0, mdm_sdk_1.getTotals)({ filters: filters, options: options })).then(function (json) { return json.total; });
        };
        this.query = query;
        this.sortField = sortField;
        this.sortOrder = sortOrder;
        this.tenant = tenant;
        this.searchTenant = searchTenant;
        this.apiPath = apiPath;
        this.dtssPath = dtssPath;
        this.globalSearchRequestOptions = globalSearchRequestOptions;
    }
    return MainSearchProvider;
}());
var GraphSearchProvider = /** @class */ (function () {
    function GraphSearchProvider(_a) {
        var data = _a.data, tenant = _a.tenant, apiPath = _a.apiPath;
        var _this = this;
        this.getEntities = function (filters, options) {
            if (filters === void 0) { filters = []; }
            if (options === void 0) { options = {}; }
            var allFilters = __spreadArray([
                {
                    rawFilter: _this.searchBody.filter
                }
            ], filters, true);
            return (0, mdm_sdk_1.getGraphSearchEntities)({
                apiPath: _this.apiPath,
                tenant: _this.tenant,
                searchBody: __assign(__assign({}, _this.searchBody), { filter: filters.length ? (0, mdm_sdk_1.buildFilterQueryString)()(allFilters) : _this.searchBody.filter }),
                options: options
            });
        };
        this.checkEntity = function (uri) {
            return _this.getEntities([
                {
                    filter: 'equals',
                    fieldName: 'id',
                    values: [(0, mdm_sdk_1.getEntityId)({ uri: uri })]
                }
            ], { from: 0, max: 1 }).then(function (entities) {
                return entities && entities.length > 0 && entities[0].uri === uri;
            });
        };
        this.requestEntities = function (max, from) {
            return _this.getEntities([], { from: from, max: max });
        };
        this.requestTotal = function () {
            return (0, mdm_sdk_1.getGraphSearchCount)({
                apiPath: _this.apiPath,
                tenant: _this.tenant,
                searchBody: _this.searchBody
            }).then(function (json) { return json.count; });
        };
        this.searchBody = data;
        this.tenant = tenant;
        this.apiPath = apiPath;
    }
    return GraphSearchProvider;
}());
var getSearchProvider = function (type, options) {
    switch (type) {
        case 'search':
            return new MainSearchProvider(options);
        case 'graphsearch':
            return new GraphSearchProvider(options);
    }
};
exports.getSearchProvider = getSearchProvider;
