"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.moveSubComponentRoot = exports._removeSubComponentFromParent = exports.reorderSubComponent = 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 deepSet_1 = require("../../deepSet");
const f_1 = require("../../system/f/object/Merge/f");
const f_2 = require("../../system/f/object/Set/f");
const array_1 = require("../../util/array");
const object_1 = require("../../util/object");
const component_1 = require("../util/component");
const appendChild = ({ childId }, state) => {
    var _a;
    state.children = (_a = state.children) !== null && _a !== void 0 ? _a : [];
    state.children.push(childId);
};
exports.appendChild = appendChild;
const insertChild = ({ childId, at }, state) => {
    const children = state.children || [];
    (0, array_1.insert)(children, childId, at);
};
exports.insertChild = insertChild;
const removeChild = ({ childId }, state) => {
    (0, array_1.pull)(state.children, childId);
};
exports.removeChild = removeChild;
const setSubComponent = ({ unitId, subComponent, }, state) => {
    (0, object_1.deepSet)(state, ['subComponents', unitId], subComponent);
};
exports.setSubComponent = setSubComponent;
const removeSubComponent = ({ unitId }, state) => {
    var _a;
    const subComponent = (0, object_1.deepGetOrDefault)(state, ['subComponents', unitId], {});
    const parentId = (0, component_1.getComponentSubComponentParentId)(state, unitId);
    const { children = [] } = subComponent;
    (0, object_1.deepDelete)(state, ['subComponents', unitId]);
    const index = (_a = state === null || state === void 0 ? void 0 : state.slots) === null || _a === void 0 ? void 0 : _a.findIndex(([_unitId]) => _unitId === unitId);
    if (index > -1) {
        (0, array_1.removeAt)(state.slots, index);
    }
    for (const childId of children) {
        if (parentId) {
            (0, exports.appendSubComponentChild)({ parentId, childId }, state);
        }
        else {
            (0, exports.appendChild)({ childId }, state);
        }
    }
};
exports.removeSubComponent = removeSubComponent;
const setSize = ({ defaultWidth, defaultHeight, }, state) => {
    return (0, f_1.default)(state, { defaultWidth, defaultHeight });
};
exports.setSize = setSize;
const setChildren = ({ children }, state) => {
    return (0, f_2.default)(state, 'children', children);
};
exports.setChildren = setChildren;
const setSubComponentSize = ({ id, width, height }, state) => {
    (0, deepSet_1.deepSet_)(state, ['subComponents', id], (0, f_1.default)(state.subComponents[id], { width, height }));
};
exports.setSubComponentSize = setSubComponentSize;
const setSubComponentChildren = ({ id, children }, state) => {
    (0, deepSet_1.deepSet_)(state, ['subComponents', id, 'children'], children);
};
exports.setSubComponentChildren = setSubComponentChildren;
const removeSubComponentChild = ({ subComponentId, childId }, state) => {
    const subComponent = (0, object_1.deepGet)(state, ['subComponents', subComponentId]);
    const { children = [], childSlot = {} } = subComponent;
    (0, array_1.pull)(children, childId);
    (0, object_1.deepDelete)(childSlot, [childId]);
};
exports.removeSubComponentChild = removeSubComponentChild;
const appendSubComponentChild = ({ parentId, childId }, state) => {
    var _a;
    const { subComponents } = state;
    subComponents[parentId] = (_a = subComponents[parentId]) !== null && _a !== void 0 ? _a : {};
    const subComponent = subComponents[parentId];
    subComponent.children = subComponent.children || [];
    const { children } = subComponent;
    (0, array_1.push)(children, childId);
};
exports.appendSubComponentChild = appendSubComponentChild;
const insertSubComponentChild = ({ parentId, childId, at }, state) => {
    var _a, _b;
    const { subComponents } = state;
    subComponents[parentId] = (_a = subComponents[parentId]) !== null && _a !== void 0 ? _a : {};
    const subComponent = subComponents[parentId];
    subComponent.children = (_b = subComponent.children) !== null && _b !== void 0 ? _b : [];
    const { children } = subComponent;
    (0, array_1.insert)(children, childId, at);
};
exports.insertSubComponentChild = insertSubComponentChild;
const reorderSubComponent = ({ parentId, childId, to }, state) => {
    var _a;
    if (parentId) {
        (0, array_1.reorder)((_a = state.subComponents[parentId].children) !== null && _a !== void 0 ? _a : [], childId, to);
    }
    else {
        (0, array_1.reorder)(state.children, childId, to);
    }
};
exports.reorderSubComponent = reorderSubComponent;
const _removeSubComponentFromParent = ({ parentId, children, slotMap }, state) => {
    for (const childId of children) {
        if (parentId) {
            (0, exports.removeChild)({ childId }, state);
            (0, exports.appendSubComponentChild)({ parentId, childId }, state);
        }
        else {
            (0, exports.appendChild)({ childId }, state);
        }
    }
};
exports._removeSubComponentFromParent = _removeSubComponentFromParent;
const moveSubComponentRoot = ({ parentId, children, slotMap }, state) => {
    for (const childId of children) {
        const currentParentId = (0, component_1.getComponentSubComponentParentId)(state, childId);
        if (currentParentId) {
            (0, exports.removeSubComponentChild)({ subComponentId: currentParentId, childId }, state);
        }
        else {
            (0, exports.removeChild)({ childId }, state);
        }
        if (parentId) {
            (0, exports.appendSubComponentChild)({ parentId, childId }, state);
        }
        else {
            (0, exports.appendChild)({ childId }, state);
        }
    }
};
exports.moveSubComponentRoot = moveSubComponentRoot;
//# sourceMappingURL=component.js.map