"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Normalizer = void 0;
const object_hash_1 = __importDefault(require("object-hash"));
const options_1 = require("./options");
const transformers_1 = require("./transformers");
const utils_1 = require("./utils");
/**
 * Class that normalizes object types to strings via hashing
 */
class Normalizer {
    constructor(options = {}) {
        this.optionsChecksum = (0, object_hash_1.default)(options);
        const _a = (0, options_1.getOptionsWithDefaults)(options), { transformer, mapValueTransformer, useToJsonTransform, caseInsensitive } = _a, objectHashOptions = __rest(_a, ["transformer", "mapValueTransformer", "useToJsonTransform", "caseInsensitive"]);
        this.objectHashOptions = objectHashOptions;
        this.caseInsensitive = caseInsensitive;
        this.keyTransformer = useToJsonTransform
            ? (0, utils_1.chain)([transformers_1.Transformers.jsonSerializeDeserialize, transformer])
            : transformer;
        this.valueTransformer = useToJsonTransform
            ? (0, utils_1.chain)([transformers_1.Transformers.jsonSerializeDeserialize, mapValueTransformer])
            : mapValueTransformer;
        if (caseInsensitive) {
            // NOTE: This block ensures case-insensitivity inside objects only.
            // See normalizeHelper() for logic which handles primitive strings
            const caseInsensitiveReplacer = (val) => typeof val === 'string' ? val.toLowerCase() : val;
            const { replacer } = this.objectHashOptions;
            this.objectHashOptions.replacer = replacer
                ? (0, utils_1.chain)([caseInsensitiveReplacer, replacer])
                : caseInsensitiveReplacer;
        }
    }
    /**
     * @returns the checksum for the options passed to this Normalizer
     */
    getOptionsChecksum() {
        return this.optionsChecksum;
    }
    /**
     * Normalize the input by transforming and then hashing the result (if an object)
     * @param input the input to normalize
     * @returns the normalized result
     */
    normalizeKey(input) {
        return this.normalizeHelper(this.keyTransformer(input));
    }
    /**
     * Normalize the input by transforming and then hashing the result (if an object)
     * @param input the input to normalize
     * @returns the normalized result
     */
    normalizeValue(input) {
        return this.normalizeHelper(this.valueTransformer(input));
    }
    normalizeHelper(input) {
        if (Normalizer.isObject(input)) {
            return (0, object_hash_1.default)(input, this.objectHashOptions);
        }
        else if (this.caseInsensitive && typeof input === 'string') {
            return input.toLowerCase();
        }
        else {
            // Primitive value, don't hash
            return input;
        }
    }
    /**
     * Returns true if the input is a javascript object.
     */
    static isObject(input) {
        return typeof input === 'object' && input !== null;
    }
}
exports.Normalizer = Normalizer;
