'use strict';

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

const index = require('./index-a7b0c73d.js');
const RichToolbarHelper = require('./RichToolbarHelper-ea12328f.js');
const index$1 = require('./index-1be40f19.js');
require('@sankhyalabs/core');

const ezLinkBuilderCss = ".link-builder-container.sc-ez-link-builder{font-family:'Roboto';color:#2b3a54;display:flex;flex-direction:column;gap:10px;justify-content:space-between}.header.sc-ez-link-builder{display:flex;font-weight:500;font-size:16px}.actions.sc-ez-link-builder{display:flex;justify-content:flex-end;gap:12px;margin-top:12px;width:100%}";

const EzLinkBuilder = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.executeToolbarCommand = index.createEvent(this, "executeToolbarCommand", 7);
    this.textToShow = undefined;
    this.link = undefined;
    this.openInNewGuide = false;
  }
  async show(text) {
    this._popupElement.opened = true;
    if (text) {
      this.textToShow = text;
    }
  }
  async hide() {
    this._popupElement.opened = false;
  }
  async componentWillLoad() {
    this.i18n = await index$1.initI18n();
  }
  async handleCancel() {
    this.clearValues();
    this.executeToolbarCommand.emit({ command: RichToolbarHelper.RICH_TOOLBAR_COMMANDS.FOCUS });
    await this.hide();
  }
  clearValues() {
    this.link = '';
    this.textToShow = '';
    this.openInNewGuide = false;
  }
  async handleCreateLink() {
    const tag = `<a href="${this.link}" ${this.openInNewGuide ? 'target="_blank" rel="noopener noreferrer"' : ''}>${this.textToShow ? this.textToShow : this.link}</a>`;
    this.executeToolbarCommand.emit({ command: RichToolbarHelper.RICH_TOOLBAR_COMMANDS.FORMAT, value: tag });
    this.clearValues();
    await this.hide();
  }
  async handleActionPopup(event) {
    if (event.detail === 'OK') {
      await this.handleCreateLink();
      return;
    }
    if (event.detail === 'CANCEL' || event.detail === 'CLOSE') {
      await this.handleCancel();
    }
  }
  getOkButtonStatus() {
    var _a;
    return ((_a = this.link) === null || _a === void 0 ? void 0 : _a.trim()) ? 'ENABLED' : 'DISABLED';
  }
  render() {
    return (index.h("ez-popup", { heightMode: 'auto', size: 'small', useHeader: false, ref: ref => this._popupElement = ref, onEzClosePopup: () => this.clearValues() }, index.h("div", null, index.h("ez-modal-container", { modalTitle: this.i18n("ez-link-builder.addLinkTitle"), modalSubTitle: this.i18n("ez-link-builder.addLinkSubtitle"), okButtonLabel: this.i18n("ez-link-builder.submit"), cancelButtonLabel: this.i18n("app.cancel"), onEzModalAction: this.handleActionPopup.bind(this), okButtonStatus: this.getOkButtonStatus() }, index.h("div", { class: 'link-builder-container' }, index.h("ez-text-input", { id: 'link_url_input', label: this.i18n("ez-link-builder.urlInputLabel"), value: this.link, onEzChange: (e) => this.link = e.detail }), index.h("ez-text-input", { id: 'link_text_input', label: this.i18n("ez-link-builder.textInputLabel"), value: this.textToShow, onEzChange: (e) => this.textToShow = e.detail }), index.h("ez-check", { label: this.i18n("ez-link-builder.openInNewPage"), value: this.openInNewGuide, onEzChange: (e) => this.openInNewGuide = e.detail }))))));
  }
};
EzLinkBuilder.style = ezLinkBuilderCss;

