"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get keyExists () {
        return keyExists;
    },
    get treePathReducer () {
        return treePathReducer;
    }
});
var _utils = require("../../../utils");
function _type_of(obj) {
    "@swc/helpers - typeof";
    return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
var keyExists = function keyExists(obj, path) {
    if ((typeof obj === "undefined" ? "undefined" : _type_of(obj)) !== 'object' || obj === null || !Array.isArray(path)) {
        return false;
    }
    var current = obj;
    for(var i = 0; i < path.length; i++){
        var key = path[i];
        if (current === null || (typeof current === "undefined" ? "undefined" : _type_of(current)) !== 'object' || !(key in current)) {
            return false;
        }
        current = current[key];
    }
    return true;
};
function createObjectAtPath(obj, path) {
    if ((typeof obj === "undefined" ? "undefined" : _type_of(obj)) !== 'object' || obj === null) {
        return;
    }
    var current = obj;
    for(var i = 0; i < path.length; i++){
        var key = path[i];
        if (i === path.length - 1) {
            current[key] = {};
            break;
        }
        if (current[key] === undefined || current[key] === null) {
            current[key] = {};
        }
        current = current[key];
    }
    return obj;
}
function removeObjectAtPath(obj, path) {
    if (path.length === 0) {
        return obj;
    }
    var current = obj;
    for(var i = 0; i < path.length; i++){
        var key = path[i];
        if (current === null || (typeof current === "undefined" ? "undefined" : _type_of(current)) !== 'object' || !(key in current)) {
            return obj;
        }
        if (i === path.length - 1) {
            delete current[key];
            break;
        }
        current = current[key];
    }
    return obj;
}
function treePathReducer(state, action) {
    switch(action.type){
        case 'reset':
            {
                return {};
            }
        case 'toggled_level':
            {
                var stateCopy = (0, _utils.deepCopy)(state);
                var isTargetLevelOpened = keyExists(stateCopy, action.value);
                if (isTargetLevelOpened) {
                    // нужно закрыть текущий уровень
                    removeObjectAtPath(stateCopy, action.value);
                } else {
                    // нужно открыть требуемый уровень
                    createObjectAtPath(stateCopy, action.value);
                }
                return stateCopy;
            }
        case 'open_level':
            {
                var stateCopy1 = (0, _utils.deepCopy)(state);
                createObjectAtPath(stateCopy1, action.value);
                return stateCopy1;
            }
        case 'close_level':
            {
                var stateCopy2 = (0, _utils.deepCopy)(state);
                removeObjectAtPath(stateCopy2, action.value);
                return stateCopy2;
            }
        default:
            {
                return state;
            }
    }
}
