import { __decorate, __metadata } from "tslib";
import React from "react";
import { DataUtilities } from "../../Common/DataUtilities";
import { AutoBind } from "../../Common/AutoBind";
import { StateManager } from "../../CommonUI/StateManagement/StateManager";
export class XBaseControl extends React.Component {
    constructor() {
        super({});
        // generates ID to be a guid
        this.id = DataUtilities.generateUniqueId();
        this.ref = React.createRef();
        this.canFocus = true;
    }
    focusControl() {
        this.ref.current.focus();
    }
    ;
    get controlRenderContext() {
        var _a;
        return (_a = this.getFormContext()) === null || _a === void 0 ? void 0 : _a.getControlContext(this);
    }
    getFormContext() {
        return this._formContext;
    }
    setFormContext(value) {
        if (!value)
            throw new Error("cannot set edit context to null (base control)");
        this._formContext = value;
    }
    getStorageData() {
        return DataUtilities.cloneWithoutReact(this, ["owner", "_formContext", "_formRuntimeContext"]);
    }
    setStorageData(data) {
        DataUtilities.spreadData(this, data, []);
    }
    // notifies state manager that this control has been updated
    controlUpdate() {
        this.getFormContext().computeDesignValidationIssues();
        StateManager.changed(this);
        // StateManager.propertyChanged(this.getFormContext()!, this.id);
    }
    // forces refresh of entire designer (expensive)
    designerUpdate() {
        var _a;
        (_a = this.getFormContext()) === null || _a === void 0 ? void 0 : _a.refreshDesigner();
    }
    isDesignSelected() {
        if (this.getFormContext()) {
            return this.getFormContext().getControlContext(this).selected;
        }
        return false;
    }
    selectInDesigner() {
        //        console.log('calling select in designer')
        if (this.getFormContext()) {
            this.getFormContext().selectControl(this.id);
        }
    }
    unselectInDesigner() {
        if (this.getFormContext()) {
            this.getFormContext().unSelectControl();
        }
    }
    getFormData() {
        var _a;
        return (_a = this.getFormContext()) === null || _a === void 0 ? void 0 : _a.getFormData();
    }
    getFormDataValue(fieldName) {
        var _a;
        return (_a = this.getFormContext()) === null || _a === void 0 ? void 0 : _a.getFormDataValue(fieldName);
    }
    setFormData(value) {
        var _a;
        return (_a = this.getFormContext()) === null || _a === void 0 ? void 0 : _a.setFormData(value);
    }
    setFormDataValue(fieldName, value) {
        var _a;
        (_a = this.getFormContext()) === null || _a === void 0 ? void 0 : _a.setFormDataValue(fieldName, value);
    }
    toString() {
        let type = Reflect.get(this, "__type");
        if (type && type.startsWith("X")) {
            type = type.substring(1);
        }
        let valueName = Reflect.get(this, "valueName");
        if (type && valueName) {
            return type + " [" + valueName + "]";
        }
        else if (type) {
            return type + " [" + valueName + "]";
        }
        else if (valueName) {
            return valueName;
        }
        return null;
    }
}
__decorate([
    AutoBind,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], XBaseControl.prototype, "controlUpdate", null);
__decorate([
    AutoBind,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], XBaseControl.prototype, "designerUpdate", null);
//# sourceMappingURL=XBaseControl.js.map