"use strict";
/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.readLastUpdateData = readLastUpdateData;
const tslib_1 = require("tslib");
const lodash_1 = tslib_1.__importDefault(require("lodash"));
const vcs_1 = require("./vcs/vcs");
// TODO Docusaurus v4: refactor/rename, make it clear this fn is only
//  for Markdown files with front matter shared by content plugin
async function readLastUpdateData(filePath, options, lastUpdateFrontMatter, vcsParam) {
    // We fallback to the default VSC config at runtime on purpose
    // It preserves retro-compatibility if a third-party plugin imports it
    // This also ensures unit tests keep working without extra setup
    // We still want to ensure type safety by requiring the VCS param
    // TODO Docusaurus v4: refactor all these Git read APIs
    const vcs = vcsParam ?? (0, vcs_1.getVcsPreset)('default-v1');
    const { showLastUpdateAuthor, showLastUpdateTime } = options;
    if (!showLastUpdateAuthor && !showLastUpdateTime) {
        return { lastUpdatedBy: undefined, lastUpdatedAt: undefined };
    }
    const frontMatterAuthor = lastUpdateFrontMatter?.author;
    const frontMatterTimestamp = lastUpdateFrontMatter?.date
        ? new Date(lastUpdateFrontMatter.date).getTime()
        : undefined;
    // We try to minimize git last update calls
    // We call it at most once
    // If all the data is provided as front matter, we do not call it
    const getLastUpdateMemoized = lodash_1.default.memoize(() => vcs.getFileLastUpdateInfo(filePath));
    const getLastUpdateBy = () => getLastUpdateMemoized().then((update) => {
        // Important, see https://github.com/facebook/docusaurus/pull/11211
        if (update === null) {
            return null;
        }
        return update?.author;
    });
    const getLastUpdateAt = () => getLastUpdateMemoized().then((update) => {
        // Important, see https://github.com/facebook/docusaurus/pull/11211
        if (update === null) {
            return null;
        }
        return update?.timestamp;
    });
    const lastUpdatedBy = showLastUpdateAuthor
        ? frontMatterAuthor ?? (await getLastUpdateBy())
        : undefined;
    const lastUpdatedAt = showLastUpdateTime
        ? frontMatterTimestamp ?? (await getLastUpdateAt())
        : undefined;
    return {
        lastUpdatedBy,
        lastUpdatedAt,
    };
}
//# sourceMappingURL=lastUpdateUtils.js.map