import { r as registerInstance, h, d as getElement } from './index-24b18689.js';

const sparkleCompassPostCss = "gi-compass-post .compass-post{padding-top:10px}gi-compass-post .compass-post-header{padding:15px;background-color:rgba(213, 213, 213, 0.5);text-transform:uppercase;display:flex;justify-content:space-between;align-items:center}gi-compass-post .compass-post-header ion-avatar{width:32px;height:32px}gi-compass-post .compass-post-header h3{color:#333;padding-top:0px;padding-bottom:0px;margin:0px}gi-compass-post .compass-post-body{padding:15px;background-color:#fff}gi-compass-post .error{color:red}gi-compass-post .success{color:green}gi-compass-post .footer{display:flex;justify-content:space-between}";

const SparkleCompassPost = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.resiliance = 0;
    this.care = 0;
    this.compassData = {};
  }
  componentDidLoad() {
  }
  componentWillUpdate() {
    console.log('Component will update and re-render');
  }
  async renderCompass(data) {
    var sectionObj = {};
    for (var i = 0; i < data.length; i++) {
      if (data[i] && data[i].extraData && data[i].extraData.compass) {
        if (typeof sectionObj[data[i].extraData.compass] === 'undefined') {
          sectionObj[data[i].extraData.compass] = 0;
        }
        sectionObj[data[i].extraData.compass] += data[i].response || 0;
      }
    }
    for (var propertyName in sectionObj) {
      sectionObj[propertyName] = Math.floor(sectionObj[propertyName] * 0.25);
    }
    console.log(sectionObj);
  }
  loadOldData() {
    // this.dataSvc.getServerData("/api/compass/recent").subscribe((x) => {
    //   if (x && x.AppJsonData) {
    //     var obj = JSON.parse(x.AppJsonData)
    //     this.compassData = { ...this.compassData, ...obj };
    //     const compassCmp = this.el.querySelector('gi-compass') as HTMLGiCompassElement;
    //     compassCmp.drawOnCompass();
    //     // compassCmp.drawOnCompass()
    //   }
    // });
  }
  submitPost() {
    // this.errorMessage = "";
    // this.successMessage = "";
    // var previousEl = this.el.previousElementSibling as HTMLGiOpinionElement;
    // var data = previousEl.getQuizResults();
    // if (data.length == 8) {
    //   var sectionObj = {};
    //   for (var i = 0; i < data.length; i++) {
    //     if (data[i] && data[i].extraData && data[i].extraData.compass) {
    //       if (typeof sectionObj[data[i].extraData.compass] === 'undefined') {
    //         sectionObj[data[i].extraData.compass] = 0;
    //       }
    //       sectionObj[data[i].extraData.compass] += data[i].response;
    //     }
    //   }
    // this.dataSvc.getServerData("/api/compass/recent").subscribe(x => {
    //   combineObj = {};
    //   if (x && x.AppJsonData) {
    //     var oldObj = JSON.parse(x.AppJsonData);
    //     for (var propertyName in oldObj) {
    //       oldObj[propertyName] = Math.floor(oldObj[propertyName] * 4);
    //     }
    //     var combineObj = { ...oldObj, ...sectionObj };
    //   }
    //   else {
    //     combineObj = sectionObj;
    //   }
    //   var componentString = "<gi-compass";
    //   for (var propertyName in combineObj) {
    //     combineObj[propertyName] = Math.floor(combineObj[propertyName] * 0.25);
    //     componentString += ' ' + propertyName + '="' + combineObj[propertyName] + '"';
    //   }
    //   componentString += "></gi-compass>";
    //   var isPublic = this.el.getElementsByClassName("IsPublic")[0] as HTMLInputElement;
    //   var appJsonData = JSON.stringify(combineObj);
    //   var postData = {
    //     AppJsonData: appJsonData,
    //     PostText: componentString,
    //     ExternalResourceUrl: "",
    //     IsPublic: isPublic.checked,
    //   };
    //   console.log(postData);
    //   this.dataSvc.saveData(postData, "/api/compass/add/")
    //     .subscribe(x => {
    //       this.successMessage = "Posted Successfully";
    //     }, error => {
    //       this.errorMessage = "Something went wrong.";
    //     });
    // }, error => {
    //   this.errorMessage = "Something went wrong.";
    // });
    // }
    // else {
    //   this.errorMessage = "Please answer all of the above questions";
    // }
  }
  getTokenData() {
    let tokenStr = window.localStorage["currentUser"];
    if (tokenStr) {
      let token = JSON.parse(tokenStr);
      token = JSON.parse(token);
      if (new Date(token["Expiration"]).getTime() > new Date().getTime()) {
        return token;
      }
    }
    return null;
  }
  renderUserInfo() {
    var tokenInfo = this.getTokenData();
    if (tokenInfo != null && tokenInfo.Token) {
      return (h("ion-avatar", { class: "header-avatar", slot: "end" }, tokenInfo.ProfileImageUrl
        ? h("img", { src: tokenInfo.ProfileImageUrl })
        : h("img", { src: "/assets/img/avatar.png" })));
    }
    else {
      return (h("ion-button", { slot: "end", href: "/login" }, "Login to Post"));
    }
  }
  render() {
    const userInfo = this.renderUserInfo();
    return (h("div", { class: "compass-post" }, h("div", { class: "compass-post-header" }, "Bulletin Board", userInfo), h("div", { class: "compass-post-body" }, h("form", null, h("ion-item", null, h("ion-label", null, "Public"), h("ion-radio", { class: "IsPublic", slot: "end", color: "primary", value: "IsPublic", name: "IsPublic" })), h("gi-compass", null), h("ion-item", { class: "footer" }, h("div", null, h("div", { class: "error" }, this.errorMessage), h("div", { class: "success" }, this.successMessage)), h("ion-button", { slot: "end", size: "default", onClick: (evt) => this.submitPost(), disabled: this.getTokenData() == null }, "Post"))))));
  }
  get el() { return getElement(this); }
};
SparkleCompassPost.style = sparkleCompassPostCss;

export { SparkleCompassPost as sparkle_compass_post };
