import React, { Component } from 'react';
import { Image, Linking, View } from 'react-native';
import { TouchableOpacityLink } from './TouchableOpacityLink';
export class CMSBanner extends Component {
    constructor() {
        super(...arguments);
        this.state = {
            containerWidth: 0
        };
        this.handleOnLayout = (e) => {
            this.setState({
                containerWidth: e.nativeEvent.layout.width
            });
        };
        this.handlePress = (instance) => () => {
            if (this.props.onPress) {
                this.props.onPress(instance);
            }
            else {
                Linking.openURL(instance.Link)
                    .catch(e => console.log('Unable to open link', instance.Link));
            }
        };
        this.renderInstance = (instance, i) => {
            const image = instance['Retina-Image'] || instance.Image;
            const imageWidth = this.props.imageWidth || this.state.containerWidth;
            let imageHeight = null;
            if (typeof this.props.imageHeight === 'number') {
                imageHeight = this.props.imageHeight;
            }
            else if (imageWidth && image.height && image.width) {
                imageHeight = imageWidth * image.height / image.width;
            }
            else {
                imageHeight = this.state.containerWidth;
            }
            const ImageJsx = (<Image source={{ uri: image.path }} accessibilityLabel={instance.Title} resizeMode='contain' style={{
                width: imageWidth,
                height: imageHeight
            }}/>);
            if (instance.Link) {
                return (<View key={i} style={this.props.imageContainerStyle} onLayout={this.handleOnLayout}>
          <TouchableOpacityLink onPress={this.handlePress(instance)} href={instance.Link}>
            {ImageJsx}
          </TouchableOpacityLink>
        </View>);
            }
            return (<View key={i} style={this.props.imageContainerStyle} onLayout={this.handleOnLayout}>
        {ImageJsx}
      </View>);
        };
    }
}
//# sourceMappingURL=CMSBanner.js.map