import { parse, isValid, differenceInDays, addDays, format } from "./utils/date";

export class DateConverter {
    constructor() {
        this.key = "date", this.baseDate = new Date(1900, 0, 1), this.dateFormats = [ {
            pattern: /^\d{4}-\d{2}-\d{2}$/,
            format: "yyyy-MM-dd"
        }, {
            pattern: /^\d{4}-\d{1,2}-\d{1,2}$/,
            format: "yyyy-M-d"
        }, {
            pattern: /^\d{4}\/\d{2}\/\d{2}$/,
            format: "yyyy/MM/dd"
        }, {
            pattern: /^\d{4}\/\d{1,2}\/\d{1,2}$/,
            format: "yyyy/M/d"
        }, {
            pattern: /^\d{2}\/\d{2}\/\d{4}$/,
            format: "MM/dd/yyyy"
        }, {
            pattern: /^\d{4}年\d{1,2}月\d{1,2}(日)?$/,
            format: "yyyy年M月d日",
            parser: value => {
                const match = value.match(/(\d{4})年(\d{1,2})月(\d{1,2})/);
                if (!match) throw new Error("无效的中文日期格式");
                return new Date(+match[1], +match[2] - 1, +match[3]);
            }
        } ];
    }
    detect(value) {
        if (value instanceof Date) return !isNaN(value.getTime());
        if ("string" != typeof value) return !1;
        const str = value.trim(), matchedFormat = this.dateFormats.find((rule => rule.pattern.test(str)));
        if (matchedFormat) try {
            const date = matchedFormat.parser ? matchedFormat.parser(str) : parse(str, matchedFormat.format, new Date);
            return isValid(date);
        } catch (_a) {
            return !1;
        }
        return !1;
    }
    toNumber(value) {
        let date, formatMeta;
        if (value instanceof Date) {
            if (!isValid(value)) throw new Error("无效的日期对象");
            date = value, formatMeta = "yyyy-MM-dd";
        } else {
            const str = value.trim(), matchedFormat = this.dateFormats.find((rule => rule.pattern.test(str)));
            if (!matchedFormat) throw new Error(`不支持的日期格式: ${str}`);
            if (date = matchedFormat.parser ? matchedFormat.parser(str) : parse(str, matchedFormat.format, new Date), 
            !isValid(date)) throw new Error(`无效的日期值: ${str}`);
            formatMeta = matchedFormat.format;
        }
        return {
            v: differenceInDays(date, this.baseDate),
            meta: formatMeta
        };
    }
    fromNumber(daysOffset, meta) {
        const targetDate = addDays(this.baseDate, daysOffset);
        return targetDate.setHours(0, 0, 0, 0), format(targetDate, meta || "yyyy-MM-dd");
    }
}

export class ConverterManager {
    constructor() {
        this.converters = [];
    }
    register(converter) {
        this.converters.push(converter);
    }
    getConverter(value) {
        return this.converters.find((converter => converter.detect(value))) || null;
    }
    getConverterByKey(key) {
        return this.converters.find((converter => converter.key === key)) || null;
    }
}

export const converterManager = new ConverterManager;

converterManager.register(new DateConverter);
//# sourceMappingURL=series-converters.js.map