import EmoticonIcon from 'mdi-react/EmoticonIcon';
import * as React from 'react';
import { Link } from 'react-router-dom';
import { Subscription } from 'rxjs';
import { currentUser } from '../auth';
import { eventLogger } from '../tracking/eventLogger';
import { Toast } from './Toast';
import { daysActiveCount } from './util';
const HAS_DISMISSED_TOAST_KEY = 'has-dismissed-survey-toast';
export class SurveyCTA extends React.PureComponent {
    constructor() {
        super(...arguments);
        this.onClick = (score) => {
            eventLogger.log('SurveyButtonClicked', { marketing: { nps_score: score } });
            if (this.props.onClick) {
                this.props.onClick(score);
            }
        };
    }
    render() {
        return (React.createElement("div", { className: this.props.className }, Array(11)
            .fill(1)
            .map((_, i) => {
            const pressed = i === this.props.score;
            return (React.createElement(Link, { key: i, className: `btn btn-primary toast__rating-btn ${pressed ? 'active' : ''}`, "aria-pressed": pressed || undefined, 
                // tslint:disable-next-line:jsx-no-lambda
                onClick: () => this.onClick(i), to: `/survey/${i}`, target: this.props.openSurveyInNewTab ? '_blank' : undefined }, i));
        })));
    }
}
export class SurveyToast extends React.Component {
    constructor(props) {
        super(props);
        this.subscriptions = new Subscription();
        this.onClickScore = (score) => this.onDismiss();
        this.onDismiss = () => {
            localStorage.setItem(HAS_DISMISSED_TOAST_KEY, 'true');
            this.setState({ visible: false });
        };
        this.state = {
            user: null,
            visible: localStorage.getItem(HAS_DISMISSED_TOAST_KEY) !== 'true' && daysActiveCount === 3,
        };
        if (this.state.visible) {
            eventLogger.log('SurveyReminderViewed', { marketing: { sessionCount: daysActiveCount } });
        }
    }
    componentDidMount() {
        this.subscriptions.add(currentUser.subscribe(user => this.setState({ user })));
    }
    componentWillUnmount() {
        this.subscriptions.unsubscribe();
    }
    render() {
        if (!this.state.visible) {
            return null;
        }
        return (React.createElement(Toast, { icon: React.createElement(EmoticonIcon, { className: "icon-inline" }), title: "Tell us what you think", subtitle: "How likely is it that you would recommend Sourcegraph to a friend?", cta: React.createElement(SurveyCTA, { onClick: this.onClickScore, openSurveyInNewTab: true }), onDismiss: this.onDismiss }));
    }
}
//# sourceMappingURL=SurveyToast.js.map