"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;
    }
});
const _utils = require("../../../utils");
const keyExists = (obj, path)=>{
    if (typeof obj !== 'object' || obj === null || !Array.isArray(path)) {
        return false;
    }
    let current = obj;
    for(let i = 0; i < path.length; i++){
        const key = path[i];
        if (current === null || typeof current !== 'object' || !(key in current)) {
            return false;
        }
        current = current[key];
    }
    return true;
};
function createObjectAtPath(obj, path) {
    if (typeof obj !== 'object' || obj === null) {
        return;
    }
    let current = obj;
    for(let i = 0; i < path.length; i++){
        const 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;
    }
    let current = obj;
    for(let i = 0; i < path.length; i++){
        const key = path[i];
        if (current === null || typeof 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':
            {
                const stateCopy = (0, _utils.deepCopy)(state);
                const isTargetLevelOpened = keyExists(stateCopy, action.value);
                if (isTargetLevelOpened) {
                    // нужно закрыть текущий уровень
                    removeObjectAtPath(stateCopy, action.value);
                } else {
                    // нужно открыть требуемый уровень
                    createObjectAtPath(stateCopy, action.value);
                }
                return stateCopy;
            }
        case 'open_level':
            {
                const stateCopy = (0, _utils.deepCopy)(state);
                createObjectAtPath(stateCopy, action.value);
                return stateCopy;
            }
        case 'close_level':
            {
                const stateCopy = (0, _utils.deepCopy)(state);
                removeObjectAtPath(stateCopy, action.value);
                return stateCopy;
            }
        default:
            {
                return state;
            }
    }
}
