"use strict";
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.connectIModelConnectionAndViewState = exports.connectIModelConnection = void 0;
/** @packageDocumentation
 * @module State
 */
const react_redux_1 = require("react-redux");
const UiFramework_1 = require("../UiFramework");
/** Private function that will map store's iModelConnection to the 'iModelConnection', 'iModel', and 'imodel' properties of props. This
 * is not ideal but it is a result of not having standard prop name for an iModelConnection.
 */
const iModeMapStateToProps = (mapStateToProps) => {
    return (state, ownProps) => {
        const frameworkState = state[UiFramework_1.UiFramework.frameworkStateKey]; // since app sets up key, don't hard-code name
        /* istanbul ignore next */
        if (!frameworkState) {
            if (mapStateToProps)
                return mapStateToProps(state, ownProps);
            return {};
        }
        const storeProps = {
            iModelConnection: frameworkState.sessionState.iModelConnection,
            imodel: frameworkState.sessionState.iModelConnection,
            iModel: frameworkState.sessionState.iModelConnection,
        };
        // istanbul ignore else
        if (mapStateToProps) {
            return {
                ...storeProps,
                ...mapStateToProps(state, ownProps),
            };
        }
        // istanbul ignore next
        return storeProps;
    };
};
/** Function that will map store's iModelConnection and defaultViewState to the props 'iModelConnection', 'iModel', 'imodel',
 * and 'viewState' property of props
 */
const iModelAndViewMapStateToProps = (mapStateToProps) => {
    // istanbul ignore next
    return (state, ownProps) => {
        const frameworkState = state[UiFramework_1.UiFramework.frameworkStateKey]; // since app sets up key, don't hard-code name
        /* istanbul ignore next */
        if (!frameworkState)
            return {};
        const props = {
            ...ownProps,
            iModelConnection: frameworkState.sessionState.iModelConnection,
            iModel: frameworkState.sessionState.iModelConnection,
            imodel: frameworkState.sessionState.iModelConnection,
            viewState: frameworkState.sessionState.defaultViewState,
        };
        if (mapStateToProps)
            return mapStateToProps(state, props);
        return props;
    };
};
/** Function that will connect a component to the IModelConnection data in the Redux store.
 * @param mapStateToProps optional user function that, if defined, will be executed to provide additional properties from store.
 * @param mapDispatchToProps data passed to Redux connect function.
 * @public
 */
const connectIModelConnection = (mapStateToProps, mapDispatchToProps) => {
    return (0, react_redux_1.connect)(iModeMapStateToProps(mapStateToProps), mapDispatchToProps);
};
exports.connectIModelConnection = connectIModelConnection;
/** Function that will connect a component to the IModelConnection data in the Redux store
 * @param mapStateToProps optional user function that, if defined, will be executed to provide additional properties from store.
 * @param mapDispatchToProps data passed to Redux connect function.
 * @beta
 *
 * @example
 *   // connect component for both getting and setting ImodelConnection data from Redux store.
 *  import { connectIModelConnection, sessionStateMapDispatchToProps, SessionStateActionsProps } from "@itwin/appui-react";
 *
 *  export interface ComponentProps extends SessionStateActionsProps {
 *    myData: string;
 *  }
 *
 *  export const ConnectControl = connectIModelConnection(null, sessionStateMapDispatchToProps)(ComponentClass);
 *
 *  //  this then allows connected control to update the store using a call like shown below.
 *  this.props.setNumItemsSelected(30);
 *
 */
const connectIModelConnectionAndViewState = (mapStateToProps, mapDispatchToProps) => {
    return (0, react_redux_1.connect)(iModelAndViewMapStateToProps(mapStateToProps), mapDispatchToProps);
};
exports.connectIModelConnectionAndViewState = connectIModelConnectionAndViewState;
//# sourceMappingURL=connectIModel.js.map