var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import { CHAR_PAD, defaultDateComponentsOrder, defaultDateComponentsSeparator, emptyDateComponents, LENGTH_DATE, LENGTH_MONTH, LENGTH_SEPARATOR, LENGTH_YEAR, RE_ORDER_DMY, RE_ORDER_MDY, RE_ORDER_YMD, } from './constants.js';
import { InternalDateValidator } from './InternalDateValidator.js';
import { InternalDateComponentType, InternalDateOrder, InternalDateSeparator } from './types.js';
var InternalDateTransformer = /** @class */ (function () {
    function InternalDateTransformer() {
    }
    InternalDateTransformer.dateToFragments = function (components, settings) {
        if (settings === void 0) { settings = {}; }
        var _a = settings.order, order = _a === void 0 ? defaultDateComponentsOrder : _a, _b = settings.separator, separator = _b === void 0 ? defaultDateComponentsSeparator : _b, _c = settings.withSeparator, withSeparator = _c === void 0 ? false : _c, _d = settings.withPad, withPad = _d === void 0 ? false : _d, pad = settings.pad;
        var year = {
            type: InternalDateComponentType.Year,
            value: components.year,
            length: LENGTH_YEAR,
        };
        var month = {
            type: InternalDateComponentType.Month,
            value: components.month,
            length: LENGTH_MONTH,
        };
        var date = {
            type: InternalDateComponentType.Date,
            value: components.date,
            length: LENGTH_DATE,
        };
        var fragments = [];
        if (order === InternalDateOrder.YMD) {
            fragments.push(year, month, date);
        }
        else if (order === InternalDateOrder.MDY) {
            fragments.push(month, date, year);
        }
        else if (order === InternalDateOrder.DMY) {
            fragments.push(date, month, year);
        }
        if (withPad) {
            year.valueWithPad = InternalDateTransformer.padYear(year.value, pad);
            month.valueWithPad = InternalDateTransformer.padMonth(month.value, pad);
            date.valueWithPad = InternalDateTransformer.padDate(date.value, pad);
        }
        year.isValid = InternalDateValidator.testParseToNumber(year.value);
        month.isValid = InternalDateValidator.testParseToNumber(month.value);
        date.isValid = InternalDateValidator.testParseToNumber(date.value);
        if (withSeparator) {
            var separatorFragment = {
                type: InternalDateComponentType.Separator,
                value: separator,
                length: LENGTH_SEPARATOR,
            };
            fragments.splice(1, 0, separatorFragment);
            fragments.splice(3, 0, separatorFragment);
        }
        return fragments;
    };
    InternalDateTransformer.parseValueToDate = function (value, order) {
        if (order === void 0) { order = defaultDateComponentsOrder; }
        if (!value) {
            return null;
        }
        var getRe = function () {
            if (order === InternalDateOrder.MDY) {
                return RE_ORDER_MDY;
            }
            else if (order === InternalDateOrder.DMY) {
                return RE_ORDER_DMY;
            }
            return RE_ORDER_YMD;
        };
        var re = getRe();
        if (!re.test(value)) {
            return null;
        }
        var match = re.exec(value);
        var dateComponents = __assign({}, emptyDateComponents);
        if (match) {
            var matchFinished = match
                .slice(1)
                .map(function (item) { return (InternalDateValidator.testParseToNumber(item) ? Number(item) : null); });
            if (order === InternalDateOrder.YMD) {
                (dateComponents.year = matchFinished[0], dateComponents.month = matchFinished[1], dateComponents.date = matchFinished[2]);
            }
            else if (order === InternalDateOrder.MDY) {
                (dateComponents.year = matchFinished[2], dateComponents.month = matchFinished[0], dateComponents.date = matchFinished[1]);
            }
            else if (order === InternalDateOrder.DMY) {
                (dateComponents.year = matchFinished[2], dateComponents.month = matchFinished[1], dateComponents.date = matchFinished[0]);
            }
        }
        return dateComponents;
    };
    InternalDateTransformer.dateComponentsStringToNumber = function (componentsRaw) {
        if (componentsRaw === null) {
            return { year: 0, month: 0, date: 0 };
        }
        var year = componentsRaw.year, month = componentsRaw.month, date = componentsRaw.date;
        return { year: Number(year), month: Number(month), date: Number(date) };
    };
    InternalDateTransformer.dateToInternalString = function (components) {
        return InternalDateTransformer.dateToFragments(components, {
            withPad: true,
            withSeparator: false,
            order: InternalDateOrder.DMY,
        })
            .filter(function (_a) {
            var value = _a.value;
            return value !== null;
        })
            .map(function (_a) {
            var valueWithPad = _a.valueWithPad;
            return valueWithPad;
        })
            .join(InternalDateSeparator.Dot);
    };
    InternalDateTransformer.dateToHumanString = function (_a) {
        var date = _a.date, month = _a.month, year = _a.year;
        return InternalDateTransformer.dateToInternalString({
            date: date,
            month: month !== null ? Number(month) + 1 : null,
            year: year,
        });
    };
    InternalDateTransformer.componentsToNativeDate = function (componentsRaw) {
        var _a = InternalDateTransformer.dateComponentsStringToNumber(componentsRaw), year = _a.year, month = _a.month, date = _a.date;
        return new Date(Date.UTC(year, month - 1, date));
    };
    InternalDateTransformer.componentsToA11YFormat = function (components, canonicalLocale) {
        var nativeDate = InternalDateTransformer.componentsToNativeDate(components);
        return nativeDate.toLocaleDateString(canonicalLocale, {
            weekday: 'long',
            year: 'numeric',
            month: 'long',
            day: 'numeric',
        });
    };
    InternalDateTransformer.padStart = function (value, length, pad) {
        if (pad === void 0) { pad = CHAR_PAD; }
        return String(value || '').padStart(length, pad);
    };
    InternalDateTransformer.padYear = function (year, pad) {
        return InternalDateTransformer.padStart(year, LENGTH_YEAR, pad);
    };
    InternalDateTransformer.padMonth = function (month, pad) {
        return InternalDateTransformer.padStart(month, LENGTH_MONTH, pad);
    };
    InternalDateTransformer.padDate = function (date, pad) {
        return InternalDateTransformer.padStart(date, LENGTH_DATE, pad);
    };
    InternalDateTransformer.padDateComponent = function (type, value, pad) {
        if (type === InternalDateComponentType.Year) {
            return InternalDateTransformer.padYear(value, pad);
        }
        else if (type === InternalDateComponentType.Month) {
            return InternalDateTransformer.padMonth(value, pad);
        }
        else if (type === InternalDateComponentType.Date) {
            return InternalDateTransformer.padDate(value, pad);
        }
        return '';
    };
    return InternalDateTransformer;
}());
export { InternalDateTransformer };
//# sourceMappingURL=InternalDateTransformer.js.map