import { __assign } from "tslib";
import { invariant } from '@formatjs/ecma402-abstract';
import { DATE_TIME_PROPS, additionPenalty, removalPenalty, longMorePenalty, shortMorePenalty, shortLessPenalty, longLessPenalty, offsetPenalty, } from './utils';
/**
 * https://tc39.es/ecma402/#sec-basicformatmatcher
 * @param options
 * @param formats
 */
export function BasicFormatMatcher(options, formats) {
    var bestScore = -Infinity;
    var bestFormat = formats[0];
    invariant(Array.isArray(formats), 'formats should be a list of things');
    for (var _i = 0, formats_1 = formats; _i < formats_1.length; _i++) {
        var format = formats_1[_i];
        var score = 0;
        for (var _a = 0, DATE_TIME_PROPS_1 = DATE_TIME_PROPS; _a < DATE_TIME_PROPS_1.length; _a++) {
            var prop = DATE_TIME_PROPS_1[_a];
            var optionsProp = options[prop];
            var formatProp = format[prop];
            if (optionsProp === undefined && formatProp !== undefined) {
                score -= additionPenalty;
            }
            else if (optionsProp !== undefined && formatProp === undefined) {
                score -= removalPenalty;
            }
            else if (prop === 'timeZoneName') {
                if (optionsProp === 'short' || optionsProp === 'shortGeneric') {
                    if (formatProp === 'shortOffset') {
                        score -= offsetPenalty;
                    }
                    else if (formatProp === 'longOffset') {
                        score -= offsetPenalty + shortMorePenalty;
                    }
                    else if (optionsProp === 'short' && formatProp === 'long') {
                        score -= shortMorePenalty;
                    }
                    else if (optionsProp === 'shortGeneric' &&
                        formatProp === 'longGeneric') {
                        score -= shortMorePenalty;
                    }
                    else if (optionsProp !== formatProp) {
                        score -= removalPenalty;
                    }
                }
                else if (optionsProp === 'shortOffset' &&
                    formatProp === 'longOffset') {
                    score -= shortMorePenalty;
                }
                else if (optionsProp === 'long' || optionsProp === 'longGeneric') {
                    if (formatProp === 'longOffset') {
                        score -= offsetPenalty;
                    }
                    else if (formatProp === 'shortOffset') {
                        score -= offsetPenalty + longLessPenalty;
                    }
                    else if (optionsProp === 'long' && formatProp === 'short') {
                        score -= longLessPenalty;
                    }
                    else if (optionsProp === 'longGeneric' &&
                        formatProp === 'shortGeneric') {
                        score -= longLessPenalty;
                    }
                    else if (optionsProp !== formatProp) {
                        score -= removalPenalty;
                    }
                }
                else if (optionsProp === 'longOffset' &&
                    formatProp === 'shortOffset') {
                    score -= longLessPenalty;
                }
                else if (optionsProp !== formatProp) {
                    score -= removalPenalty;
                }
            }
            else if (optionsProp !== formatProp) {
                var values = void 0;
                if (prop === 'fractionalSecondDigits') {
                    values = [1, 2, 3];
                }
                else {
                    values = ['2-digit', 'numeric', 'narrow', 'short', 'long'];
                }
                var optionsPropIndex = values.indexOf(optionsProp);
                var formatPropIndex = values.indexOf(formatProp);
                var delta = Math.max(-2, Math.min(formatPropIndex - optionsPropIndex, 2));
                if (delta === 2) {
                    score -= longMorePenalty;
                }
                else if (delta === 1) {
                    score -= shortMorePenalty;
                }
                else if (delta === -1) {
                    score -= shortLessPenalty;
                }
                else if (delta === -2) {
                    score -= longLessPenalty;
                }
            }
        }
        if (score > bestScore) {
            bestScore = score;
            bestFormat = format;
        }
    }
    return __assign({}, bestFormat);
}
//# sourceMappingURL=BasicFormatMatcher.js.map