var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { DEFINITIONS } from "../constants";
import { ComparisonOperator, CompositeFilterOperator, CompositeQueryFilter, DefinitionQueryFilter, FilterDataType, PropertyQueryFilter, RelationQueryFilter, } from "../contracts/querying/filters";
import { Query } from "../contracts/querying/query";
import { InvalidOperationError } from "../errors/invalid-operation-error";
import Guard from "../guard";
import { ResponseHandler } from "../clients/response-handler";
export class SettingsClient {
    constructor(client) {
        Guard.notNull(client);
        this._client = client;
    }
    getCategoryIdAsync(name) {
        return __awaiter(this, void 0, void 0, function* () {
            Guard.stringNotNullOrEmpty(name);
            // Build query
            const query = new Query();
            query.filter = new CompositeQueryFilter({
                combineMethod: CompositeFilterOperator.And,
                children: [
                    new DefinitionQueryFilter({
                        name: DEFINITIONS["SettingsCategory"].definitionName,
                        operator: ComparisonOperator.Equals,
                    }),
                    new PropertyQueryFilter({
                        operator: ComparisonOperator.Contains,
                        dataType: FilterDataType.String,
                        property: DEFINITIONS["SettingsCategory"].name,
                        value: name,
                    }),
                ],
            });
            // Get the category
            const result = yield this._client.querying.queryIdsAsync(query);
            let id = null;
            if (!result.items || !result.items.length) {
                return null;
            }
            else if (result.items.length === 1) {
                id = result.items[0];
                return id;
            }
            else {
                throw new InvalidOperationError("Can't have more than one result, must be unique");
            }
        });
    }
    getSettingAsync(category_1, name_1) {
        return __awaiter(this, arguments, void 0, function* (category, name, loadConfiguration = null) {
            var _a;
            Guard.stringNotNullOrEmpty(category);
            Guard.stringNotNullOrEmpty(name);
            // Get the category id
            const categoryId = yield this.getCategoryIdAsync(category);
            if (categoryId == null) {
                return null;
            }
            // Build query
            const query = new Query();
            query.filter = new CompositeQueryFilter({
                combineMethod: CompositeFilterOperator.And,
                children: [
                    new DefinitionQueryFilter({
                        name: DEFINITIONS["Setting"].definitionName,
                        operator: ComparisonOperator.Equals,
                    }),
                    new PropertyQueryFilter({
                        operator: ComparisonOperator.Contains,
                        dataType: FilterDataType.String,
                        property: DEFINITIONS["Setting"].name,
                        value: name,
                    }),
                    new RelationQueryFilter({
                        relation: DEFINITIONS["Setting"].settingCategoryToSettings,
                        parentId: categoryId,
                    }),
                ],
            });
            // Get the setting
            const result = yield this._client.querying.queryAsync(query, loadConfiguration);
            return (_a = result.items[0]) !== null && _a !== void 0 ? _a : null;
        });
    }
    getSettingValueAndSchemaAsync(category, name) {
        return __awaiter(this, void 0, void 0, function* () {
            Guard.stringNotNullOrEmpty(category);
            Guard.stringNotNullOrEmpty(name);
            const link = yield this._client.linkHelper.settingByNameLink(category, name);
            // Get the setting
            const result = yield this._client.raw.getAsync(link.href);
            ResponseHandler.handleErrors(result);
            return result.content;
        });
    }
    getSettingsForCategoryAsync(category_1) {
        return __awaiter(this, arguments, void 0, function* (category, loadConfiguration = null) {
            Guard.stringNotNullOrEmpty(category);
            // Get the category id
            const categoryId = yield this.getCategoryIdAsync(category);
            if (categoryId == null) {
                throw new InvalidOperationError("categoryId was not found");
            }
            // Build query
            const query = new Query();
            query.filter = new CompositeQueryFilter({
                combineMethod: CompositeFilterOperator.And,
                children: [
                    new DefinitionQueryFilter({
                        name: DEFINITIONS["Setting"].definitionName,
                        operator: ComparisonOperator.Equals,
                    }),
                    new RelationQueryFilter({
                        relation: DEFINITIONS["Setting"].settingCategoryToSettings,
                        parentId: categoryId,
                    }),
                ],
            });
            // Get the settings
            const result = yield this._client.querying.queryAsync(query, loadConfiguration);
            return result.items;
        });
    }
}
//# sourceMappingURL=settings-client.js.map