/**
 * DevExtreme (core/options/option_manager.js)
 * Version: 20.2.8 (build 21202-1243)
 * Build date: Wed Jul 21 2021
 *
 * Copyright (c) 2012 - 2021 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.OptionManager = void 0;
var _data = require("../utils/data");
var _common = require("../utils/common");
var _comparator = require("../utils/comparator");
var _extend = require("../utils/extend");
var _type = require("../utils/type");
var _utils = require("./utils");
var cachedGetters = {};
var cachedSetters = {};
var OptionManager = function() {
    function OptionManager(options, optionsByReference) {
        this._options = options;
        this._optionsByReference = optionsByReference;
        this._changingCallback;
        this._changedCallback;
        this._namePreparedCallbacks
    }
    var _proto = OptionManager.prototype;
    _proto._setByReference = function(options, rulesOptions) {
        (0, _extend.extend)(true, options, rulesOptions);
        for (var fieldName in this._optionsByReference) {
            if (Object.prototype.hasOwnProperty.call(rulesOptions, fieldName)) {
                options[fieldName] = rulesOptions[fieldName]
            }
        }
    };
    _proto._setPreparedValue = function(name, value, merge, silent) {
        var previousValue = this.get(this._options, name, false);
        if (!(0, _comparator.equals)(previousValue, value)) {
            var path = name.split(/[.[]/);
            !silent && this._changingCallback(name, previousValue, value);
            cachedSetters[name] = cachedSetters[name] || (0, _data.compileSetter)(name);
            cachedSetters[name](this._options, value, {
                functionsAsIs: true,
                merge: (0, _type.isDefined)(merge) ? merge : !this._optionsByReference[name],
                unwrapObservables: path.length > 1 && !!this._optionsByReference[path[0]]
            });
            !silent && this._changedCallback(name, value, previousValue)
        }
    };
    _proto._prepareRelevantNames = function(options, name, value, silent) {
        if ((0, _type.isPlainObject)(value)) {
            for (var valueName in value) {
                this._prepareRelevantNames(options, "".concat(name, ".").concat(valueName), value[valueName])
            }
        }
        this._namePreparedCallbacks(options, name, value, silent)
    };
    _proto.get = function() {
        var options = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : this._options;
        var name = arguments.length > 1 ? arguments[1] : void 0;
        var unwrapObservables = arguments.length > 2 ? arguments[2] : void 0;
        cachedGetters[name] = cachedGetters[name] || (0, _data.compileGetter)(name);
        return cachedGetters[name](options, {
            functionsAsIs: true,
            unwrapObservables: unwrapObservables
        })
    };
    _proto.set = function(options, value, merge, silent) {
        options = (0, _utils.normalizeOptions)(options, value);
        for (var name in options) {
            this._prepareRelevantNames(options, name, options[name], silent)
        }
        for (var _name in options) {
            this._setPreparedValue(_name, options[_name], merge, silent)
        }
    };
    _proto.onRelevantNamesPrepared = function(callBack) {
        this._namePreparedCallbacks = callBack
    };
    _proto.onChanging = function(callBack) {
        this._changingCallback = callBack
    };
    _proto.onChanged = function(callBack) {
        this._changedCallback = callBack
    };
    _proto.dispose = function() {
        this._changingCallback = _common.noop;
        this._changedCallback = _common.noop
    };
    return OptionManager
}();
exports.OptionManager = OptionManager;
