import CalendarUtils from '@dvn/core/util/calendar';
import { SozlukApi } from '@dvn/sozluk/api';
import { Component, Event, h, Host, Prop, State } from '@stencil/core';
import { sozlukEntries } from '../sozluk-entries';
import { mentornityAnnouncementWrapperI18nDefault, MentornityAnnouncementWrapperI18nKey } from './i18n';
import { MentornityAnnouncementPublishBridgeApi } from './mentornity-announcement-publish-bridge-api';
export class MentornityAnnouncementPublish {
  constructor() {
    this.itemsSelectedCount = 0;
    this.isLoading = false;
    this.setDictionaryLanguage = async () => {
      const apiSozluk = new SozlukApi().setLanguage(this.language).setContents(sozlukEntries);
      this.strings = await apiSozluk.getContent(MentornityAnnouncementWrapperI18nKey, mentornityAnnouncementWrapperI18nDefault);
    };
  }
  componentWillLoad() {
    this.api = new MentornityAnnouncementPublishBridgeApi(this.selectedUsers);
    this.setDictionaryLanguage();
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const { strings } = this;
    if (!strings)
      return;
    const hasSentEmails = ((_b = (_a = this.announcement) === null || _a === void 0 ? void 0 : _a.targetList) === null || _b === void 0 ? void 0 : _b.length) > 0;
    const sentEmailsAt = ((_d = (_c = this.announcement) === null || _c === void 0 ? void 0 : _c.sentEmailsAt) === null || _d === void 0 ? void 0 : _d.length) > 0 && CalendarUtils.format(((_e = this.announcement) === null || _e === void 0 ? void 0 : _e.sentEmailsAt[((_g = (_f = this.announcement) === null || _f === void 0 ? void 0 : _f.sentEmailsAt) === null || _g === void 0 ? void 0 : _g.length) - 1]) * 1000, `DD-MM-YYYY - ${CalendarUtils.timeFormat === '12-hour' ? 'hh:mm A' : 'HH:mm'}`); // get latest sent date
    const hasSelectedUsers = this.selectedUsers.length > 0;
    return (h(Host, { class: "row", style: { overflow: 'hidden' } },
      h("div", { class: "col-12" },
        h("dvn-card", { leanedSides: true, outlined: true, cornerRadius: 5 },
          h("div", { class: "d-flex justify-content-between align-items-center" },
            h("div", null,
              h("h4", null, strings.publishOnPlatform),
              h("p", null, strings.publishDescription)),
            h("dvn-switch", { tint: "secondary", scale: "small", onChange: () => this.updateStatusAnnouncement.emit(this.announcement), checked: ((_h = this.announcement) === null || _h === void 0 ? void 0 : _h.status) === 'published' ? true : false })))),
      h("div", { class: "col-12" },
        h("dvn-card", { leanedSides: true, outlined: true, cornerRadius: 5 },
          !hasSelectedUsers &&
            h("dvn-alert", { tint: "danger", message: strings.usersSelectedAlert }),
          h("div", { class: "d-flex justify-content-between align-items-center" },
            h("div", null,
              h("h4", null, strings.mailing),
              h("p", null, strings.mailingDescription)),
            h("dvn-button", { tint: "primary", leaned: true, disabled: !hasSelectedUsers, onClick: () => {
                if (hasSelectedUsers)
                  this.sendEmails.emit();
              } }, strings.startMailing)),
          hasSentEmails &&
            h("div", { class: "d-flex align-items-center" },
              h("p", { style: { color: 'var(--color-neutral)' }, class: "mb-0 mr-2" },
                " ",
                strings.lastMailing,
                " ",
                sentEmailsAt),
              h("strong", { style: { cursor: 'pointer' }, onClick: () => this.openAnnouncementReport.emit(this.announcement) }, strings.checkReport))))));
  }
  static get is() { return "mentornity-announcement-publish"; }
  static get properties() { return {
    "isLoading": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "is-loading",
      "reflect": false,
      "defaultValue": "false"
    },
    "language": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "language",
      "reflect": false
    },
    "announcement": {
      "type": "any",
      "mutable": false,
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "announcement",
      "reflect": false
    },
    "selectedUsers": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "any[]",
        "resolved": "any[]",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "api": {},
    "itemsSelectedCount": {},
    "strings": {}
  }; }
  static get events() { return [{
      "method": "updateStatusAnnouncement",
      "name": "updateStatusAnnouncement",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "sendEmails",
      "name": "sendEmails",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "openAnnouncementReport",
      "name": "openAnnouncementReport",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "showErrorMessage",
      "name": "showErrorMessage",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
}
