"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormControl = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const DataUtilities_1 = require("@appglue/common/dist/Common/DataUtilities");
const AutoBind_1 = require("@appglue/common/dist/Common/AutoBind");
const StateManager_1 = require("@appglue/common/dist/Common/StateManager");
class FormControl extends react_1.default.Component {
    id;
    ref;
    canFocus;
    // not serialized
    _formContext;
    constructor() {
        super({});
        // generates ID to be a guid
        this.id = DataUtilities_1.DataUtilities.generateUniqueId();
        this.ref = react_1.default.createRef();
        this.canFocus = true;
    }
    get controlRenderContext() {
        return this.getFormContext()?.getControlContext(this);
    }
    focusControl() {
        this.ref.current.focus();
    }
    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_1.DataUtilities.addStorageData(this, undefined, "__type", "id", "canFocus");
    }
    setStorageData(data) {
        DataUtilities_1.DataUtilities.setStorageData(this, data, "__type", "id", "canFocus");
    }
    // notifies state manager that this control has been updated
    controlUpdate() {
        this.getFormContext().computeDesignValidationIssues();
        StateManager_1.StateManager.changed(this);
        // StateManager.propertyChanged(this.getFormContext()!, this.id);
    }
    // forces refresh of entire designer (expensive)
    designerUpdate() {
        this.getFormContext()?.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() {
        return this.getFormContext()?.getFormData();
    }
    getFormDataValue(fieldName) {
        return this.getFormContext()?.getFormDataValue(fieldName);
    }
    setFormData(value) {
        return this.getFormContext()?.setFormData(value);
    }
    setFormDataValue(fieldName, value) {
        this.getFormContext()?.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 "";
    }
}
tslib_1.__decorate([
    AutoBind_1.AutoBind
], FormControl.prototype, "controlUpdate", null);
tslib_1.__decorate([
    AutoBind_1.AutoBind
], FormControl.prototype, "designerUpdate", null);
exports.FormControl = FormControl;
//# sourceMappingURL=FormControl.js.map