/**
 * DevExtreme (cjs/viz/tree_map/api.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
var _tree_map = _interopRequireDefault(require("./tree_map.base"));
var _node = _interopRequireDefault(require("./node"));
var _extend2 = require("../../core/utils/extend");
var _common = require("../../core/utils/common");

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}
const proto = _tree_map.default.prototype;
const nodeProto = _node.default.prototype;
proto._eventsMap.onNodesInitialized = {
    name: "nodesInitialized"
};
proto._eventsMap.onNodesRendering = {
    name: "nodesRendering"
};
proto._createProxyType = function() {
    const that = this;
    let nodes;
    Proxy.prototype = {
        constructor: Proxy,
        getParent: function() {
            return nodes[this._id].parent.proxy || null
        },
        getChild: function(index) {
            const _nodes = nodes[this._id].nodes;
            return _nodes ? _nodes[index].proxy : null
        },
        getChildrenCount: function() {
            const _nodes = nodes[this._id].nodes;
            return _nodes ? _nodes.length : 0
        },
        getAllChildren: function() {
            const _nodes = nodes[this._id].nodes;
            let i;
            const ii = _nodes && _nodes.length;
            const list = [];
            for (i = 0; i < ii; ++i) {
                list.push(_nodes[i].proxy)
            }
            return list
        },
        getAllNodes: function() {
            const list = [];
            collectNodes(nodes[this._id], list);
            return list
        },
        isLeaf: function() {
            return !nodes[this._id].isNode()
        },
        isActive: function() {
            return nodes[this._id].isActive()
        },
        value: function(arg) {
            const node = nodes[this._id];
            let result;
            if (void 0 !== arg) {
                updateValue(node, arg > 0 ? Number(arg) : 0);
                change(node, ["TILING"]);
                result = this
            } else {
                result = node.value
            }
            return result
        },
        label: function(arg) {
            const node = nodes[this._id];
            let result;
            if (void 0 !== arg) {
                node.customLabel = arg ? String(arg) : null;
                change(node, ["LABELS"]);
                result = this
            } else {
                result = node.customLabel || node.label
            }
            return result
        },
        customize: function(settings) {
            const node = nodes[this._id];
            if (settings) {
                node._custom = node._custom || {};
                (0, _extend2.extend)(true, node._custom, settings);
                node._partialState = node._partialLabelState = null
            }
            change(node, ["TILES", "LABELS"]);
            return this
        },
        resetCustomization: function() {
            const node = nodes[this._id];
            node._custom = node._partialState = node._partialLabelState = null;
            change(node, ["TILES", "LABELS"]);
            return this
        }
    };
    that._extendProxyType(Proxy.prototype);

    function Proxy(node) {
        node.proxy = this;
        this._id = node._id;
        this.level = node.level;
        this.index = node.index;
        this.data = node.data
    }
    that._handlers.beginBuildNodes = function() {
        nodes = that._nodes;
        new Proxy(that._root)
    };
    that._handlers.buildNode = function(node) {
        new Proxy(node)
    };
    that._handlers.endBuildNodes = function() {
        that._eventTrigger("nodesInitialized", {
            root: that._root.proxy
        })
    }
};

function change(node, codes) {
    const ctx = node.ctx;
    ctx.suspend();
    ctx.change(codes);
    ctx.resume()
}

function collectNodes(node, list) {
    const nodes = node.nodes;
    let i;
    const ii = nodes && nodes.length;
    for (i = 0; i < ii; ++i) {
        list.push(nodes[i].proxy);
        collectNodes(nodes[i], list)
    }
}

function updateValue(node, value) {
    const delta = value - node.value;
    while (node) {
        node.value += delta;
        node = node.parent
    }
}
proto._extendProxyType = _common.noop;
const _resetNodes = proto._resetNodes;
proto._resetNodes = function() {
    _resetNodes.call(this);
    this._eventTrigger("nodesRendering", {
        node: this._topNode.proxy
    })
};
const _updateStyles = nodeProto.updateStyles;
nodeProto.updateStyles = function() {
    const that = this;
    _updateStyles.call(that);
    if (that._custom) {
        that._partialState = !that.ctx.forceReset && that._partialState || that.ctx.calculateState(that._custom);
        (0, _extend2.extend)(true, that.state, that._partialState)
    }
};
const _updateLabelStyle = nodeProto.updateLabelStyle;
nodeProto.updateLabelStyle = function() {
    const that = this;
    const custom = that._custom;
    _updateLabelStyle.call(that);
    if (custom && custom.label) {
        that._partialLabelState = !that.ctx.forceReset && that._partialLabelState || calculatePartialLabelState(that, custom.label);
        that.labelState = (0, _extend2.extend)(true, {}, that.labelState, that._partialLabelState)
    }
};

function calculatePartialLabelState(node, settings) {
    const state = node.ctx.calculateLabelState(settings);
    if ("visible" in settings) {
        state.visible = !!settings.visible
    }
    return state
}
proto.getRootNode = function() {
    return this._root.proxy
};
proto.resetNodes = function() {
    const context = this._context;
    context.suspend();
    context.change(["NODES_CREATE"]);
    context.resume();
    return this
};
