import DiscourseURL from 'discourse/lib/url';
import { createWidget } from 'discourse/widgets/widget';
import { h } from 'virtual-dom';
import { iconNode } from 'discourse/helpers/fa-icon';

export default createWidget('home-logo', {
  tagName: 'div.title',

  logo() {
    const { siteSettings } = this;
    const mobileView = this.site.mobileView;

    const mobileLogoUrl = siteSettings.mobile_logo_url || "";
    const showMobileLogo = mobileView && (mobileLogoUrl.length > 0);

    const logoUrl = siteSettings.logo_url || '';
    const title = siteSettings.title;

    if (!mobileView && this.attrs.minimized) {
      const logoSmallUrl = siteSettings.logo_small_url || '';
      if (logoSmallUrl.length) {
        return h('img#site-logo.logo-small', { attributes: { src: logoSmallUrl, width: 33, height: 33, alt: title } });
      } else {
        return iconNode('home');
      }
    } else if (showMobileLogo) {
      return h('img#site-logo.logo-big', { attributes: { src: mobileLogoUrl, alt: title } });
    } else if (logoUrl.length) {
      return h('img#site-logo.logo-big', { attributes: { src: logoUrl, alt: title } });
    } else {
      return h('h2#site-text-logo.text-logo', title);
    }
  },

  html() {
    return h('a', { attributes: { href: "/", 'data-auto-route': true } }, this.logo());
  },

  click(e) {
    // if they want to open in a new tab, let it so
    if (e.shiftKey || e.metaKey || e.ctrlKey || e.which === 2) { return true; }

    e.preventDefault();

    DiscourseURL.routeTo("/");
    return false;
  }
});

@@ -748,24 +748,43 @@
 go-small', %7B
+ key: 'logo-small',
  attributes:
@@ -968,32 +968,52 @@
 ogo.logo-big', %7B
+ key: 'logo-mobile',
  attributes: %7B s
@@ -1119,24 +1119,41 @@
 logo-big', %7B
+ key: 'logo-big',
  attributes:
@@ -1244,16 +1244,38 @@
 t-logo',
+ %7B key: 'logo-text' %7D,
  title);

Add a vdom key to the logo to avoid weird resizing