'use strict';

var stringify = require('./stringify-c9ac1214.js');
var keys = require('./keys-7f2c2108.js');
require('./_commonjsHelpers-06173234.js');
require('./_to-object-6de10e57.js');
require('./_object-sap-a175b4a9.js');

var UstreamEmbed = function () {
  function a(a) {
    return b(a);
  }function b(a) {
    var b = c(a),
        m = function (a) {
      function b(b) {
        if ("socialstream" !== b) {
          if (!w) return y || (y = []), y.push(arguments), void 0;var d = i(arguments).slice(1);d[0] && "function" == typeof d[0] && (z[b] || (z[b] = []), z[b].push(d[0])), g(a, t, { cmd: b, args: d });
        } else if (j(window, "message", n), v = c(arguments[1]), u = h(v.getAttribute("src")), x = !0, B.length) for (var e = 0, k = B.length; k > e; e++) {
          f(B[e]);
        }
      }function m() {
        if (y) {
          for (; y.length;) {
            b.apply(this, y.shift());
          }y = null;
        }
      }function n(a) {
        var b = v;b && b.contentWindow && b.contentWindow === a.source ? s.onmessage(a) : a.source === v.id && s.onmessage(a);
      }function o(a) {
        var c,
            d = JSON.parse(a.data);return d.cmd && "ready" == d.cmd ? (g(v, u, { cmd: "ready" }), void 0) : (c = [d.cmd], c = c.concat(d.args), b.apply(this, c), void 0);
      }function p() {
        g(a, t, { cmd: "ready" });
      }function q() {
        w = !0, g(a, t, { cmd: "apihandshake", args: [] }), m(), v && g(v, u, { cmd: "viewer_ready" });
      }function r() {
        b.apply(this, arguments);
      }var s,
          t,
          u,
          v,
          w = !1,
          x = !1,
          y = [],
          z = {},
          A = {},
          B = [];return t = h(a.getAttribute("src")).toLowerCase(), a.onload = p, s = { host: t, callMethod: r, getProperty: function getProperty() {
          r.apply(this, arguments);
        }, addListener: function addListener(a, b) {
          A[a] || (A[a] = []), A[a].push(b);
        }, removeListener: function removeListener(a, b) {
          if (b) for (var c = 0, d = A[a].length; d > c; c++) {
            A[a][c] === b && A[a].splice(c, 1);
          } else A[a] = null;
        }, onmessage: function onmessage(a) {
          var b;if (t || u || B.push({ origin: a.origin, data: a.data }), a.origin.toLowerCase() == t) {
            try {
              b = JSON.parse(a.data);
            } catch (c) {
              return;
            }if (b.sstream) return o(a), void 0;if (b.event && b.event.ready && (q(), d(A, "ready")), b.event && b.event.live === !0) return d(A, "live"), void 0;if (b.event && b.event.live === !1) return d(A, "offline"), void 0;if (b.event && !b.event.ready) if (k) keys._Object$keys(b.event).forEach(function (a) {
              d(A, a, b.event[a]);
            });else for (var f in b.event) {
              b.event.hasOwnProperty(f) && d(A, f, b.event[f]);
            }if (b.property) if (k) keys._Object$keys(b.property).forEach(function (a) {
              e(z, a, b.property[a]);
            });else for (var f in b.property) {
              b.property.hasOwnProperty(f) && e(z, f, b.property[f]);
            }
          } else if (x && a.origin == u) return o(a), void 0;
        }, destroy: function destroy() {
          w = !1, t = "", x = !1, u = "", v = null, y = [], z = {}, A = {}, B = [], l[a.id] && (l[a.id] = null), a = null;
        } };
    }(b);return b.id || (b.id = "UstreamEmbed" + Math.ceil(1e5 * Math.random())), m.id = b.id, l[b.id] = m, m;
  }function c(a) {
    return "string" == typeof a && (a = document.getElementById(a)), a;
  }function d(a, b, c) {
    for (var d in a[b]) {
      a[b].hasOwnProperty(d) && a[b][d].call(window, b, c);
    }
  }function e(a, b, c) {
    if (a[b]) {
      var d = a[b];a[b] = null, delete a[b], d.forEach(function (a) {
        a.call(window, c);
      });
    }
  }function f(a) {
    var b, c;for (b in l) {
      l.hasOwnProperty(b) && l[b] && (c = document.getElementById(b), c && c.contentWindow ? c.contentWindow === a.source && l[b].onmessage(a) : "string" == typeof a.source && a.source == b && l[b].onmessage(a));
    }
  }function g(a, b, c) {
    a.contentWindow.postMessage(stringify._JSON$stringify(c), b);
  }function h(a) {
    return a.indexOf("http") < 0 && (a = location.protocol + a), a.match(m)[1].toString();
  }function i(a) {
    return Array.prototype.slice.call(a, 0);
  }function j(a, b, c) {
    a.addEventListener ? a.addEventListener(b, c, !1) : a.attachEvent("on" + b, c);
  }var k = "undefined" != typeof keys._Object$keys,
      l = {},
      m = new RegExp("^(http(?:s)?://[^/]+)", "im");return j(window, "message", f), "function" == typeof define && define.amd && define([], function () {
    return a;
  }), window.UstreamEmbed = a;
}();

exports.default = UstreamEmbed;
