import { parseSitecoreDate } from "./sitecoreDate";
function isVersionValidAtDate(version, date) {
    const restrictions = version.publishingRestrictions;
    if (restrictions?.hideVersion)
        return false;
    const validFrom = parseSitecoreDate(restrictions?.validFrom);
    if (validFrom && validFrom.getTime() > date.getTime())
        return false;
    const validTo = parseSitecoreDate(restrictions?.validTo);
    if (validTo && validTo.getTime() <= date.getTime())
        return false;
    return true;
}
function byNewestVersion(a, b) {
    return b.version - a.version;
}
export function pickVersionAtDate(versions, date) {
    const sortedVersions = [...versions].sort(byNewestVersion);
    return (sortedVersions.find((version) => isVersionValidAtDate(version, date)) ||
        sortedVersions.find((version) => !version.publishingRestrictions?.hideVersion) ||
        sortedVersions[0]);
}
//# sourceMappingURL=versionAtDate.js.map