const ezRichToolbarCss = ".sc-ez-rich-toolbar-h{display:block;width:100%}.taskbar.sc-ez-rich-toolbar{display:flex;padding:8px;border-radius:4px 4px 0 0}.taskbar-group.sc-ez-rich-toolbar{display:flex;margin-right:15px;padding-right:15px;border-right:1px solid #ddd}.taskbar-group.sc-ez-rich-toolbar:last-child{border-right:none}.preview-btn.sc-ez-rich-toolbar{font-family:Roboto;font-size:14px;background:none;display:flex;align-items:center;padding:0 8px;border-radius:4px;cursor:pointer;color:var(--color--secondary, #383c45);transition:0.3s all ease-in-out;width:110px;justify-content:center}.preview-btn.sc-ez-rich-toolbar:hover{background-color:#f5f5f5}";

const EzRichToolbar = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.executeToolbarCommand = index.createEvent(this, "executeToolbarCommand", 7);
    this.showPreview = true;
    this.isPreviewMode = false;
    this.showConfigs = true;
    this.showTextFormat = true;
    this.showUndoRedo = true;
  }
  async componentWillLoad() {
    this.i18n = await index$1.initI18n();
  }
  handleActionTriggered(event) {
    this.executeToolbarCommand.emit({ command: event.detail.command, value: event.detail.value });
  }
  handleClickPreview() {
    this.executeToolbarCommand.emit({ command: RichToolbarHelper.RICH_TOOLBAR_COMMANDS.PREVIEW });
  }
  renderArrows() {
    if (!this.showUndoRedo)
      return;
    return (index.h("div", { id: "rich-toolbar-undo-redo", class: "taskbar-group" }, index.h("ez-rich-toolbar-arrows", null)));
  }
  renderLetters() {
    if (!this.showTextFormat)
      return;
    return (index.h("div", { id: "rich-toolbar-text-format", class: "taskbar-group" }, index.h("ez-rich-toolbar-letters", null)));
  }
  renderConfigs() {
    if (!this.showConfigs)
      return;
    return (index.h("div", { id: "rich-toolbar-options-configs", class: "taskbar-group" }, index.h("ez-rich-toolbar-configs", null)));
  }
  renderPreview() {
    if (!this.showPreview)
      return;
    return (index.h("div", { class: "taskbar-group" }, index.h("span", { id: 'rich-text-preview-btn', class: "preview-btn", title: this.getPreviewTitle(), onClick: this.handleClickPreview.bind(this) }, this.isPreviewMode ? this.i18n("ez-rich-text.edit") : this.i18n("ez-rich-text.preview"))));
  }
  getPreviewTitle() {
    return this.isPreviewMode
      ? this.i18n("ez-rich-text.title.edit")
      : this.i18n("ez-rich-text.title.preview");
  }
  render() {
    return (index.h(index.Host, null, index.h("div", { class: "taskbar" }, this.renderPreview(), this.renderArrows(), this.renderLetters(), this.renderConfigs())));
  }
};
EzRichToolbar.style = ezRichToolbarCss;

const ezRichToolbarArrowsCss = ".sc-ez-rich-toolbar-arrows-h{display:flex;gap:5px}";

const EzRichToolbarArrows = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
  }
  async componentWillLoad() {
    this.i18n = await index$1.initI18n();
  }
  render() {
    return (index.h(index.Host, null, index.h("ez-rich-toolbar-item", { icon: RichToolbarHelper.RICH_TOOLBAR_COMMAND_ICON.UNDO, command: RichToolbarHelper.RICH_TOOLBAR_COMMANDS.UNDO, title: this.i18n("ez-rich-text.title.undo") }), index.h("ez-rich-toolbar-item", { icon: RichToolbarHelper.RICH_TOOLBAR_COMMAND_ICON.REDO, command: RichToolbarHelper.RICH_TOOLBAR_COMMANDS.REDO, title: this.i18n("ez-rich-text.title.redo") })));
  }
};
EzRichToolbarArrows.style = ezRichToolbarArrowsCss;

const ezRichToolbarConfigsCss = ".sc-ez-rich-toolbar-configs-h{display:flex;gap:5px}";

