"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileSizeValueConverter = exports.VCSValueConverter = exports.IbanValueConverter = exports.CurrencyValueConverter = exports.BoolValueConverter = exports.DecimalValueConverter = exports.EuroValueConverter = exports.PercentValueConverter = exports.NumberValueConverter = exports.IsoValueConverter = exports.UtcValueConverter = exports.AgeValueConverter = exports.FromNowValueConverter = exports.DatetimeValueConverter = exports.TimeValueConverter = exports.DateValueConverter = exports.PhoneLocalHtmlValueConverter = exports.PhoneHtmlValueConverter = exports.PhoneLocalValueConverter = exports.PhoneValueConverter = exports.JsonValueConverter = void 0;
var aurelia_framework_1 = require("aurelia-framework");
var libphonenumber_js_1 = require("libphonenumber-js");
var ui_format_1 = require("../utils/ui-format");
var getPhone = function (value, country) {
    if (value === void 0) { value = ""; }
    if (country === void 0) { country = "us"; }
    var number = (0, libphonenumber_js_1.parsePhoneNumberFromString)(value || "", country);
    return number ? number : {
        country: "",
        formatNational: function () { return ""; },
        formatInternational: function () { return ""; }
    };
};
var JsonValueConverter = exports.JsonValueConverter = (function () {
    function JsonValueConverter() {
    }
    JsonValueConverter.prototype.toView = function (value) {
        return JSON.stringify(value);
    };
    JsonValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("json")
    ], JsonValueConverter);
    return JsonValueConverter;
}());
var PhoneValueConverter = exports.PhoneValueConverter = (function () {
    function PhoneValueConverter() {
    }
    PhoneValueConverter.prototype.toView = function (value, country) {
        if (country === void 0) { country = ""; }
        return getPhone(value, country).formatInternational();
    };
    PhoneValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("phone")
    ], PhoneValueConverter);
    return PhoneValueConverter;
}());
var PhoneLocalValueConverter = exports.PhoneLocalValueConverter = (function () {
    function PhoneLocalValueConverter() {
    }
    PhoneLocalValueConverter.prototype.toView = function (value, country) {
        if (country === void 0) { country = "us"; }
        return getPhone(value, country).formatNational();
    };
    PhoneLocalValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("phoneLocal")
    ], PhoneLocalValueConverter);
    return PhoneLocalValueConverter;
}());
var PhoneHtmlValueConverter = exports.PhoneHtmlValueConverter = (function () {
    function PhoneHtmlValueConverter() {
    }
    PhoneHtmlValueConverter.prototype.toView = function (value, country) {
        if (country === void 0) { country = ""; }
        var phoneNumber = getPhone(value, country);
        return "<span class=\"ui-flag ".concat(phoneNumber.country, "\"></span>&nbsp;").concat(phoneNumber.formatInternational());
    };
    PhoneHtmlValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("phoneHtml")
    ], PhoneHtmlValueConverter);
    return PhoneHtmlValueConverter;
}());
var PhoneLocalHtmlValueConverter = exports.PhoneLocalHtmlValueConverter = (function () {
    function PhoneLocalHtmlValueConverter() {
    }
    PhoneLocalHtmlValueConverter.prototype.toView = function (value, country) {
        if (country === void 0) { country = "us"; }
        var phoneNumber = getPhone(value, country);
        return "<span class=\"ui-flag ".concat(phoneNumber.country, "\"></span>&nbsp;").concat(phoneNumber.formatNational());
    };
    PhoneLocalHtmlValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("phoneLocalHtml")
    ], PhoneLocalHtmlValueConverter);
    return PhoneLocalHtmlValueConverter;
}());
var DateValueConverter = exports.DateValueConverter = (function () {
    function DateValueConverter() {
    }
    DateValueConverter.prototype.toView = function (value, format) {
        return ui_format_1.UIFormat.date(value, format);
    };
    DateValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("date")
    ], DateValueConverter);
    return DateValueConverter;
}());
var TimeValueConverter = exports.TimeValueConverter = (function () {
    function TimeValueConverter() {
    }
    TimeValueConverter.prototype.toView = function (value, format) {
        return ui_format_1.UIFormat.time(value, format);
    };
    TimeValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("time")
    ], TimeValueConverter);
    return TimeValueConverter;
}());
var DatetimeValueConverter = exports.DatetimeValueConverter = (function () {
    function DatetimeValueConverter() {
    }
    DatetimeValueConverter.prototype.toView = function (value, format) {
        return ui_format_1.UIFormat.datetime(value, format);
    };
    DatetimeValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("datetime")
    ], DatetimeValueConverter);
    return DatetimeValueConverter;
}());
var FromNowValueConverter = exports.FromNowValueConverter = (function () {
    function FromNowValueConverter() {
    }
    FromNowValueConverter.prototype.toView = function (value) {
        return ui_format_1.UIFormat.fromNow(value);
    };
    FromNowValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("fromnow")
    ], FromNowValueConverter);
    return FromNowValueConverter;
}());
var AgeValueConverter = exports.AgeValueConverter = (function () {
    function AgeValueConverter() {
    }
    AgeValueConverter.prototype.toView = function (value) {
        return ui_format_1.UIFormat.age(value);
    };
    AgeValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("age")
    ], AgeValueConverter);
    return AgeValueConverter;
}());
var UtcValueConverter = exports.UtcValueConverter = (function () {
    function UtcValueConverter() {
    }
    UtcValueConverter.prototype.toView = function (value) {
        return ui_format_1.UIFormat.utcDate(value);
    };
    UtcValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("utc")
    ], UtcValueConverter);
    return UtcValueConverter;
}());
var IsoValueConverter = exports.IsoValueConverter = (function () {
    function IsoValueConverter() {
    }
    IsoValueConverter.prototype.toView = function (value) {
        return ui_format_1.UIFormat.dateToISO(value);
    };
    IsoValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("iso")
    ], IsoValueConverter);
    return IsoValueConverter;
}());
var NumberValueConverter = exports.NumberValueConverter = (function () {
    function NumberValueConverter() {
    }
    NumberValueConverter.prototype.toView = function (value, format) {
        return ui_format_1.UIFormat.number(value, format);
    };
    NumberValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("number")
    ], NumberValueConverter);
    return NumberValueConverter;
}());
var PercentValueConverter = exports.PercentValueConverter = (function () {
    function PercentValueConverter() {
    }
    PercentValueConverter.prototype.toView = function (value) {
        return ui_format_1.UIFormat.percent(value);
    };
    PercentValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("percent")
    ], PercentValueConverter);
    return PercentValueConverter;
}());
var EuroValueConverter = exports.EuroValueConverter = (function () {
    function EuroValueConverter() {
    }
    EuroValueConverter.prototype.toView = function (value) {
        return ui_format_1.UIFormat.euro(value);
    };
    EuroValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("euro")
    ], EuroValueConverter);
    return EuroValueConverter;
}());
var DecimalValueConverter = exports.DecimalValueConverter = (function () {
    function DecimalValueConverter() {
    }
    DecimalValueConverter.prototype.fromView = function (val) {
        var src = val.trim();
        if (src === "")
            return null;
        var fVal = parseFloat(src);
        if (isNaN(fVal) || fVal.toString() != val) {
            this.viewVal = val;
        }
        else {
            this.viewVal = null;
        }
        return val;
    };
    DecimalValueConverter.prototype.toView = function (value, format) {
        if (this.viewVal != null)
            return this.viewVal;
        return value;
    };
    DecimalValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("decimal")
    ], DecimalValueConverter);
    return DecimalValueConverter;
}());
var BoolValueConverter = exports.BoolValueConverter = (function () {
    function BoolValueConverter() {
    }
    BoolValueConverter.prototype.toView = function (value) {
        if (value != null) {
            return (value) ? "\u2713" : "\u2716";
        }
        return null;
    };
    BoolValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("bool")
    ], BoolValueConverter);
    return BoolValueConverter;
}());
var CurrencyValueConverter = exports.CurrencyValueConverter = (function () {
    function CurrencyValueConverter() {
    }
    CurrencyValueConverter.prototype.toView = function (value) {
        if (value != null) {
            return value.toLocaleString('de-DE', {
                minimumFractionDigits: 2,
                maximumFractionDigits: 2
            }) + ' €';
        }
        else {
            return '';
        }
    };
    CurrencyValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("currency")
    ], CurrencyValueConverter);
    return CurrencyValueConverter;
}());
var IbanValueConverter = exports.IbanValueConverter = (function () {
    function IbanValueConverter() {
        this.NON_ALPHANUM = /[^a-zA-Z0-9]/g;
        this.EVERY_FOUR_CHARS = /(.{4})(?!$)/g;
    }
    IbanValueConverter.prototype.toView = function (value, separator) {
        if (separator === void 0) { separator = ' '; }
        if (value != null) {
            value = value.replace(this.NON_ALPHANUM, '').toUpperCase();
            return value.replace(this.EVERY_FOUR_CHARS, "$1" + separator);
        }
        else {
            return '';
        }
    };
    IbanValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("iban")
    ], IbanValueConverter);
    return IbanValueConverter;
}());
var VCSValueConverter = exports.VCSValueConverter = (function () {
    function VCSValueConverter() {
        this.NON_ALPHANUM = /[^a-zA-Z0-9]/g;
        this.EVERY_FOUR_CHARS = /(.{4})(?!$)/g;
    }
    VCSValueConverter.prototype.toView = function (value, isVCS) {
        if (isVCS === void 0) { isVCS = true; }
        if (value == null || value.length != 12 || isVCS === false)
            return value;
        value = value.replace(this.NON_ALPHANUM, '').toUpperCase();
        if (value != null) {
            return "+++".concat(value.substring(0, 3), "/").concat(value.substring(3, 7), "/").concat(value.substring(7), "+++");
        }
        else {
            return '';
        }
    };
    VCSValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("vcs")
    ], VCSValueConverter);
    return VCSValueConverter;
}());
var FileSizeValueConverter = exports.FileSizeValueConverter = (function () {
    function FileSizeValueConverter() {
    }
    FileSizeValueConverter.prototype.toView = function (value) {
        if (value && typeof (value) == 'number') {
            var formatedValue;
            var suffix;
            if (value > Math.pow(1024, 3)) {
                formatedValue = value / Math.pow(1024, 3);
                suffix = ' GB';
            }
            else if (value > Math.pow(1024, 2)) {
                formatedValue = value / Math.pow(1024, 2);
                suffix = ' MB';
            }
            else if (value > 1024) {
                formatedValue = value / 1024;
                suffix = ' KB';
            }
            else {
                formatedValue = value;
                suffix = ' B';
            }
            return formatedValue.toFixed(0) + suffix;
        }
        else {
            return '';
        }
    };
    FileSizeValueConverter = __decorate([
        (0, aurelia_framework_1.valueConverter)("fileSize")
    ], FileSizeValueConverter);
    return FileSizeValueConverter;
}());

//# sourceMappingURL=ui-text.js.map
