/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable deprecation/deprecation */
/** @packageDocumentation
 * @module SyncUi
 */
import * as React from "react";
import { SyncUiEventDispatcher } from "./SyncUiEventDispatcher";
// cSpell:Ignore Unmounting
/**
 * A component that expect its children to be a function that will be passed the current boolValue state.
 * @public @deprecated in 3.0. Refactored SynchUiEventDispatcher.
 */
export class BooleanSyncUiListener extends React.Component {
    constructor(props) {
        super(props);
        this._componentUnmounting = false;
        this._handleVisibilitySyncUiEvent = (args) => {
            /* istanbul ignore next */
            if (this._componentUnmounting)
                return;
            /* istanbul ignore else */
            if (this.props.eventIds.some((value) => args.eventIds.has(value.toLowerCase()))) {
                const boolValue = this.props.boolFunc();
                /* istanbul ignore else */
                if (this.state.boolValue !== boolValue) {
                    this.setState({ boolValue });
                }
            }
        };
        // istanbul ignore next
        this._hasNoChildren = (children) => React.Children.count(children) === 0;
        this.state = {
            boolValue: undefined !== props.defaultValue ? props.defaultValue : true,
        };
    }
    componentDidMount() {
        /* istanbul ignore else */
        if ((this.props.boolFunc !== undefined) && this.props.eventIds.length > 0)
            SyncUiEventDispatcher.onSyncUiEvent.addListener(this._handleVisibilitySyncUiEvent);
    }
    componentWillUnmount() {
        /* istanbul ignore else */
        if ((this.props.boolFunc !== undefined) && this.props.eventIds.length > 0) {
            this._componentUnmounting = true;
            SyncUiEventDispatcher.onSyncUiEvent.removeListener(this._handleVisibilitySyncUiEvent);
        }
    }
    render() {
        const { 
        // do not bleed our props
        children, eventIds, boolFunc, defaultValue, // eslint-disable-line @typescript-eslint/no-unused-vars
        // eslint-disable-next-line comma-dangle
        ...otherProps // pass-through props
         } = this.props;
        const boolValue = this.state.boolValue;
        return (typeof children === "function" ?
            children(boolValue, otherProps)
            : /* istanbul ignore next */ !this._hasNoChildren(children)
                ? React.Children.only(children)
                : null);
    }
}
//# sourceMappingURL=BooleanListener.js.map