"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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require("@angular/core");
var moment = require("moment");
var typescript_angular_utilities_1 = require("typescript-angular-utilities");
var __dateFormats = typescript_angular_utilities_1.services.date.defaultFormats;
var __timezone = typescript_angular_utilities_1.services.timezone;
var __utc = __timezone.UTC;
/*
 * Filters a string and find all, datetimes that match the format M/D/YYYY H:M:S AM|PM that is used to store dates in message log body text.
 * Assumes they are UTC  and parses them, converts to local browser time replaces them in the string.
 */
var LocalizeStringDatesPipe = (function () {
    function LocalizeStringDatesPipe(timezoneService) {
        this.timezoneService = timezoneService;
    }
    LocalizeStringDatesPipe.prototype.transform = function (input) {
        var _this = this;
        if (!input) {
            return '';
        }
        //if no timezone is specified then use moment to get the browsers time zone.
        var timezone = this.timezoneService.currentTimezone.momentName;
        // regex to match M/DD/YYYY hh:mm:ss AM|PM with an optional UTC at the end.
        var regex = /([1-9]|1[0-2])\/([1-9]|1\d|2\d|3[01])\/(19|20)\d{2} ([1-9]|1[0-9])\:([0-9])([0-9])(?:\:([0-9])([0-9]))? (AM|PM)(?: UTC)?/g;
        var messageLogText = input;
        var datesToReplace = messageLogText.match(regex);
        if (datesToReplace != null) {
            //Iterates each date found, reformats it for the current time zone and replaces its original in the string.
            datesToReplace.forEach(function (date) {
                messageLogText = messageLogText.replace(date, _this.formatDate(date, timezone));
            });
        }
        return messageLogText;
    };
    LocalizeStringDatesPipe.prototype.formatDate = function (input, timezone) {
        if (!input) {
            return '';
        }
        var momentDate = moment.tz(input, __dateFormats.dateTimeFormat, __utc.momentName);
        return momentDate.tz(timezone).format(__dateFormats.dateTimeFormat) + ' ' + momentDate.tz(timezone).zoneAbbr();
    };
    return LocalizeStringDatesPipe;
}());
LocalizeStringDatesPipe = __decorate([
    core_1.Pipe({ name: 'rlLocalizeStringDates' }),
    __metadata("design:paramtypes", [__timezone.TimezoneService])
], LocalizeStringDatesPipe);
exports.LocalizeStringDatesPipe = LocalizeStringDatesPipe;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG9jYWxpemVTdHJpbmdEYXRlcy5waXBlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsibG9jYWxpemVTdHJpbmdEYXRlcy5waXBlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7QUFBQSxzQ0FBb0Q7QUFDcEQsK0JBQWlDO0FBRWpDLDZFQUF3RDtBQUN4RCxJQUFPLGFBQWEsR0FBRyx1Q0FBUSxDQUFDLElBQUksQ0FBQyxjQUFjLENBQUM7QUFDcEQsSUFBTyxVQUFVLEdBQUcsdUNBQVEsQ0FBQyxRQUFRLENBQUM7QUFDdEMsSUFBTyxLQUFLLEdBQUcsVUFBVSxDQUFDLEdBQUcsQ0FBQztBQUU5Qjs7O0dBR0c7QUFFSCxJQUFhLHVCQUF1QjtJQUduQyxpQ0FBWSxlQUEyQztRQUN0RCxJQUFJLENBQUMsZUFBZSxHQUFHLGVBQWUsQ0FBQztJQUN4QyxDQUFDO0lBRUQsMkNBQVMsR0FBVCxVQUFVLEtBQWM7UUFBeEIsaUJBc0JDO1FBckJBLEVBQUUsQ0FBQyxDQUFDLENBQUMsS0FBSyxDQUFDLENBQUMsQ0FBQztZQUNaLE1BQU0sQ0FBQyxFQUFFLENBQUM7UUFDWCxDQUFDO1FBRUQsNEVBQTRFO1FBQzVFL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