"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.XBaseStackContainer = exports.OverlapDiv = void 0;
const tslib_1 = require("tslib");
const XBaseContainer_1 = require("./XBaseContainer");
const DataUtilities_1 = require("@appglue/common/dist/Common/DataUtilities");
const styled_components_1 = tslib_1.__importDefault(require("styled-components"));
const RegisterUIControl_1 = require("../Utilities/RegisterUIControl");
exports.OverlapDiv = (0, styled_components_1.default)("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;
`;
class XBaseStackContainer extends XBaseContainer_1.XBaseContainer {
    innerMargin;
    interControlSpacing;
    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_1.XBaseContainer) {
                    let found = c.find(id);
                    if (found)
                        return found;
                }
            }
        }
        return null;
    }
    getControls() {
        return this.controls;
    }
    getStorageData() {
        let retData = super.getStorageData();
        retData = DataUtilities_1.DataUtilities.addStorageData(this, retData, "innerMargin", "interControlSpacing");
        Reflect.set(retData, "__controls", this.controls.map((c) => {
            return c.getStorageData();
        }));
        return retData;
    }
    setStorageData(data) {
        super.setStorageData(data);
        DataUtilities_1.DataUtilities.setStorageData(this, data, "innerMargin", "interControlSpacing");
        let controlArray = Reflect.get(data, "__controls");
        if (controlArray && controlArray instanceof Array && controlArray.length !== 0) {
            for (let idx in controlArray) {
                let controlData = controlArray[idx];
                let typeName = Reflect.get(controlData, "__type");
                if (typeName) {
                    let registeredControl = RegisterUIControl_1.UIControlRegistration[typeName];
                    registeredControl = RegisterUIControl_1.UIControlRegistration[typeName];
                    if (!registeredControl) {
                        throw new Error(`${Reflect.get(data, "__type")} > ${Reflect.get(controlData, "label")}: type [${typeName}] is invalid`);
                    }
                    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);
                }
                if (!typeName) {
                    throw new Error(`${Reflect.get(data, "__type")} > ${Reflect.get(controlData, "label")}: type is required`);
                }
            }
        }
    }
    getInnerMargin() {
        return this.innerMargin || this.getFormContext()?.form?.defaultInnerContainerMargin;
    }
}
exports.XBaseStackContainer = XBaseStackContainer;
//# sourceMappingURL=XBaseStackContainer.js.map