import { h } from "@stencil/core";
import { _t } from "../i18n/i18n";
export class Share {
  constructor() {
    this.popupHeight = 480;
    this.popupWidth = 640;
    this.shareTitle = undefined;
    this.shareUser = undefined;
    this.shareHashtags = undefined;
    this.shareUrl = undefined;
  }
  constructShareUrl(base, params) {
    let url = base;
    let qs = Object.keys(params || {})
      .filter((key) => params[key] != undefined)
      .map((key) => `${key}=${encodeURIComponent(params[key])}`)
      .join("&");
    if (qs != null)
      url += `?${qs}`;
    return url;
  }
  share(platform) {
    let targetUrl = this.shareUrl || window.location.href;
    let title = this.shareTitle;
    if (title == undefined) {
      let app = this.el.closest("gl-app");
      if (app != undefined)
        title = app.label;
    }
    let url;
    if (platform === "twitter") {
      url = this.constructShareUrl("https://twitter.com/intent/tweet", {
        url: targetUrl,
        title: title,
        via: this.shareUser,
        hashtags: this.shareHashtags,
      });
    }
    else {
      url = this.constructShareUrl("https://www.facebook.com/sharer.php", {
        u: targetUrl,
      });
    }
    this.openUrl(url);
    this.closePopover();
  }
  openUrl(url) {
    window.open(url, "gl-share", `width=${this.popupWidth},height=${this.popupHeight},` +
      `resizable,scrollbars=yes`);
  }
  closePopover() {
    let popover = this.el.closest("ion-popover");
    if (popover != undefined)
      popover.dismiss();
  }
  clickShare(name) {
    return () => this.share(name.toLocaleLowerCase());
  }
  render() {
    let items = ["Facebook", "Twitter"].map((name) => (h("ion-item", { button: true, onClick: this.clickShare(name) }, h("ion-icon", { slot: "start", name: `logo-${name.toLowerCase()}` }), h("ion-label", null, name))));
    return (h("ion-list", null, h("ion-list-header", null, _t("webmapgl.share.title")), items));
  }
  static get is() { return "gl-share"; }
  static get properties() {
    return {
      "popupHeight": {
        "type": "number",
        "mutable": false,
        "complexType": {
          "original": "number",
          "resolved": "number",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Height of the popup window in pixels."
        },
        "attribute": "popup-height",
        "reflect": false,
        "defaultValue": "480"
      },
      "popupWidth": {
        "type": "number",
        "mutable": false,
        "complexType": {
          "original": "number",
          "resolved": "number",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Width of the popup window in pixels."
        },
        "attribute": "popup-width",
        "reflect": false,
        "defaultValue": "640"
      },
      "shareTitle": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Title of the item being shared."
        },
        "attribute": "share-title",
        "reflect": false
      },
      "shareUser": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "User attribution. It is used for sharing on Twitter."
        },
        "attribute": "share-user",
        "reflect": false
      },
      "shareHashtags": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Hashtags for the post. It is used for sharing on Twitter."
        },
        "attribute": "share-hashtags",
        "reflect": false
      },
      "shareUrl": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "URL to share. If a URL is not provided, the current page URL is used."
        },
        "attribute": "share-url",
        "reflect": false
      }
    };
  }
  static get elementRef() { return "el"; }
}
//# sourceMappingURL=share.js.map
