"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sameResourceState = exports.getETag = void 0;
const Vocabularies_1 = require("../util/Vocabularies");
/**
 * Generates an ETag based on the last modified date of a resource.
 * @param metadata - Metadata of the resource.
 *
 * @returns the generated ETag. Undefined if no last modified date was found.
 */
function getETag(metadata) {
    const modified = metadata.get(Vocabularies_1.DC.terms.modified);
    const { contentType } = metadata;
    if (modified) {
        const date = new Date(modified.value);
        // It is possible for the content type to be undefined,
        // such as when only the metadata returned by a `DataAccessor` is used.
        return `"${date.getTime()}-${contentType ?? ''}"`;
    }
}
exports.getETag = getETag;
/**
 * Validates whether 2 ETags correspond to the same state of a resource,
 * independent of the representation the ETags correspond to.
 * Assumes ETags are made with the {@link getETag} function.
 */
function sameResourceState(eTag1, eTag2) {
    // Since we base the ETag on the last modified date,
    // we know the ETags match as long as the date part is the same.
    return eTag1.split('-')[0] === eTag2.split('-')[0];
}
exports.sameResourceState = sameResourceState;
//# sourceMappingURL=Conditions.js.map