import * as React from 'react';
import { Subscription } from 'rxjs';
import { browserExtensionInstalled } from '../tracking/analyticsUtils';
import { eventLogger } from '../tracking/eventLogger';
import { showDotComMarketing } from '../util/features';
import { Toast } from './Toast';
import { daysActiveCount } from './util';
const CHROME_EXTENSION_STORE_LINK = 'https://chrome.google.com/webstore/detail/dgjhfomjieaadpoljlnidmbgkdffpack';
const FIREFOX_EXTENSION_STORE_LINK = 'https://addons.mozilla.org/en-US/firefox/addon/sourcegraph/';
const HAS_DISMISSED_TOAST_KEY = 'has-dismissed-browser-ext-toast';
class BrowserExtensionToast extends React.Component {
    constructor(props) {
        super(props);
        this.subscriptions = new Subscription();
        this.onClickInstall = () => {
            this.props.onClickInstall();
            this.onDismiss();
        };
        this.onDismiss = () => {
            localStorage.setItem(HAS_DISMISSED_TOAST_KEY, 'true');
            this.setState({ visible: false });
        };
        this.state = {
            visible: false,
        };
    }
    componentDidMount() {
        // Display if we don't receive confirmation that the user already has
        // the extension installed within a short time.
        this.subscriptions.add(browserExtensionInstalled.subscribe(isInstalled => {
            const visible = !isInstalled &&
                showDotComMarketing &&
                localStorage.getItem(HAS_DISMISSED_TOAST_KEY) !== 'true' &&
                daysActiveCount === 1;
            this.setState({ visible });
            if (visible) {
                eventLogger.log('BrowserExtReminderViewed');
            }
        }));
    }
    componentWillUnmount() {
        this.subscriptions.unsubscribe();
    }
    render() {
        if (!this.state.visible) {
            return null;
        }
        return (React.createElement(Toast, { icon: React.createElement("img", { className: "logo-icon", src: this.props.browserLogoAsset }), title: "Get Sourcegraph on GitHub", subtitle: `Get code intelligence while browsing GitHub and reading PRs with the Sourcegraph ${this.props.browserName} extension`, cta: React.createElement("a", { target: "_blank", rel: "noopener", className: "btn btn-primary", href: this.props.link, onClick: this.onClickInstall }, "Install"), onDismiss: this.onDismiss }));
    }
}
export class ChromeExtensionToast extends React.Component {
    constructor() {
        super(...arguments);
        this.onClickInstall = () => eventLogger.log('BrowserExtInstallClicked', { marketing: { browser: 'Chrome' } });
    }
    render() {
        return (React.createElement(BrowserExtensionToast, { browserName: "Chrome", browserLogoAsset: "/.assets/img/logo-chrome.svg", onClickInstall: this.onClickInstall, link: CHROME_EXTENSION_STORE_LINK }));
    }
}
export class FirefoxExtensionToast extends React.Component {
    constructor() {
        super(...arguments);
        this.onClickInstall = () => eventLogger.log('BrowserExtInstallClicked', { marketing: { browser: 'Firefox' } });
    }
    render() {
        return (React.createElement(BrowserExtensionToast, { browserName: "Firefox", browserLogoAsset: "/.assets/img/logo-firefox.svg", onClickInstall: this.onClickInstall, link: FIREFOX_EXTENSION_STORE_LINK }));
    }
}
//# sourceMappingURL=BrowserExtensionToast.js.map