"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.insertSubComponentChild = exports.appendSubComponentChild = exports.removeSubComponentChild = exports.setSubComponentChildren = exports.setSubComponentSize = exports.setChildren = exports.setSize = exports.removeSubComponent = exports.setSubComponent = exports.removeChild = exports.insertChild = exports.appendChild = exports.defaultState = void 0;
const f_1 = require("../../system/core/array/RemoveIndex/f");
const f_2 = require("../../system/core/object/AssocPath/f");
const f_3 = require("../../system/core/object/DeepGet/f");
const f_4 = require("../../system/core/object/DissocPath/f");
const f_5 = require("../../system/f/array/IndexOf/f");
const f_6 = require("../../system/f/array/Insert/f");
const f_7 = require("../../system/f/object/Merge/f");
const f_8 = require("../../system/f/object/Set/f");
const component_1 = require("../actions/component");
exports.defaultState = {};
const appendChild = ({ unitId }, state) => {
    const children = state.children || [];
    return (0, f_8.default)(state, 'children', [...children, unitId]);
};
exports.appendChild = appendChild;
const insertChild = ({ id, at }, state) => {
    const children = state.children || [];
    return (0, f_8.default)(state, 'children', (0, f_6._insert)(children, at, id));
};
exports.insertChild = insertChild;
const removeChild = ({ id }, state) => {
    const children = [...(state.children || [])];
    const index = children.indexOf(id);
    if (index > -1) {
        children.splice(index, 1);
        return (0, f_8.default)(state, 'children', children);
    }
    return state;
};
exports.removeChild = removeChild;
const setSubComponent = ({ unitId, spec }, state) => {
    return (0, f_2.default)(state, ['subComponents', unitId], spec);
};
exports.setSubComponent = setSubComponent;
const removeSubComponent = ({ id }, state) => {
    state = (0, f_4.default)(state, ['subComponents', id]);
    return state;
};
exports.removeSubComponent = removeSubComponent;
const setSize = ({ defaultWidth, defaultHeight, }, state) => {
    return (0, f_7.default)(state, { defaultWidth, defaultHeight });
};
exports.setSize = setSize;
const setChildren = ({ children }, state) => {
    return (0, f_8.default)(state, 'children', children);
};
exports.setChildren = setChildren;
const setSubComponentSize = ({ id, width, height }, state) => {
    return (0, f_2.default)(state, ['subComponents', id], (0, f_7.default)(state.subComponents[id], { width, height }));
};
exports.setSubComponentSize = setSubComponentSize;
const setSubComponentChildren = ({ id, children }, state) => {
    return (0, f_2.default)(state, ['subComponents', id, 'children'], children);
};
exports.setSubComponentChildren = setSubComponentChildren;
const removeSubComponentChild = ({ id, childId }, state) => {
    const children = (0, f_3.default)(state, ['subComponents', id, 'children']);
    const { i } = (0, f_5.default)({ 'a[]': children, a: childId });
    const { a: _children } = (0, f_1.default)({ a: children, i });
    state = (0, f_2.default)(state, ['subComponents', id, 'children'], _children);
    return state;
};
exports.removeSubComponentChild = removeSubComponentChild;
const appendSubComponentChild = ({ id, childId }, state) => {
    const { subComponents } = state;
    const subComponent = subComponents[id] || {};
    const { children = [] } = subComponent;
    return (0, f_2.default)(state, ['subComponents', id, 'children'], [...children, childId]);
};
exports.appendSubComponentChild = appendSubComponentChild;
const insertSubComponentChild = ({ id, childId, at }, state) => {
    const { subComponents } = state;
    const subComponent = subComponents[id] || {};
    const { children = [] } = subComponent;
    return (0, f_2.default)(state, ['subComponents', id, 'children'], (0, f_6._insert)(children, at, childId));
};
exports.insertSubComponentChild = insertSubComponentChild;
function default_1(state = exports.defaultState, { type, data }) {
    switch (type) {
        case component_1.SET_SUB_COMPONENT:
            return (0, exports.setSubComponent)(data, state);
        case component_1.REMOVE_SUB_COMPONENT:
            return (0, exports.removeSubComponent)(data, state);
        case component_1.SET_SIZE:
            return (0, exports.setSubComponentSize)(data, state);
        case component_1.SET_SUB_COMPONENT_CHILDREN:
            return (0, exports.setSubComponentChildren)(data, state);
        default:
            return state;
    }
}
exports.default = default_1;
//# sourceMappingURL=component.js.map