"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function isPressabilityInState(state) {
    return state.pressability != null;
}
exports.TouchableHelper = (Cloudwise, Logger) => {
    return {
        attachOnPressibility: function (touchable) {
            if (isPressabilityInState(touchable.state)) {
                const origOnPress = touchable.state.pressability._config.onPress;
                touchable.state.pressability._config.onPress = (...args) => {
                    this._createActionOnTouch(touchable, origOnPress, ...args);
                };
            }
        },
        attachHandleButton: function (button) {
            const renderOrig = button.render.bind(button);
            button.render = () => {
                let buttonRender = renderOrig();
                let buttonRenderCopy = Object.assign(Object.assign({}, buttonRender), { props: Object.assign(Object.assign({}, buttonRender.props), { onPress: (...args) => {
                            this._createActionOnTouch(buttonRender, buttonRender.props.onPress, args);
                        } }) });
                return buttonRenderCopy;
            };
        },
        attachHandlePress: function (touchable) {
            const touchableHandlePressOrig = touchable.touchableHandlePress;
            touchable.touchableHandlePress = (...args) => {
                this._createActionOnTouch(touchable, touchableHandlePressOrig, ...args);
            };
        },
        attachToOnPress: function (base) {
            const renderOrig = base.render;
            base.render = (...args) => {
                let baseRender = renderOrig(...args);
                let baseRenderCopy = Object.assign(Object.assign({}, baseRender), { props: Object.assign(Object.assign({}, baseRender.props), { onPress: (...args) => {
                            this._createActionOnTouch(baseRender, baseRender.props.onPress, ...args);
                        } }) });
                return baseRenderCopy;
            };
            return base;
        },
        _createActionOnTouch: function (element, onTouchFunction, ...args) {
            let action = this._createActionFromName(element);
            if (action != null) {
                if (onTouchFunction != null) {
                    onTouchFunction(...args);
                }
                action.leaveAction();
            }
        },
        _createActionFromName: function (element) {
            if (element.props.onPress != null) {
                let actionName;
                if (this._isPropsButton(element.props) && element.props.title != null) {
                    actionName = element.props.title;
                }
                else if (element.props.accessibilityLabel != null) {
                    actionName = element.props.accessibilityLabel;
                }
                else {
                    actionName = this._getTouchableTextName(element);
                }
                Logger.log(`Enter Action - Touch On - Name: ${actionName}`);
                return Cloudwise.enterAction(`Touch on ${actionName}`);
            }
            return null;
        },
        _isPropsButton(props) {
            return props.title != null;
        },
        _isImageButton(props) {
            return props.source != null;
        },
        _isImageURISourceType(source) {
            return source.uri != null;
        },
        _isIconButton(element) {
            let type = element.type;
            return type != null && type.name === "Icon";
        },
        _getTouchableTextName: function (touchable) {
            let name = null;
            if (this._isReactElement(touchable)) {
                name = this._walkTreeToFindText(touchable);
            }
            if (name == null) {
                return "Button";
            }
            else {
                return name;
            }
        },
        _walkTreeToFindText: function (element) {
            if (element == null || element.props == null) {
                return null;
            }
            else if (this._isImageButton(element.props) && this._isImageURISourceType(element.props.source)) {
                if (element.props.source.uri != null) {
                    return "Image Button: " + element.props.source.uri;
                }
                else {
                    return "Image Button";
                }
            }
            else if (this._isIconButton(element) && element.props.name != null) {
                return "Icon Button: " + element.props.name;
            }
            let childrenValue = element.props.children;
            if (typeof childrenValue === "string") {
                return childrenValue;
            }
            else if (Array.isArray(childrenValue)) {
                for (let i = 0; i < childrenValue.length; i++) {
                    let childrenValueElem = childrenValue[i];
                    if (this._isReactElement(childrenValueElem)) {
                        let name = this._walkTreeToFindText(childrenValueElem);
                        if (name != null) {
                            return name;
                        }
                    }
                }
                return null;
            }
            else {
                return this._walkTreeToFindText(childrenValue);
            }
        },
        _isReactElement(node) {
            return node != null && node.props != null && node.props.children != null;
        }
    };
};
