"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("../polyfills/tslib.js");
var webmapgl_core_js_1 = require("../webmapgl.core.js");
var LikeButton = function () { function t() { this.count = 0, this.liked = !1, this.attribute = "_likes", this.disabled = !1, this.iconNo = "star-outline", this.iconYes = "star", this.method = "POST"; } return t.prototype.componentWillLoad = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (t) { switch (t.label) {
    case 0: return [4, this.setState()];
    case 1: return t.sent(), [2];
} }); }); }, t.prototype.setState = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var t; return tslib_1.__generator(this, function (e) { switch (e.label) {
    case 0: return this.feature ? (this.count = this.feature.properties[this.attribute], t = this, [4, this.likeCtrl.create(this.feature)]) : [3, 2];
    case 1: t.proxy = e.sent(), this.liked = this.proxy.isLiked(), e.label = 2;
    case 2: return [2];
} }); }); }, t.prototype.toggle = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var t, e; return tslib_1.__generator(this, function (r) { switch (r.label) {
    case 0: this.liked = !this.liked, this.count += this.liked ? 1 : -1, this.feature && this.feature.properties && (this.feature.properties[this.attribute] = this.count), this.liked ? this.proxy.like() : this.proxy.unlike(), t = !1, e = this.proxy.getClientId(), r.label = 1;
    case 1: return r.trys.push([1, 3, , 4]), [4, this.restCtrl.create({ type: "Feature", properties: { action: this.liked ? "like" : "unlike", feature_id: this.feature.id, client_id: e } }, { url: this.url, token: this.token, method: this.method, mode: this.requestMode })];
    case 2: return r.sent(), t = !0, [3, 4];
    case 3: return r.sent(), [3, 4];
    case 4: return this.glLike.emit({ success: t, feature: this.feature, action: this.liked ? "like" : "unlike", client_id: e }), [2];
} }); }); }, t.prototype.render = function () { var t = this; return webmapgl_core_js_1.h("ion-button", { fill: "clear", onClick: function () { t.disabled || t.toggle(); }, disabled: this.disabled }, webmapgl_core_js_1.h("ion-icon", { slot: "start", name: this.liked ? this.iconYes : this.iconNo }), webmapgl_core_js_1.h("span", { class: "gl-like-button-count" }, this.count)); }, Object.defineProperty(t, "is", { get: function () { return "gl-like-button"; }, enumerable: !0, configurable: !0 }), Object.defineProperty(t, "properties", { get: function () { return { attribute: { type: String, attr: "attribute" }, count: { state: !0 }, disabled: { type: Boolean, attr: "disabled" }, el: { elementRef: !0 }, feature: { type: "Any", attr: "feature", mutable: !0, watchCallbacks: ["setState"] }, iconNo: { type: String, attr: "icon-no" }, iconYes: { type: String, attr: "icon-yes" }, likeCtrl: { connect: "gl-like-controller" }, liked: { state: !0 }, method: { type: String, attr: "method" }, proxy: { state: !0 }, requestMode: { type: String, attr: "request-mode" }, restCtrl: { connect: "gl-rest-controller" }, token: { type: String, attr: "token" }, url: { type: String, attr: "url" } }; }, enumerable: !0, configurable: !0 }), Object.defineProperty(t, "events", { get: function () { return [{ name: "glLike", method: "glLike", bubbles: !0, cancelable: !0, composed: !0 }]; }, enumerable: !0, configurable: !0 }), t; }(), LikeProxy = function () { function t(t, e) { void 0 === e && (e = {}), this.feature = t, e.keyPrefix || (e.keyPrefix = "webmapgl.like"), e.clientId || (e.clientId = localStorage.getItem(e.keyPrefix + ".clientId"), e.clientId || (e.clientId = this.makeId(64), localStorage.setItem(e.keyPrefix + ".clientId", e.clientId))), this.options = e; } return t.prototype.getClientId = function () { return this.options.clientId; }, t.prototype.isLiked = function () { return -1 !== this.getArray(this.feature.layer).indexOf(this.feature.id); }, t.prototype.like = function () { var t = this.getArray(this.feature.layer); -1 === t.indexOf(this.feature.id) && t.push(this.feature.id), this.setArray(this.feature.layer, t); }, t.prototype.unlike = function () { var t = this.getArray(this.feature.layer), e = t.indexOf(this.feature.id); -1 !== e && t.splice(e, 1), this.setArray(this.feature.layer, t); }, t.prototype.makeId = function (t) { for (var e = "", r = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", i = 0; i < t; i++)
    e += r.charAt(Math.floor(Math.random() * r.length)); return e; }, t.prototype.getLayerKey = function (t) { return this.options.keyPrefix + "|" + document.location.pathname + "|" + (t || "default"); }, t.prototype.getArray = function (t) { var e = localStorage.getItem(this.getLayerKey(t)); return e ? e.split(",").map(function (t) { return parseInt(t); }).filter(function (t) { return !isNaN(t); }) : []; }, t.prototype.setArray = function (t, e) { var r = e.filter(function (t) { return !isNaN(t); }).map(function (t) { return t.toString(); }).join(","); localStorage.setItem(this.getLayerKey(t), r); }, t; }(), LikeController = function () { function t() { } return t.prototype.create = function (t, e) { return void 0 === e && (e = {}), tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (r) { return [2, new LikeProxy(t, e)]; }); }); }, Object.defineProperty(t, "is", { get: function () { return "gl-like-controller"; }, enumerable: !0, configurable: !0 }), Object.defineProperty(t, "properties", { get: function () { return { create: { method: !0 } }; }, enumerable: !0, configurable: !0 }), t; }(), RestController = function () { function t() { } return t.prototype.create = function (t, e) { return tslib_1.__awaiter(this, void 0, void 0, function () { var r; return tslib_1.__generator(this, function (i) { switch (i.label) {
    case 0: return (r = {})["Content-Type"] = "application/json", e.token && (r.Authorization = "Bearer " + e.token), [4, fetch(e.url, { body: JSON.stringify(t), headers: r, method: e.method || "POST", mode: e.mode || "cors" })];
    case 1: return [2, i.sent()];
} }); }); }, Object.defineProperty(t, "is", { get: function () { return "gl-rest-controller"; }, enumerable: !0, configurable: !0 }), Object.defineProperty(t, "properties", { get: function () { return { create: { method: !0 } }; }, enumerable: !0, configurable: !0 }), t; }();
exports.GlLikeButton = LikeButton;
exports.GlLikeController = LikeController;
exports.GlRestController = RestController;
