"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObserveState = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const AutoBind_1 = require("@appglue/common/dist/Common/AutoBind");
const DataUtilities_1 = require("@appglue/common/dist/Common/DataUtilities");
const StateManager_1 = require("@appglue/common/dist/Common/StateManager");
/**
 <ObserveState
 listenTo={anobject}
 control={() => {
        return (

        );
    }}
 />


 **/
// export const ObserveState: React.FC<{
//     listenTo: object | null | undefined;
//     control: () => JSX.Element | React.ReactNode;
//     properties?: string[];
// }> = ({ listenTo, control, properties }) => {
//     let [_render, setRender] = useReducer((x) => x + 1, 0);
//     let [id, setId] = useState<string>();
//
//     useEffect(() => {
//         let observerId = id;
//         if (!observerId) {
//             observerId = DataUtilities.generateUniqueId();
//             setId(observerId);
//         }
//
//         if (listenTo && listenTo !== null)
//             StateManager.addObserver(
//                 listenTo,
//                 () => {
//                     console.log("rerender");
//                     setRender();
//                 },
//                 properties,
//                 observerId
//             );
//
//         return function cleanup() {
//             if (listenTo && listenTo !== null) StateManager.removeObserverByName(listenTo, id!);
//         };
//     }, [listenTo, properties]);
//
//     return <>{control()}</>;
// };
class ObserveState extends react_1.default.Component {
    render() {
        return this.props.control();
    }
    componentDidMount() {
        if (this.props.listenTo)
            StateManager_1.StateManager.addObserver(this.props.listenTo, this, this.props.properties || []);
    }
    componentDidUpdate(prevProps) {
        if (prevProps.listenTo !== this.props.listenTo) {
            if (prevProps.listenTo)
                StateManager_1.StateManager.removeObserver(prevProps.listenTo, this);
            if (this.props.listenTo)
                StateManager_1.StateManager.addObserver(this.props.listenTo, this, this.props.properties || []);
        }
    }
    componentWillUnmount() {
        if (this.props.listenTo)
            StateManager_1.StateManager.removeObserver(this.props.listenTo, this);
        if (this.props.onWillUnmount) {
            this.props.onWillUnmount();
        }
    }
    // shouldComponentUpdate(_nextProps: Readonly<{ listenTo: object | null | undefined; control: () => (JSX.Element | React.ReactNode); properties?: string[]; onWillUnmount?: () => void }>, _nextState: Readonly<{}>, _nextContext: any): boolean {
    //     return true;
    // }
    update() {
        this.setState({ key: DataUtilities_1.DataUtilities.generateUniqueId() });
        this.forceUpdate();
    }
}
tslib_1.__decorate([
    AutoBind_1.AutoBind
], ObserveState.prototype, "update", null);
exports.ObserveState = ObserveState;
//# sourceMappingURL=ObserveState.js.map