"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 = void 0;
const deepGet_1 = require("../../deepGet");
const deepSet_1 = require("../../deepSet");
const f_1 = require("../../system/core/array/RemoveIndex/f");
const f_2 = require("../../system/core/object/DeletePath/f");
const f_3 = require("../../system/f/array/IndexOf/f");
const f_4 = require("../../system/f/array/Insert/f");
const f_5 = require("../../system/f/object/Merge/f");
const f_6 = require("../../system/f/object/Set/f");
const appendChild = ({ unitId }, state) => {
    const children = state.children || [];
    return (0, f_6.default)(state, 'children', [...children, unitId]);
};
exports.appendChild = appendChild;
const insertChild = ({ id, at }, state) => {
    const children = state.children || [];
    return (0, f_6.default)(state, 'children', (0, f_4._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_6.default)(state, 'children', children);
    }
    return state;
};
exports.removeChild = removeChild;
const setSubComponent = ({ unitId, spec }, state) => {
    return (0, deepSet_1.default)(state, ['subComponents', unitId], spec);
};
exports.setSubComponent = setSubComponent;
const removeSubComponent = ({ id }, state) => {
    state = (0, f_2.default)(state, ['subComponents', id]);
    return state;
};
exports.removeSubComponent = removeSubComponent;
const setSize = ({ defaultWidth, defaultHeight, }, state) => {
    return (0, f_5.default)(state, { defaultWidth, defaultHeight });
};
exports.setSize = setSize;
const setChildren = ({ children }, state) => {
    return (0, f_6.default)(state, 'children', children);
};
exports.setChildren = setChildren;
const setSubComponentSize = ({ id, width, height }, state) => {
    return (0, deepSet_1.default)(state, ['subComponents', id], (0, f_5.default)(state.subComponents[id], { width, height }));
};
exports.setSubComponentSize = setSubComponentSize;
const setSubComponentChildren = ({ id, children }, state) => {
    return (0, deepSet_1.default)(state, ['subComponents', id, 'children'], children);
};
exports.setSubComponentChildren = setSubComponentChildren;
const removeSubComponentChild = ({ id, childId }, state) => {
    const children = (0, deepGet_1.default)(state, ['subComponents', id, 'children']);
    const { i } = (0, f_3.default)({ 'a[]': children, a: childId });
    const { a: _children } = (0, f_1.default)({ a: children, i });
    state = (0, deepSet_1.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, deepSet_1.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, deepSet_1.default)(state, ['subComponents', id, 'children'], (0, f_4._insert)(children, at, childId));
};
exports.insertSubComponentChild = insertSubComponentChild;
//# sourceMappingURL=component.js.map