'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-51e8292e.js');
const assignment_service = require('./assignment.service-690fb59c.js');
const feed_service = require('./feed.service-8eca4de6.js');
const media_service = require('./media.service-8a1b9bc5.js');
const token_service = require('./token.service-7a41953d.js');
const util = require('./util-47e320b2.js');
require('./httpService-d2e7bf8f.js');
require('./auth.store-1e630a7d.js');
require('./overlays-32df265a.js');
require('./ionic-global-2cde9d3a.js');
require('./hardware-back-button-92d97ff8.js');
require('./helpers-c2496722.js');
require('./environment-config.service-c8ff1f3d.js');
require('./localstorage.service-6346a41d.js');

const sparkleFeedPostCss = ":host{display:block}.post-footer-container{display:flex;justify-content:flex-end}.post-footer{display:flex;justify-content:space-between;align-items:center}.error{color:red;margin-right:10px}.success{color:green;margin-right:10px}.uploaded-image{display:block;max-height:140px;width:auto !important}.public-post-toggle{display:flex;justify-content:flex-end;align-items:center}.assignment-name{display:none}";

const SparkleFeedPost = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
  }
  // @Prop({ connect: 'ion-toast-controller' })
  // toastCtrl: HTMLIonToastControllerElement;
  componentDidLoad() {
    this.postText = this.textValue;
    if (this.ref && this.ref.innerText) {
      this.assignmentName = this.ref.innerText;
      console.log(this.assignmentName);
    }
  }
  setText(text) {
    this.postText = text;
  }
  isAuthenticated() {
    return token_service.TokenService.getInstance().isAuthenticated();
  }
  async submitPost() {
    this.postLoading = true;
    this.errorMessage = '';
    this.successMessage = '';
    //let formData = new FormData(this.el.querySelector("form")) as any;
    //var postText = this.el.getElementsByClassName("PostText")[0] as HTMLInputElement;
    var postExternalUrl = this.el.getElementsByClassName('PostExternalUrl')[0];
    var isPublic = this.el.getElementsByClassName('IsPublic')[0];
    var data = {
      PostText: this.postText,
      IsPublic: isPublic.checked,
      ExternalResourceUrl: postExternalUrl.value,
      MiniAppId: util.SparkleGlobal.MY_GOALS_APP_ID,
      AssignmentName: this.assignmentName,
      AssignmentData: '',
      PostTextJSON: null,
    };
    const result = await feed_service.FeedService.getInstance().createPost(data);
    if (result.Succeeded) {
      this.postLoading = false;
      //show success toast
      this.successMessage = 'Posted Successfully.';
      this.postText = '';
      this.externalResourceUrl = '';
      this.uploadedImage = '';
    }
    else {
      this.postLoading = false;
      this.errorMessage = 'There is an error while posting to feed.';
    }
    if (this.assignmentName) {
      var assignmentModel = {
        AssignmentData: JSON.stringify({ text: data.PostText, url: data.ExternalResourceUrl }),
        AssignmentName: this.assignmentName,
      };
      await assignment_service.AssignmentService.getInstance().createAssignment(assignmentModel);
    }
  }
  async uploadImage(evt) {
    this.uploadImageLoading = true;
    var files = evt.target.files;
    let result = await media_service.MediaService.getInstance().uploadImage(files);
    if (result.Succeeded) {
      console.log(result);
      this.uploadImageLoading = false;
      this.uploadedImage = result.Data.length > 0 ? result.Data[0].FullImageUrl : '';
      this.externalResourceUrl = result.Data.length > 0 ? result.Data[0].FullImageUrl : '';
    }
    else {
      this.uploadImageLoading = false;
      this.errorMessage = 'There is an error while uploading an image.';
    }
  }
  selectFile() {
    var fileInput = this.el.querySelector('#fileInput');
    fileInput.click();
  }
  renderUserInfo() {
    let authUser = token_service.TokenService.getInstance().getAuthUserSync();
    //var tokenInfo = { Token: null, ProfileImageUrl: null };
    if (authUser && authUser.JWToken) {
      return (index.h("ion-avatar", { class: "header-avatar", slot: "end" }, authUser.PhotoUrl ? index.h("img", { src: authUser.PhotoUrl }) : index.h("img", { src: "/assets/img/user.png" })));
    }
    else {
      return (index.h("ion-button", { slot: "end", href: "/login" }, "Login to Post"));
    }
  }
  render() {
    const userInfo = this.renderUserInfo();
    return (index.h(index.Host, null, index.h("div", { class: "assignment-name", ref: el => (this.ref = el) }, index.h("slot", null)), index.h("ion-card", null, index.h("ion-item", { class: "header", lines: "full" }, userInfo), index.h("ion-card-content", null, index.h("form", null, index.h("ion-list", null, index.h("ion-item", { class: "padding" }, index.h("ion-textarea", { class: "PostText", required: true, placeholder: "Post Text", name: "PostText", value: this.postText })), index.h("ion-item", { class: "padding" }, index.h("ion-input", { class: "PostExternalUrl", required: true, type: "text", placeholder: "URL", name: "ExternalResourceUrl", value: this.externalResourceUrl })), this.uploadedImage ? (index.h("ion-item", { class: "ion-padding", lines: "none" }, index.h("img", { class: "uploaded-image", src: this.uploadedImage }))) : (''), index.h("ion-item", { lines: "none" }, index.h("input", { type: "file", id: "fileInput", onChange: evt => this.uploadImage(evt), style: { display: 'none' }, accept: "image/*" }), this.uploadImageLoading ? index.h("ion-spinner", { color: "primary" }) : '', index.h("ion-button", { slot: "start", onClick: evt => this.selectFile() }, index.h("ion-icon", { name: "add" }), "Upload Image")), index.h("div", { class: "public-post-toggle" }, index.h("ion-toggle", { class: "IsPublic", color: "primary", name: "IsPublic" }), index.h("div", null, "Public Post")), index.h("div", { class: "post-footer-container" }, index.h("div", { class: "post-footer" }, index.h("div", null, index.h("div", { class: "error" }, this.errorMessage), index.h("div", { class: "success" }, this.successMessage)), index.h("div", null, this.postLoading ? index.h("ion-spinner", { color: "primary" }) : ''), index.h("ion-button", { slot: "end", size: "default", onClick: evt => this.submitPost(), disabled: this.postLoading || !this.isAuthenticated() }, "Post")))))))));
  }
  get el() { return index.getElement(this); }
};
SparkleFeedPost.style = sparkleFeedPostCss;

exports.sparkle_feed_post = SparkleFeedPost;
