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