"use strict";
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
    if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
    return cooked;
};
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 __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var react_apollo_1 = require("react-apollo");
var graphql_tag_1 = __importDefault(require("graphql-tag"));
var icons_1 = require("./icons");
var LoadingMessage_1 = require("./LoadingMessage");
exports.CHANNEL_BY_NEWSROOM_QUERY = graphql_tag_1.default(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n  query Channel($contractAddress: String!) {\n    channelsGetByNewsroomAddress(contractAddress: $contractAddress) {\n      id\n      currentUserIsAdmin\n      isStripeConnected\n      newsroom {\n        multisigAddress\n        charter {\n          name\n          tagline\n          logoUrl\n          newsroomUrl\n        }\n      }\n    }\n  }\n"], ["\n  query Channel($contractAddress: String!) {\n    channelsGetByNewsroomAddress(contractAddress: $contractAddress) {\n      id\n      currentUserIsAdmin\n      isStripeConnected\n      newsroom {\n        multisigAddress\n        charter {\n          name\n          tagline\n          logoUrl\n          newsroomUrl\n        }\n      }\n    }\n  }\n"])));
exports.CREATE_NEWSROOM_CHANNEL_MUTATION = graphql_tag_1.default(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n  mutation($newsroomContractAddress: String!) {\n    channelsCreateNewsroomChannel(newsroomContractAddress: $newsroomContractAddress) {\n      id\n      currentUserIsAdmin\n    }\n  }\n"], ["\n  mutation($newsroomContractAddress: String!) {\n    channelsCreateNewsroomChannel(newsroomContractAddress: $newsroomContractAddress) {\n      id\n      currentUserIsAdmin\n    }\n  }\n"])));
exports.NRSIGNUP_DELETE = graphql_tag_1.default(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n  mutation {\n    nrsignupDelete\n  }\n"], ["\n  mutation {\n    nrsignupDelete\n  }\n"])));
/** Usage: The component returned by this HOC will require the `newsroomAddress` or `newsroomContractAddress` prop and will pass `channelData` to the wrapped component, or instead will show loading or error states as necessary. If the newsroom channel does not yet exist, it will attempt to create it. */
exports.withNewsroomChannel = function (WrappedComponent) {
    return /** @class */ (function (_super) {
        __extends(ComponentWithNewsroomChannel, _super);
        function ComponentWithNewsroomChannel(props) {
            var _this = _super.call(this, props) || this;
            var newsroomAddress = _this.props.newsroomAddress || _this.props.newsroomContractAddress;
            if (!newsroomAddress || typeof newsroomAddress !== "string") {
                throw Error("Must supply `newsroomAddress` or `newsroomContractAddress` prop.");
            }
            _this.state = {
                newsroomAddress: newsroomAddress,
            };
            return _this;
        }
        ComponentWithNewsroomChannel.prototype.render = function () {
            var _this = this;
            return (React.createElement(react_apollo_1.Query, { query: exports.CHANNEL_BY_NEWSROOM_QUERY, variables: { contractAddress: this.state.newsroomAddress } }, function (_a) {
                var loading = _a.loading, error = _a.error, data = _a.data;
                if (loading) {
                    return React.createElement(LoadingMessage_1.LoadingMessage, null, "Loading");
                }
                else if (error && error.message.indexOf("record not found") !== -1) {
                    return _this.renderNoChannel();
                }
                else if (error || !data || !data.channelsGetByNewsroomAddress) {
                    console.error("error loading channel data for", _this.state.newsroomAddress, " error:", error, "data:", data);
                    return _this.renderError("Sorry, there was an error loading your newsroom information. Please try again later.");
                }
                return React.createElement(WrappedComponent, __assign({}, _this.props, { channelData: data.channelsGetByNewsroomAddress }));
            }));
        };
        ComponentWithNewsroomChannel.prototype.renderNoChannel = function () {
            var _this = this;
            return (React.createElement(react_apollo_1.Mutation, { mutation: exports.CREATE_NEWSROOM_CHANNEL_MUTATION, variables: { newsroomContractAddress: this.state.newsroomAddress }, refetchQueries: [
                    { query: exports.CHANNEL_BY_NEWSROOM_QUERY, variables: { contractAddress: this.state.newsroomAddress } },
                ] }, function (createChannel, _a) {
                var data = _a.data, error = _a.error, called = _a.called;
                if (!called && !error) {
                    console.warn("Channel doesn't exist for newsroom", _this.state.newsroomAddress, "attempting to create...");
                    createChannel().catch(function () { });
                }
                if (error || !data || !data.channelsCreateNewsroomChannel) {
                    console.error("Error creating new channel for newsroom with channelsCreateNewsroomChannel", error, data);
                    return _this.renderError("Sorry, there was an error initializing your newsroom information. Please try again later.");
                }
                if (!data.channelsCreateNewsroomChannel.currentUserIsAdmin) {
                    console.error("Error using channelsCreateNewsroomChannel, created channel doesn't have user as admin", data);
                    return _this.renderError("Sorry, there was an error assigning admin rights to your newsroom. Please try again later.");
                }
                return React.createElement(LoadingMessage_1.LoadingMessage, null, "Loading");
            }));
        };
        ComponentWithNewsroomChannel.prototype.renderError = function (message) {
            return (React.createElement(React.Fragment, null,
                React.createElement(icons_1.ErrorIcon, { width: 16, height: 16 }),
                " ",
                message));
        };
        return ComponentWithNewsroomChannel;
    }(React.Component));
};
var templateObject_1, templateObject_2, templateObject_3;
//# sourceMappingURL=WithNewsroomChannelHOC.js.map