"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PickerHelper = (Cloudwise, Logger) => {
    return {
        attachToRender(picker) {
            if (picker.render !== undefined) {
                const renderOrig = picker.render.bind(picker);
                picker.render = () => {
                    return this.renderPicker(renderOrig);
                };
            }
        },
        renderPicker(renderOrig) {
            const pickerRender = renderOrig();
            if (pickerRender && pickerRender.props && pickerRender.props.onValueChange) {
                return Object.assign(Object.assign({}, pickerRender), { props: Object.assign(Object.assign({}, pickerRender.props), { onValueChange: (itemValue, itemNumber) => {
                            this.onValueChange(itemValue, itemNumber, pickerRender);
                        } }) });
            }
            else {
                return pickerRender;
            }
        },
        onValueChange(itemValue, itemNumber, picker) {
            let action;
            let value = this.getPickerItemValue(picker, itemNumber, itemValue);
            Logger.log(`Enter Action - Picked Value: ${value} with index: ${itemNumber}`);
            action = Cloudwise.enterAction(`Picked Value: ${value}`);
            if (picker.props.onValueChange !== undefined) {
                picker.props.onValueChange(itemValue, itemNumber);
            }
            if (action) {
                action.leaveAction();
            }
        },
        getPickerItemValue(picker, index, value) {
            if (picker && index >= 0) {
                let children = picker.props.children;
                if (children && children.length >= index && isPickerItemProps(children[index].props)) {
                    return children[index].props.label;
                }
            }
            return value;
        }
    };
};
function isPickerItemProps(props) {
    return props.label !== undefined;
}
