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;
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "../core/StringHelper", "../di/RegisterSingleton", "@web-atoms/date-time/dist/DateTime"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.JsonService = exports.dateFormatMSRegEx = exports.dateFormatISORegEx = void 0;
    const StringHelper_1 = require("../core/StringHelper");
    const RegisterSingleton_1 = require("../di/RegisterSingleton");
    const DateTime_1 = require("@web-atoms/date-time/dist/DateTime");
    exports.dateFormatISORegEx = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/;
    exports.dateFormatMSRegEx = /^\/Date\((d|-|.*)\)[\/|\\]$/;
    const timeZoneDiff = (new Date()).getTimezoneOffset();
    let JsonService = class JsonService {
        constructor() {
            this.options = {
                indent: 2,
                namingStrategy: "none",
                dateConverter: [
                    {
                        regex: exports.dateFormatISORegEx,
                        valueConverter: {
                            fromSource(v) {
                                const d = new DateTime_1.default(v);
                                // if (/z$/i.test(v)) {
                                //     d.setMinutes( d.getMinutes() - timeZoneDiff );
                                // }
                                return d;
                            },
                            fromTarget(v) {
                                return v.toISOString();
                            }
                        }
                    }, {
                        regex: exports.dateFormatMSRegEx,
                        valueConverter: {
                            fromSource(v) {
                                const a = exports.dateFormatMSRegEx.exec(v);
                                const b = a[1].split(/[-+,.]/);
                                return new DateTime_1.default(b[0] ? +b[0] : 0 - +b[1]);
                            },
                            fromTarget(v) {
                                return v.toISOString();
                            }
                        }
                    }
                ]
            };
        }
        transformKeys(t, v) {
            if (!v) {
                return v;
            }
            if (typeof v !== "object") {
                return v;
            }
            if (v instanceof Date) {
                return v;
            }
            if (typeof v === "object" && v.length !== undefined && typeof v.length === "number") {
                const a = v;
                if (a.map) {
                    return a.map((x) => this.transformKeys(t, x));
                }
                const ra = [];
                // tslint:disable-next-line: prefer-for-of
                for (let i = 0; i < a.length; i++) {
                    const iterator = a[i];
                    ra.push(this.transformKeys(t, iterator));
                }
                return ra;
            }
            const r = {};
            for (const key in v) {
                if (v.hasOwnProperty(key)) {
                    const element = v[key];
                    r[t(key)] = this.transformKeys(t, element);
                }
            }
            return r;
        }
        parse(text, options) {
            const { dateConverter, namingStrategy } = Object.assign(Object.assign({}, this.options), options);
            const result = JSON.parse(text, (key, value) => {
                // transform date...
                if (typeof value === "string") {
                    for (const iterator of dateConverter) {
                        const a = iterator.regex.test(value);
                        if (a) {
                            const dv = iterator.valueConverter.fromSource(value);
                            return dv;
                        }
                    }
                }
                return value;
            });
            switch (namingStrategy) {
                case "hyphen":
                    return this.transformKeys(StringHelper_1.StringHelper.fromHyphenToCamel, result);
                case "underscore":
                    return this.transformKeys(StringHelper_1.StringHelper.fromUnderscoreToCamel, result);
                case "pascal":
                    return this.transformKeys(StringHelper_1.StringHelper.fromPascalToCamel, result);
            }
            return result;
        }
        stringify(v, options) {
            const { namingStrategy, dateConverter, indent } = Object.assign(Object.assign({}, this.options), options);
            switch (namingStrategy) {
                case "hyphen":
                    v = this.transformKeys(StringHelper_1.StringHelper.fromCamelToHyphen, v);
                    break;
                case "underscore":
                    v = this.transformKeys(StringHelper_1.StringHelper.fromCamelToUnderscore, v);
                    break;
                case "pascal":
                    v = this.transformKeys(StringHelper_1.StringHelper.fromCamelToPascal, v);
                    break;
            }
            return JSON.stringify(v, (key, value) => {
                if (key && /^\_\$\_/.test(key)) {
                    return undefined;
                }
                if (dateConverter && (value instanceof Date)) {
                    return dateConverter[0].valueConverter.fromTarget(value);
                }
                return value;
            }, indent);
        }
    };
    JsonService = __decorate([
        RegisterSingleton_1.RegisterSingleton
    ], JsonService);
    exports.JsonService = JsonService;
});
//# sourceMappingURL=JsonService.js.map