/* eslint-disable max-len */
import { Component } from 'react';

import { mergeProps } from '../Util';

export interface DriftBotProps {
  appId: string;
  userId?: string;
  attributes?: Record<string, any>;
}

export default class DriftBot extends Component<DriftBotProps> {
  // eslint-disable-next-line react/static-property-placement
  static defaultProps: { userId: string; attributes: Record<string, any> } = {
    userId: '',
    attributes: {},
  };

  constructor(props: DriftBotProps) {
    super(mergeProps(props, DriftBot, ['attributes']) as DriftBotProps);

    this.addMainScript = this.addMainScript.bind(this);
    this.addIdentityVariables = this.addIdentityVariables.bind(this);
    this.insertScript = this.insertScript.bind(this);
  }

  insertScript(scriptText: string) {
    const script = document.createElement('script');
    script.innerText = scriptText;
    script.async = true;
    document.body.appendChild(script);
  }

  addMainScript() {
    const scriptText = `!function() {
        var t = window.driftt = window.drift = window.driftt || [];
        if (!t.init) {
          if (t.invoked) return void (window.console && console.error && console.error("Drift snippet included twice."));
          t.invoked = !0, t.methods = [ "identify", "config", "track", "reset", "debug", "show", "ping", "page", "hide", "off", "on" ],
          t.factory = function(e) {
            return function() {
              var n = Array.prototype.slice.call(arguments);
              return n.unshift(e), t.push(n), t;
            };
          }, t.methods.forEach(function(e) {
            t[e] = t.factory(e);
          }), t.load = function(t) {
            var e = 3e5, n = Math.ceil(new Date() / e) * e, o = document.createElement("script");
            o.type = "text/javascript", o.async = !0, o.crossorigin = "anonymous", o.src = "https://js.driftt.com/include/" + n + "/" + t + ".js";
            var i = document.getElementsByTagName("script")[0];
            i.parentNode.insertBefore(o, i);
          };
        }
      }();
      drift.SNIPPET_VERSION = '0.3.1';
      drift.load('${this.props.appId}');`;

    this.insertScript(scriptText);
  }

  addIdentityVariables() {
    if (typeof this.props.userId !== 'undefined') {
      const scriptText = `
        var t = window.driftt = window.drift = window.driftt || [];
        drift.identify('${this.props.userId}', ${JSON.stringify(this.props.attributes)})
      `;
      this.insertScript(scriptText);
    }
  }

  componentDidMount() {
    if (typeof window !== 'undefined') {
      this.addMainScript();
      this.addIdentityVariables();
    }
  }

  render() {
    return '';
  }
}
