"use strict";
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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RecordsQuery = exports.DateSort = void 0;
const time_js_1 = require("../utils/time.js");
const message_js_1 = require("../core/message.js");
const object_js_1 = require("../utils/object.js");
const auth_js_1 = require("../core/auth.js");
const message_js_2 = require("../core/message.js");
const url_js_1 = require("../utils/url.js");
var DateSort;
(function (DateSort) {
    DateSort["CreatedAscending"] = "createdAscending";
    DateSort["CreatedDescending"] = "createdDescending";
    DateSort["PublishedAscending"] = "publishedAscending";
    DateSort["PublishedDescending"] = "publishedDescending";
})(DateSort = exports.DateSort || (exports.DateSort = {}));
class RecordsQuery extends message_js_1.Message {
    static parse(message) {
        return __awaiter(this, void 0, void 0, function* () {
            if (message.authorization !== undefined) {
                yield (0, auth_js_1.validateAuthorizationIntegrity)(message);
            }
            if (message.descriptor.filter.protocol !== undefined) {
                (0, url_js_1.validateProtocolUrlNormalized)(message.descriptor.filter.protocol);
            }
            if (message.descriptor.filter.schema !== undefined) {
                (0, url_js_1.validateSchemaUrlNormalized)(message.descriptor.filter.schema);
            }
            return new RecordsQuery(message);
        });
    }
    static create(options) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const descriptor = {
                interface: message_js_2.DwnInterfaceName.Records,
                method: message_js_2.DwnMethodName.Query,
                messageTimestamp: (_a = options.messageTimestamp) !== null && _a !== void 0 ? _a : (0, time_js_1.getCurrentTimeInHighPrecision)(),
                filter: RecordsQuery.normalizeFilter(options.filter),
                dateSort: options.dateSort
            };
            // delete all descriptor properties that are `undefined` else the code will encounter the following IPLD issue when attempting to generate CID:
            // Error: `undefined` is not supported by the IPLD Data Model and cannot be encoded
            (0, object_js_1.removeUndefinedProperties)(descriptor);
            // only generate the `authorization` property if signature input is given
            const authorizationSignatureInput = options.authorizationSignatureInput;
            const authorization = authorizationSignatureInput ? yield message_js_1.Message.signAsAuthorization(descriptor, authorizationSignatureInput) : undefined;
            const message = { descriptor, authorization };
            message_js_1.Message.validateJsonSchema(message);
            return new RecordsQuery(message);
        });
    }
    static convertFilter(filter) {
        const filterCopy = Object.assign({}, filter);
        const { dateCreated } = filterCopy;
        let rangeFilter = undefined;
        if (dateCreated !== undefined) {
            if (dateCreated.to !== undefined && dateCreated.from !== undefined) {
                rangeFilter = {
                    gte: dateCreated.from,
                    lt: dateCreated.to,
                };
            }
            else if (dateCreated.to !== undefined) {
                rangeFilter = {
                    lt: dateCreated.to,
                };
            }
            else if (dateCreated.from !== undefined) {
                rangeFilter = {
                    gte: dateCreated.from,
                };
            }
        }
        if (rangeFilter) {
            filterCopy.dateCreated = rangeFilter;
        }
        return filterCopy;
    }
    static normalizeFilter(filter) {
        let protocol;
        if (filter.protocol === undefined) {
            protocol = undefined;
        }
        else {
            protocol = (0, url_js_1.normalizeProtocolUrl)(filter.protocol);
        }
        let schema;
        if (filter.schema === undefined) {
            schema = undefined;
        }
        else {
            schema = (0, url_js_1.normalizeSchemaUrl)(filter.schema);
        }
        return Object.assign(Object.assign({}, filter), { protocol,
            schema });
    }
}
exports.RecordsQuery = RecordsQuery;
//# sourceMappingURL=records-query.js.map