"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.lunar2solar = exports.solar2lunar = exports.normalizeDateStr = void 0;
var lunar_typescript_1 = require("lunar-typescript");
/**
 * 将日期字符串 YYYY-MM-DD 或者一个 Date 对象分割为 [YYYY, M, D, H, m, s]
 * 当参数为字符串时分割符可以是 `-` `.` 或者 `/`
 *
 * @param dateStr 日期字符串或者 Date 对象
 * @returns [年, 月, 日]
 * @example
 * normalizeDateStr('2023-07-31'); // [2023, 7, 31]
 */
var normalizeDateStr = function (date) {
    if (date instanceof Date) {
        return [
            date.getFullYear(),
            date.getMonth() + 1,
            date.getDate(),
            date.getHours(),
            date.getMinutes(),
            date.getSeconds(),
        ];
    }
    return date
        .split(/[ ]+/)
        .map(function (item) { return item.split(/[-:/.]/); })
        .reduce(function (prev, next) { return prev.concat(next); }, [])
        .map(function (item) { return Math.abs(+item); });
};
exports.normalizeDateStr = normalizeDateStr;
/**
 * 公历转农历，年份需要在【1900~2100】之间，并且日期必须在1900-1-31之后
 *
 * @param dateStr 公历日期 YYYY-MM-DD格式的字符串或者Date对象
 * @returns LunarDate
 */
var solar2lunar = function (dateStr) {
    var _a = (0, exports.normalizeDateStr)(dateStr), year = _a[0], month = _a[1], day = _a[2];
    var solar = lunar_typescript_1.Solar.fromYmd(year, month, day);
    var lunar = solar.getLunar();
    var lunarYear = lunar.getYear();
    var lunarMonth = Math.abs(lunar.getMonth());
    var lunarDay = lunar.getDay();
    var isLeap = lunar.getMonth() < 0;
    return {
        lunarYear: lunarYear,
        lunarMonth: lunarMonth,
        lunarDay: lunarDay,
        isLeap: isLeap,
        toString: function (toCnStr) {
            if (toCnStr) {
                return lunar.toString();
            }
            return "".concat(lunarYear, "-").concat(lunarMonth, "-").concat(lunarDay);
        },
    };
};
exports.solar2lunar = solar2lunar;
/**
 * 农历转公历
 *
 * @param dateStr 农历日期 YYYY-MM-DD
 * @param isLeapMonth 是否闰月，若该月不是闰月，会忽略该参数
 * @returns SolarDate
 */
var lunar2solar = function (dateStr, isLeapMonth) {
    var _a = (0, exports.normalizeDateStr)(dateStr), year = _a[0], month = _a[1], day = _a[2];
    var lunar = lunar_typescript_1.Lunar.fromYmd(year, month, day);
    var lunarYear = lunar_typescript_1.LunarYear.fromYear(lunar.getYear());
    var leapMonth = lunarYear.getLeapMonth();
    if (leapMonth > 0 && leapMonth === month && isLeapMonth) {
        lunar = lunar_typescript_1.Lunar.fromYmd(year, 0 - month, day);
    }
    var solar = lunar.getSolar();
    var solarYear = solar.getYear();
    var solarMonth = solar.getMonth();
    var solarDay = solar.getDay();
    return {
        solarYear: solarYear,
        solarMonth: solarMonth,
        solarDay: solarDay,
        toString: function () {
            return "".concat(solarYear, "-").concat(solarMonth, "-").concat(solarDay);
        },
    };
};
exports.lunar2solar = lunar2solar;
