import { XBaseContainer } from "./XBaseContainer";
import { DataUtilities } from "../../Common/DataUtilities";
import styled from "styled-components";
import { UIControlRegistration } from "../Utilities/RegisterUIControl";
export const OverlapDiv = styled("div") `
    background: transparent;
    position: absolute;
    top: 0px;
    left: 0px;
    right: 0px;
    bottom: 0px;
    z-index: 100;
    filter: alpha(opacity = 0);
    border: ${(props) => (props.selected && props.border ? props.border : "none")};
    border-radius: 2px;
    cursor: initial;
`;
export class XBaseStackContainer extends XBaseContainer {
    constructor() {
        super(...arguments);
        this.controls = [];
    }
    add(control, index) {
        // set control order
        if (index === undefined || index === null) {
            this.controls.push(control);
        }
        else {
            this.controls.splice(index, 0, control);
        }
        if (this.getFormContext())
            control.setFormContext(this.getFormContext());
    }
    remove(control) {
        const index = this.controls.indexOf(control, 0);
        if (index > -1) {
            this.controls.splice(index, 1);
        }
    }
    setOrder(id, order) {
        let controlToMove;
        for (let c of this.controls) {
            if (c.id === id) {
                controlToMove = c;
            }
        }
        if (controlToMove) {
            this.remove(controlToMove);
            this.add(controlToMove, order);
        }
    }
    find(id) {
        if (Array.isArray(this.controls)) {
            for (let c of this.controls) {
                if (c.id === id) {
                    return c;
                }
                if (c instanceof XBaseContainer) {
                    let found = c.find(id);
                    if (found)
                        return found;
                }
            }
        }
        return null;
    }
    getControls() {
        return this.controls;
    }
    getStorageData() {
        let retData = DataUtilities.cloneWithoutReact(this, ["container", "form", "controls", "_formContext"]);
        Reflect.set(retData, "__controls", this.controls.map((c) => {
            return c.getStorageData();
        }));
        return retData;
    }
    setStorageData(data) {
        DataUtilities.spreadData(this, data, ["__controls"]);
        let controlArray = Reflect.get(data, "__controls");
        if (controlArray && controlArray instanceof Array && controlArray.length !== 0) {
            for (let controlData of controlArray) {
                let typeName = Reflect.get(controlData, "__type");
                if (typeName) {
                    let registeredControl = UIControlRegistration[typeName];
                    let val = new registeredControl.prototype.constructor();
                    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                    // @ts-ignore
                    let cc = val;
                    if (this.getFormContext())
                        cc.setFormContext(this.getFormContext());
                    cc.setStorageData(controlData);
                    this.controls.push(cc);
                }
            }
        }
    }
    getInnerMargin() {
        var _a, _b;
        return this.innerMargin || ((_b = (_a = this.getFormContext()) === null || _a === void 0 ? void 0 : _a.form) === null || _b === void 0 ? void 0 : _b.defaultInnerContainerMargin);
    }
}
//# sourceMappingURL=XBaseStackContainer.js.map