"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasicConditions = void 0;
const Vocabularies_1 = require("../util/Vocabularies");
const Conditions_1 = require("./Conditions");
/**
 * Stores all the relevant Conditions values and matches them based on RFC7232.
 */
class BasicConditions {
    constructor(options) {
        this.matchesETag = options.matchesETag;
        this.notMatchesETag = options.notMatchesETag;
        this.modifiedSince = options.modifiedSince;
        this.unmodifiedSince = options.unmodifiedSince;
    }
    matchesMetadata(metadata, strict) {
        if (!metadata) {
            // RFC7232: ...If-Match... If the field-value is "*", the condition is false if the origin server
            // does not have a current representation for the target resource.
            return !this.matchesETag?.includes('*');
        }
        // RFC7232: ...If-None-Match... If the field-value is "*", the condition is false if the origin server
        // has a current representation for the target resource.
        if (this.notMatchesETag?.includes('*')) {
            return false;
        }
        const eTag = (0, Conditions_1.getETag)(metadata);
        if (eTag) {
            // Helper function to see if an ETag matches the provided metadata
            // eslint-disable-next-line func-style
            let eTagMatches = (tag) => (0, Conditions_1.sameResourceState)(tag, eTag);
            if (strict) {
                eTagMatches = (tag) => tag === eTag;
            }
            if (this.matchesETag && !this.matchesETag.includes('*') && !this.matchesETag.some(eTagMatches)) {
                return false;
            }
            if (this.notMatchesETag?.some(eTagMatches)) {
                return false;
            }
        }
        // In practice, this will only be undefined on a backend
        // that doesn't store the modified date.
        const modified = metadata.get(Vocabularies_1.DC.terms.modified);
        if (modified) {
            const modifiedDate = new Date(modified.value);
            if (this.modifiedSince && modifiedDate < this.modifiedSince) {
                return false;
            }
            if (this.unmodifiedSince && modifiedDate > this.unmodifiedSince) {
                return false;
            }
        }
        return true;
    }
}
exports.BasicConditions = BasicConditions;
//# sourceMappingURL=BasicConditions.js.map