const EzRichToolbarConfigs = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
  }
  async componentWillLoad() {
    this.i18n = await index$1.initI18n();
  }
  render() {
    return (index.h(index.Host, null, index.h("ez-rich-toolbar-item", { icon: RichToolbarHelper.RICH_TOOLBAR_COMMAND_ICON.LIST, command: RichToolbarHelper.RICH_TOOLBAR_COMMANDS.FORMAT, value: RichToolbarHelper.RICH_TOOLBAR_FORMAT_TAGS.LIST, title: this.i18n("ez-rich-text.title.list") }), index.h("ez-rich-toolbar-item", { icon: RichToolbarHelper.RICH_TOOLBAR_COMMAND_ICON.IMAGE, command: RichToolbarHelper.RICH_TOOLBAR_COMMANDS.IMAGE, title: this.i18n("ez-rich-text.title.image") }), index.h("ez-rich-toolbar-item", { icon: RichToolbarHelper.RICH_TOOLBAR_COMMAND_ICON.LINK, command: RichToolbarHelper.RICH_TOOLBAR_COMMANDS.LINK, title: this.i18n("ez-rich-text.title.link") })));
  }
};
EzRichToolbarConfigs.style = ezRichToolbarConfigsCss;

const ezRichToolbarLettersCss = ".sc-ez-rich-toolbar-letters-h{display:flex;gap:5px}";

const EzRichToolbarLetters = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
  }
  async componentWillLoad() {
    this.i18n = await index$1.initI18n();
  }
  render() {
    return (index.h(index.Host, null, index.h("ez-rich-toolbar-item", { icon: RichToolbarHelper.RICH_TOOLBAR_COMMAND_ICON.BOLD, command: RichToolbarHelper.RICH_TOOLBAR_COMMANDS.FORMAT, value: RichToolbarHelper.RICH_TOOLBAR_FORMAT_TAGS.BOLD, title: this.i18n("ez-rich-text.title.bold") }), index.h("ez-rich-toolbar-item", { icon: RichToolbarHelper.RICH_TOOLBAR_COMMAND_ICON.ITALIC, command: RichToolbarHelper.RICH_TOOLBAR_COMMANDS.FORMAT, value: RichToolbarHelper.RICH_TOOLBAR_FORMAT_TAGS.ITALIC, title: this.i18n("ez-rich-text.title.italic") }), index.h("ez-rich-toolbar-item", { icon: RichToolbarHelper.RICH_TOOLBAR_COMMAND_ICON.UNDERLINE, command: RichToolbarHelper.RICH_TOOLBAR_COMMANDS.FORMAT, value: RichToolbarHelper.RICH_TOOLBAR_FORMAT_TAGS.UNDERLINE, title: this.i18n("ez-rich-text.title.underline") })));
  }
};
EzRichToolbarLetters.style = ezRichToolbarLettersCss;

function formatBytes(bytes, decimals = 1) {
  if (bytes === 0) {
    return "0 Bytes";
  }
  const k = 1024;
  const dm = decimals < 0 ? 0 : decimals;
  const sizes = ["Bytes", "Kilobytes", "Megabytes", "Gigabytes", "Terabytes", "Petabytes", "Exabytes", "Zettabytes", "Yottabytes"];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
}

function fileToBase64(file) {
  return new Promise((resolve, reject) => {
    try {
      const reader = new FileReader();
      reader.onload = () => {
        resolve(reader.result);
      };
      reader.readAsDataURL(file);
    }
    catch (error) {
      reject(error);
    }
  });
}

