/**
 * Copyright 2013-2026 the original author or authors from the JHipster project.
 *
 * This file is part of the JHipster project, see https://www.jhipster.tech/
 * for more information.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
export function formatDateForChangelog(now) {
    const nowUTC = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
    const year = `${nowUTC.getFullYear()}`;
    let month = `${nowUTC.getMonth() + 1}`;
    if (month.length === 1) {
        month = `0${month}`;
    }
    let day = `${nowUTC.getDate()}`;
    if (day.length === 1) {
        day = `0${day}`;
    }
    let hour = `${nowUTC.getHours()}`;
    if (hour.length === 1) {
        hour = `0${hour}`;
    }
    let minute = `${nowUTC.getMinutes()}`;
    if (minute.length === 1) {
        minute = `0${minute}`;
    }
    let second = `${nowUTC.getSeconds()}`;
    if (second.length === 1) {
        second = `0${second}`;
    }
    return `${year}${month}${day}${hour}${minute}${second}`;
}
export function parseChangelog(changelogDate) {
    if (!changelogDate) {
        throw new Error('changelogDate is required.');
    }
    if (typeof changelogDate !== 'string') {
        throw new TypeError(`changelogDate ${changelogDate} must be a string.`);
    }
    if (changelogDate.length !== 14) {
        throw new Error(`changelogDate ${changelogDate} is not a valid changelogDate.`);
    }
    const zeroFallback = (val, fallback) => (/^0+$/.test(val) ? fallback : val);
    const year = zeroFallback(changelogDate.slice(0, 4), '2024');
    const month = zeroFallback(changelogDate.slice(4, 6), '01');
    const day = zeroFallback(changelogDate.slice(6, 8), '01');
    const formattedDate = `${year}-${month}-${day}T${changelogDate.slice(8, 10)}:${changelogDate.slice(10, 12)}:${changelogDate.slice(12, 14)}+00:00`;
    const parsedTimestamp = Date.parse(formattedDate);
    if (Number.isNaN(parsedTimestamp)) {
        throw new TypeError(`changelogDate ${changelogDate} is not a valid date.`);
    }
    return new Date(parsedTimestamp);
}
/**
 * Parse creationTimestamp option
 * @returns representing the milliseconds elapsed since January 1, 1970, 00:00:00 UTC
 *                   obtained by parsing the given string representation of the creationTimestamp.
 */
export const parseCreationTimestamp = (creationTimestampOption) => {
    let creationTimestamp;
    if (creationTimestampOption) {
        creationTimestamp = Date.parse(creationTimestampOption);
        if (!creationTimestamp) {
            return undefined;
        }
        if (creationTimestamp > Date.now()) {
            throw new Error(`Creation timestamp should not be in the future: ${creationTimestampOption}.`);
        }
    }
    return creationTimestamp;
};
