'use strict';

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

const index = require('./index-51e8292e.js');
const util = require('./util-d383acb8.js');
const environmentConfig_service = require('./environment-config.service-0e02b049.js');
const assets_service = require('./assets.service-0c759d51.js');
const feed_service = require('./feed.service-b4f09441.js');
require('./httpService-7211d93b.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');

const sparkleGoalFormCss = ":host{display:block}";

const SparkleGoalForm = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.name = {
      valid: false,
      value: ''
    };
    this.description = {
      valid: false,
      value: ''
    };
    this.category = {
      valid: false,
      value: ''
    };
    this.progress = 0;
    this.public = true;
    this.goalUpdateText = {
      valid: true,
      value: ''
    };
    this.submitted = false;
  }
  goalHanlder(data) {
    this.updateGoal(data.AppData);
  }
  ;
  componentWillLoad() {
    if (this.appData) {
      this.updateGoal(this.appData.AppData);
    }
  }
  updateGoal(data) {
    if (data) {
      this.name = Object.assign(Object.assign({}, this.name), { value: data.Name });
      this.description = Object.assign(Object.assign({}, this.description), { value: data.Description });
      this.category = Object.assign(Object.assign({}, this.category), { value: data.Category });
      this.goalUpdateText = Object.assign(Object.assign({}, this.goalUpdateText), { value: data.GoalText });
      this.progress = data.Percentage;
    }
  }
  handleTitle(ev) {
    this.validateTitle();
    this.name = Object.assign(Object.assign({}, this.name), { value: ev.target.value });
  }
  handleDescription(ev) {
    this.validateDescription();
    this.description = Object.assign(Object.assign({}, this.description), { value: ev.target.value });
  }
  handleCategory(ev) {
    this.validateCategoy();
    this.category = Object.assign(Object.assign({}, this.category), { value: ev.target.value });
  }
  handleGoalProgressInfo(ev) {
    this.goalUpdateText = Object.assign(Object.assign({}, this.goalUpdateText), { value: ev.target.value });
  }
  validateTitle() {
    if (this.name.value && this.name.value.length > 0) {
      this.name = Object.assign(Object.assign({}, this.name), { valid: true });
      return;
    }
    this.name = Object.assign(Object.assign({}, this.name), { valid: false });
  }
  validateDescription() {
    if (this.description.value && this.description.value.length > 0) {
      this.description = Object.assign(Object.assign({}, this.description), { valid: true });
      return;
    }
    this.description = Object.assign(Object.assign({}, this.description), { valid: false });
  }
  validateCategoy() {
    if (this.category.value && this.category.value.length > 0) {
      this.category = Object.assign(Object.assign({}, this.category), { valid: true });
      return;
    }
    this.category = Object.assign(Object.assign({}, this.category), { valid: false });
  }
  validateGoalUpdateText() {
    if (this.goalUpdateText.value && this.goalUpdateText.value.length > 0) {
      this.goalUpdateText = Object.assign(Object.assign({}, this.goalUpdateText), { valid: true });
      return;
    }
    this.goalUpdateText = Object.assign(Object.assign({}, this.goalUpdateText), { valid: false });
  }
  async onSubmit(e) {
    e.preventDefault();
    console.log('clicked signup');
    this.validateTitle();
    this.validateDescription();
    this.validateCategoy();
    this.submitted = true;
    if (this.appData && this.appData.Id > 0) {
      this.validateGoalUpdateText();
      if (this.name.valid && this.description.valid && this.category.valid && this.goalUpdateText.valid) {
        //update
        var appData = util.GetGoalAppData(this.name.value, this.description.value, this.category.value, this.progress, this.goalUpdateText.value);
        const result = await feed_service.AppDataService.getInstance().updateGoal({ Id: this.appData.Id, MiniAppId: util.SparkleGlobal.MY_GOALS_APP_ID, AppJsonData: JSON.stringify(appData) });
        if (result.Succeeded) {
          if (this.public) {
            await this.postToFeed();
          }
          else {
            this.dismiss();
          }
        }
      }
    }
    else {
      if (this.name.valid && this.description.valid && this.category.valid) {
        //add
        var appData = util.GetGoalAppData(this.name.value, this.description.value, this.category.value, this.progress, this.goalUpdateText.value);
        const result = await feed_service.AppDataService.getInstance().createGoal({ MiniAppId: util.SparkleGlobal.MY_GOALS_APP_ID, AppJsonData: JSON.stringify(appData) });
        if (result.Succeeded) {
          this.dismiss();
        }
      }
    }
  }
  async postToFeed() {
    const sparkleConfig = environmentConfig_service.EnvironmentConfigService.getInstance().get('sparkle');
    const tocData = await assets_service.AssetsService.getInstance().loadJsonFile(sparkleConfig.tocPath);
    var model = {
      CourseId: tocData.id,
      MiniAppId: util.SparkleGlobal.MY_GOALS_APP_ID,
      PostText: util.MyGoalTemplate(this.name.value, this.goalUpdateText.value, this.progress),
      PostTextJSON: util.MyGoalTemplateJSON(this.name.value, this.goalUpdateText.value, this.progress),
      IsPublic: this.public,
      ExternalResourceUrl: "",
    };
    const result = await feed_service.FeedService.getInstance().createPost(model);
    if (result.Succeeded) {
      this.dismiss();
    }
  }
  dismiss(data) {
    // dismiss the closest modal and optionally pass back data
    this.el.closest('ion-modal').dismiss({
      'dismissed': true
    });
  }
  renderEditBookingFields() {
    if (this.appData) {
      return (index.h("div", null, index.h("ion-item", null, index.h("ion-label", { position: "stacked", color: "primary" }, "Goal Progress Info"), index.h("ion-input", { name: "title", type: "text", value: this.goalUpdateText.value, onInput: (ev) => this.handleGoalProgressInfo(ev) })), index.h("ion-text", { color: "danger" }, index.h("p", { hidden: this.goalUpdateText.valid || this.submitted === false, class: "ion-padding-start" }, "Goal Progress text is required")), index.h("ion-item", null, index.h("ion-label", { position: "stacked" }, "Progress"), index.h("ion-range", { value: this.progress, min: 1, max: 100, step: 1, snaps: true, pin: true, onIonChange: (e) => this.progress = +e.detail.value }, index.h("ion-icon", { size: "small", slot: "start", name: "pulse" }), index.h("ion-icon", { name: "pulse", size: "small" }), index.h("strong", { slot: "end" }, this.progress))), index.h("ion-text", { color: "danger" }, index.h("p", { hidden: this.progress > 0 || this.submitted === false, class: "ion-padding-start" }, "Min Progress value should be 1")), index.h("ion-item", { lines: "none" }, index.h("ion-label", null, "Share to Community Wall"), index.h("ion-checkbox", { slot: "start", onChange: (ev) => this.public = ev.detail.checked, checked: true }))));
    }
  }
  render() {
    return [
      index.h("div", null, index.h("ion-toolbar", { color: "primary" }, index.h("ion-buttons", { slot: "start" }, index.h("ion-back-button", { defaultHref: "home", onClick: () => this.dismiss() })), index.h("ion-title", null, "Add New Goal")), index.h("div", { class: "inner-content" }, index.h("form", { class: "ion-padding", novalidate: true, onSubmit: (e) => this.onSubmit(e) }, index.h("ion-list", { "no-lines": true }, index.h("ion-item", null, index.h("ion-label", { position: "stacked", color: "primary" }, "Title"), index.h("ion-input", { name: "title", type: "text", value: this.name.value, onInput: (ev) => this.handleTitle(ev), required: true })), index.h("ion-text", { color: "danger" }, index.h("p", { hidden: this.name.valid || this.submitted === false, class: "ion-padding-start" }, "Title is required")), index.h("ion-item", null, index.h("ion-label", { position: "stacked", color: "primary" }, "Description"), index.h("ion-textarea", { name: "desciption", value: this.description.value, onInput: (ev) => this.handleDescription(ev), required: true })), index.h("ion-text", { color: "danger" }, index.h("p", { hidden: this.description.valid || this.submitted === false, class: "ion-padding-start" }, "Description is required")), index.h("ion-item", null, index.h("ion-label", null, "Category"), index.h("ion-select", { value: this.category.value, onIonChange: (e) => this.handleCategory(e) }, index.h("ion-select-option", { value: "Emotional" }, "Emotional"), index.h("ion-select-option", { value: "Interpersonal" }, "Interpersonal"), index.h("ion-select-option", { value: "Learning" }, "Learning"), index.h("ion-select-option", { value: "Financial" }, "Financial"), index.h("ion-select-option", { value: "Physical" }, "Physical"))), index.h("ion-text", { color: "danger" }, index.h("p", { hidden: this.category.valid || this.submitted === false, class: "ion-padding-start" }, "Category is required")), this.renderEditBookingFields()), index.h("div", { class: "ion-padding" }, this.appData && index.h("ion-button", { type: "submit", expand: "block", disabled: feed_service.goalStore.state.goalLoading || feed_service.feedStore.state.postIdLoading > 0 }, feed_service.goalStore.state.goalLoading && index.h("ion-spinner", { name: "dots" }), "  Update"), !this.appData && index.h("ion-button", { type: "submit", expand: "block", disabled: feed_service.goalStore.state.goalLoading }, feed_service.goalStore.state.goalLoading && index.h("ion-spinner", { name: "dots" }), "  Add")))))
    ];
  }
  get el() { return index.getElement(this); }
  static get watchers() { return {
    "appData": ["goalHanlder"]
  }; }
};
SparkleGoalForm.style = sparkleGoalFormCss;

exports.sparkle_goal_form = SparkleGoalForm;
