"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("react");
var sdk_1 = require("@builder.io/sdk");
var react_native_1 = require("react-native");
var BuilderContent = /** @class */ (function (_super) {
    __extends(BuilderContent, _super);
    function BuilderContent() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            loading: true,
            data: null,
        };
        _this.subscriptions = new sdk_1.Subscription();
        _this.firstLoad = true;
        _this.clicked = false;
        _this.trackedImpression = false;
        _this.onClick = function (reactEvent) {
            // TODO: viewport scrolling tracking for impression events
            var event = reactEvent.nativeEvent;
            var content = _this.state.data;
            if (!content) {
                return;
            }
            if (sdk_1.builder.autoTrack) {
                // TODO
                // builder.trackInteraction(content.id, content.variationId, this.clicked, event)
            }
            if (!_this.clicked) {
                _this.clicked = true;
            }
        };
        return _this;
    }
    // TODO: observe model name for changes
    BuilderContent.prototype.componentDidMount = function () {
        // Temporary to test metrics diving in with bigquery and heatmaps
        // builder.autoTrack = true;
        // builder.env = 'development';
        this.subscribeToContent();
        ///REACT15ONLY if (this.ref) { this.ref.setAttribute('builder-model', this.props.modelName); }
    };
    BuilderContent.prototype.subscribeToContent = function () {
        var _this = this;
        if (this.props.modelName !== '_inline') {
            // TODO:... using targeting...? express.request hmmm
            this.subscriptions.add(sdk_1.builder.queueGetContent(this.props.modelName, this.props.options).subscribe(function (matches) {
                var match = matches && matches[0];
                _this.setState({
                    data: match,
                });
                if (match && _this.firstLoad) {
                    // TODO: autoTrack
                    if (sdk_1.builder.autoTrack) {
                        _this.trackedImpression = true;
                        sdk_1.builder.trackImpression(match.id, match.variationId);
                    }
                    _this.firstLoad = false;
                }
                if (_this.props.contentLoaded) {
                    _this.props.contentLoaded(match && match.data);
                }
            }, function (error) {
                if (_this.props.contentError) {
                    _this.props.contentError(error);
                }
            }));
        }
    };
    BuilderContent.prototype.componentWillUnmount = function () {
        this.subscriptions.unsubscribe();
    };
    BuilderContent.prototype.render = function () {
        var _a = this.state, data = _a.data, loading = _a.loading;
        // TODO: why is this server only? any time there is initial content
        // use it no?
        //
        var useData = ((this.props.inline || !sdk_1.Builder.isBrowser) &&
            this.props.options &&
            this.props.options.initialContent &&
            this.props.options.initialContent[0]) ||
            data;
        return (
        // TODO: use fragment
        // <TouchableWithoutFeedback
        react_1.default.createElement(react_native_1.View
        // ref={ref => (this.ref = ref)}
        // onPress={this.onClick}
        , { "builder-content-id": useData && useData.id, "builder-model": this.props.modelName }, this.props.children(useData && useData.data, loading, useData)));
    };
    return BuilderContent;
}(react_1.default.Component));
exports.BuilderContent = BuilderContent;
//# sourceMappingURL=builder-content.component.js.map