import {Helpers, SIBWidget, store} from 'https://unpkg.com/@startinblox/core@0.6';

class HDAppMail extends SIBWidget {
  get template() {
    return `<a href="mailto:${this.value}"><div class="icon-envelope"></div><div>SEND A MESSAGE</div></a>`;
  }
}

class HDAppMember extends SIBWidget {
  get template() {
    return `
        <div name="${this.name}">
          <img src="${this.value.avatar}"/>
        </div>
      `;
  }

  render() {
    store.get(this.value).then(value => {
      this._value = value;
      this.innerHTML = this.template;
    });
  }
}

class HDAppClosingDate extends SIBWidget {
  get template() {
    return this.value
      ? `<strong>closed</strong> (${this.value})`
      : '<strong>open</strong>';
  }

  render() {
    this.innerHTML = this.template;
  }
}

class HDAppAvailable extends SIBWidget {
  get template() {
    return this.value
      ? '<strong>Available</strong>'
      : '<strong>Not available</strong>';
  }

  render() {
    this.innerHTML = this.template;
  }
}

class HDAppHyperlink extends SIBWidget {
  get template() {
    const escaped = this.value
      .replace(/&/g, '&amp;')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;')
      .replace(/"/g, '&quot;')
      .replace(/'/g, '&#039;');
    return `<a href="${escaped}">${escaped}</a>`;
  }

  render() {
    this.innerHTML = this.template;
  }
}

class HDAppLinkMore extends SIBWidget {
  get template() {
    const id = Helpers.uniqID();
    return `
      ${this.label}
      ${this.name}
      ${this.escapedValue}
      `;
  }
}

class HDAppFormText extends SIBWidget {
  get template() {
    return `<p name="${this.name}">${this.value}</p>`;
  }

  render() {
    this.innerHTML = this.template;
  }
}

customElements.define('hdapp-mail', HDAppMail);
customElements.define('hdapp-member', HDAppMember);
customElements.define('hdapp-closing-date', HDAppClosingDate);
customElements.define('hdapp-available', HDAppAvailable);
customElements.define('hdapp-hyperlink', HDAppHyperlink);
customElements.define('hdapp-link-more', HDAppLinkMore);
customElements.define('hdapp-form-text', HDAppFormText);