const ezSimpleImageUploaderCss = ".image-uploader-container.sc-ez-simple-image-uploader{font-family:\"Roboto\";color:#2b3a54;display:flex;flex-direction:column;gap:var(--space--10, 10px);justify-content:space-between;align-items:center;padding-bottom:var(--space--8, 8px)}.header.sc-ez-simple-image-uploader{display:flex;font-weight:500;font-size:var(--font-size--medium, 16px)}.actions.sc-ez-simple-image-uploader{display:flex;justify-content:flex-end;gap:var(--space--12, 12px);margin-top:var(--space--12, 12px);width:100%}.drop-zone-section.sc-ez-simple-image-uploader{width:100%}.drop-zone.sc-ez-simple-image-uploader{display:flex;flex-wrap:wrap;background-color:var(--color--petroleum-70, #F4F6F9);padding:var(--space--6, 6px);width:100%;border-radius:var(--space--12, 12px);box-sizing:border-box;min-height:100px}.clickable.sc-ez-simple-image-uploader{cursor:pointer}.dragging.sc-ez-simple-image-uploader{background-color:var(--color--petroleum-100, #C8CCD3)}.drop-zone-container.sc-ez-simple-image-uploader{width:100%;display:flex;flex-wrap:wrap;justify-content:center;align-items:center;border:2px dashed var(--color-strokes, #DCE0E8);border-radius:6px;box-sizing:border-box}.text-error.sc-ez-simple-image-uploader{font-family:var(--font--pattern, \"Roboto\");color:var(--color--red-600, #BD0025);font-size:var(--font-size--small, 12px)}.button-close.sc-ez-simple-image-uploader{border:none;background:none;cursor:pointer}";

