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 { Message } from '../core/message.js';
import { Records } from '../utils/records.js';
import { removeUndefinedProperties } from '../utils/object.js';
import { Time } from '../utils/time.js';
import { validateMessageSignatureIntegrity } from '../core/auth.js';
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
import { DwnInterfaceName, DwnMethodName } from '../core/message.js';
import { validateProtocolUrlNormalized, validateSchemaUrlNormalized } from '../utils/url.js';
export var DateSort;
(function (DateSort) {
    DateSort["CreatedAscending"] = "createdAscending";
    DateSort["CreatedDescending"] = "createdDescending";
    DateSort["PublishedAscending"] = "publishedAscending";
    DateSort["PublishedDescending"] = "publishedDescending";
})(DateSort || (DateSort = {}));
export class RecordsQuery extends Message {
    static parse(message) {
        return __awaiter(this, void 0, void 0, function* () {
            let authorizationPayload;
            if (message.authorization !== undefined) {
                authorizationPayload = yield validateMessageSignatureIntegrity(message.authorization.signature, message.descriptor);
            }
            if ((authorizationPayload === null || authorizationPayload === void 0 ? void 0 : authorizationPayload.protocolRole) !== undefined) {
                if (message.descriptor.filter.protocolPath === undefined) {
                    throw new DwnError(DwnErrorCode.RecordsQueryFilterMissingRequiredProperties, 'Role-authorized queries must include `protocolPath` in the filter');
                }
            }
            if (message.descriptor.filter.protocol !== undefined) {
                validateProtocolUrlNormalized(message.descriptor.filter.protocol);
            }
            if (message.descriptor.filter.schema !== undefined) {
                validateSchemaUrlNormalized(message.descriptor.filter.schema);
            }
            Time.validateTimestamp(message.descriptor.messageTimestamp);
            return new RecordsQuery(message);
        });
    }
    static create(options) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const descriptor = {
                interface: DwnInterfaceName.Records,
                method: DwnMethodName.Query,
                messageTimestamp: (_a = options.messageTimestamp) !== null && _a !== void 0 ? _a : Time.getCurrentTimestamp(),
                filter: Records.normalizeFilter(options.filter),
                dateSort: options.dateSort,
                pagination: options.pagination,
            };
            // 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
            removeUndefinedProperties(descriptor);
            // only generate the `authorization` property if signature input is given
            const signer = options.signer;
            let authorization;
            if (signer) {
                authorization = yield Message.createAuthorization(descriptor, signer, { protocolRole: options.protocolRole });
            }
            const message = { descriptor, authorization };
            Message.validateJsonSchema(message);
            return new RecordsQuery(message);
        });
    }
}
//# sourceMappingURL=records-query.js.map