const EzSimpleImageUploader = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.executeToolbarCommand = index.createEvent(this, "executeToolbarCommand", 7);
    this.link = undefined;
    this.base64 = undefined;
    this.messageError = undefined;
    this.selectedFile = undefined;
    this.maxSize = 500;
  }
  /**
  * Método responsável pela abertura do popup.
  */
  async show() {
    this._popupElement.opened = true;
  }
  /**
  * Método responsável pelo fechamento do popup.
  */
  async hide() {
    this._popupElement.opened = false;
  }
  async componentWillLoad() {
    this.i18n = await index$1.initI18n();
  }
  async handleCancel() {
    this.removeSelectedFile();
    this.executeToolbarCommand.emit({ command: RichToolbarHelper.RICH_TOOLBAR_COMMANDS.FOCUS });
    await this.hide();
  }
  removeSelectedFile(event) {
    event === null || event === void 0 ? void 0 : event.stopPropagation();
    this.link = undefined;
    this.base64 = undefined;
    this.selectedFile = undefined;
    this.messageError = undefined;
  }
  triggerFileSelect() {
    if (!this.selectedFile) {
      this._fileInput.click();
    }
  }
  async handleImage(files) {
    const file = files && files[0];
    if (!file) {
      return;
    }
    if (file.size < (this.maxSize * 1000)) {
      this.selectedFile = file;
      this.base64 = await fileToBase64(file);
    }
    else {
      this.messageError = this.i18n("ez-simple-image-uploader.imageTooLarge", { maxSize: formatBytes(this.maxSize) });
    }
  }
  getFileList(event) {
    if (event instanceof DragEvent) {
      return Array.from(event.dataTransfer.files);
    }
    else {
      const input = event.target;
      return input.files;
    }
  }
  handleFileChange(event) {
    event.preventDefault();
    this.messageError = undefined;
    this._dropZone.classList.remove("dragging");
    const files = this.getFileList(event);
    this.handleImage(files);
    const input = event.target;
    input.value = "";
  }
  handleDragOver(event) {
    event.stopPropagation();
    event.preventDefault();
  }
  handleDragEnter(event) {
    event.preventDefault();
    this._dropZone.classList.add("dragging");
  }
  handleDragLeave(event) {
    event.preventDefault();
    if (!this._dropZone.contains(event.relatedTarget)) {
      this._dropZone.classList.remove("dragging");
    }
  }
  async handleActionPopup(event) {
    if (event.detail === "OK") {
      await this.handleCreateImgTag();
    }
    else if (event.detail === "CANCEL" || event.detail === "CLOSE") {
      await this.handleCancel();
    }
  }
  async handleCreateImgTag() {
    var _a;
    const tag = `<img src="${(_a = this.base64) !== null && _a !== void 0 ? _a : this.link}" alt="${this.i18n("ez-simple-image-uploader.imageAlt")}" />`;
    this.executeToolbarCommand.emit({ command: RichToolbarHelper.RICH_TOOLBAR_COMMANDS.FORMAT, value: tag });
    this.removeSelectedFile();
    await this.hide();
  }
  getOkButtonStatus() {
    var _a;
    return (!this.base64 && !((_a = this.link) === null || _a === void 0 ? void 0 : _a.trim())) ? 'DISABLED' : 'ENABLED';
  }
  render() {
    return (index.h("ez-popup", { ref: ref => this._popupElement = ref, heightMode: "auto", size: "small", useHeader: false, onEzClosePopup: () => this.handleCancel() }, index.h("div", null, index.h("ez-modal-container", { modalTitle: this.i18n("ez-simple-image-uploader.uploadTitle"), modalSubTitle: this.i18n("ez-simple-image-uploader.uploadSubtitle"), okButtonLabel: this.i18n("ez-simple-image-uploader.submit"), cancelButtonLabel: this.i18n("app.cancel"), onEzModalAction: this.handleActionPopup.bind(this), okButtonStatus: this.getOkButtonStatus() }, index.h("div", { class: "image-uploader-container" }, index.h("ez-tooltip", { message: this.i18n("ez-simple-image-uploader.tooltipMessage"), active: !!this.selectedFile }, index.h("ez-text-input", { label: this.i18n("ez-simple-image-uploader.urlInputLabel"), value: this.link, onEzChange: (e) => this.link = e.detail, enabled: !this.selectedFile })), index.h("span", null, this.i18n("ez-simple-image-uploader.orText")), index.h("section", { class: "drop-zone-section" }, index.h("div", { ref: (ref) => this._dropZone = ref, class: "drop-zone", onDrop: (ev) => this.handleFileChange(ev), onDragOver: (ev) => this.handleDragOver(ev), onDragEnter: (ev) => this.handleDragEnter(ev), onDragLeave: (ev) => this.handleDragLeave(ev) }, index.h("div", { class: {
        "drop-zone-container": true,
        "clickable": !this.selectedFile,
      }, onClick: () => this.triggerFileSelect() }, !!this.selectedFile ?
      index.h(index.Fragment, null, index.h("span", { class: "ez-text ez-text--primary ez-text--medium" }, this.selectedFile.name, " (", formatBytes(this.selectedFile.size), ")"), index.h("button", { class: "button-close", onClick: (event) => this.removeSelectedFile(event), title: this.i18n("ez-simple-image-uploader.removeFile") }, index.h("ez-icon", { iconName: "close" })))
      :
        index.h(index.Fragment, null, index.h("ez-icon", { iconName: "anexo" }), index.h("span", { class: "ez-text ez-text--primary ez-text--medium" }, this.i18n("ez-simple-image-uploader.dragDropText")))), index.h("input", { ref: (el) => this._fileInput = el, type: "file", accept: "image/*", style: { display: "none" }, onChange: (ev) => this.handleFileChange(ev) })), !!this.messageError && index.h("span", { class: "text-error" }, this.messageError)))))));
  }
};
EzSimpleImageUploader.style = ezSimpleImageUploaderCss;

exports.ez_link_builder = EzLinkBuilder;
exports.ez_rich_toolbar = EzRichToolbar;
exports.ez_rich_toolbar_arrows = EzRichToolbarArrows;
exports.ez_rich_toolbar_configs = EzRichToolbarConfigs;
exports.ez_rich_toolbar_letters = EzRichToolbarLetters;
exports.ez_simple_image_uploader = EzSimpleImageUploader;
