!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("mobx")):"function"==typeof define&&define.amd?define(["mobx"],t):(e=e||self).gpui=t(e.mobx)}(this,(function(e){"use strict";
/*! *****************************************************************************
    Copyright (c) Microsoft Corporation.

    Permission to use, copy, modify, and/or distribute this software for any
    purpose with or without fee is hereby granted.

    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
    REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
    INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
    LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
    OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
    PERFORMANCE OF THIS SOFTWARE.
    ***************************************************************************** */var t=function(e,r){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,r)};var r=function(){return(r=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};function n(e,t,r,n){var o,s=arguments.length,i=s<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(e,t,r,n);else for(var a=e.length-1;a>=0;a--)(o=e[a])&&(i=(s<3?o(i):s>3?o(t,r,i):o(t,r))||i);return s>3&&i&&Object.defineProperty(t,r,i),i}function o(e,t,r,n){return new(r||(r=Promise))((function(o,s){function i(e){try{c(n.next(e))}catch(e){s(e)}}function a(e){try{c(n.throw(e))}catch(e){s(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(i,a)}c((n=n.apply(e,t||[])).next())}))}var s=Object.create?function(e,t,r,n){void 0===n&&(n=r),Object.defineProperty(e,n,{enumerable:!0,get:function(){return t[r]}})}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]};function i(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function a(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,o,s=r.call(e),i=[];try{for(;(void 0===t||t-- >0)&&!(n=s.next()).done;)i.push(n.value)}catch(e){o={error:e}}finally{try{n&&!n.done&&(r=s.return)&&r.call(s)}finally{if(o)throw o.error}}return i}function c(e){return this instanceof c?(this.v=e,this):new c(e)}var l=Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t};var u=Object.freeze({__proto__:null,__extends:function(e,r){function n(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)},get __assign(){return r},__rest:function(e,t){var r={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(r[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(e);o<n.length;o++)t.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(e,n[o])&&(r[n[o]]=e[n[o]])}return r},__decorate:n,__param:function(e,t){return function(r,n){t(r,n,e)}},__metadata:function(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)},__awaiter:o,__generator:function(e,t){var r,n,o,s,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return s={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function a(s){return function(a){return function(s){if(r)throw new TypeError("Generator is already executing.");for(;i;)try{if(r=1,n&&(o=2&s[0]?n.return:s[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,s[1])).done)return o;switch(n=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return i.label++,{value:s[1],done:!1};case 5:i.label++,n=s[1],s=[0];continue;case 7:s=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){i=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){i.label=s[1];break}if(6===s[0]&&i.label<o[1]){i.label=o[1],o=s;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(s);break}o[2]&&i.ops.pop(),i.trys.pop();continue}s=t.call(e,i)}catch(e){s=[6,e],n=0}finally{r=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,a])}}},__createBinding:s,__exportStar:function(e,t){for(var r in e)"default"===r||t.hasOwnProperty(r)||s(t,e,r)},__values:i,__read:a,__spread:function(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(a(arguments[t]));return e},__spreadArrays:function(){for(var e=0,t=0,r=arguments.length;t<r;t++)e+=arguments[t].length;var n=Array(e),o=0;for(t=0;t<r;t++)for(var s=arguments[t],i=0,a=s.length;i<a;i++,o++)n[o]=s[i];return n},__await:c,__asyncGenerator:function(e,t,r){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var n,o=r.apply(e,t||[]),s=[];return n={},i("next"),i("throw"),i("return"),n[Symbol.asyncIterator]=function(){return this},n;function i(e){o[e]&&(n[e]=function(t){return new Promise((function(r,n){s.push([e,t,r,n])>1||a(e,t)}))})}function a(e,t){try{(r=o[e](t)).value instanceof c?Promise.resolve(r.value.v).then(l,u):f(s[0][2],r)}catch(e){f(s[0][3],e)}var r}function l(e){a("next",e)}function u(e){a("throw",e)}function f(e,t){e(t),s.shift(),s.length&&a(s[0][0],s[0][1])}},__asyncDelegator:function(e){var t,r;return t={},n("next"),n("throw",(function(e){throw e})),n("return"),t[Symbol.iterator]=function(){return this},t;function n(n,o){t[n]=e[n]?function(t){return(r=!r)?{value:c(e[n](t)),done:"return"===n}:o?o(t):t}:o}},__asyncValues:function(e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t,r=e[Symbol.asyncIterator];return r?r.call(e):(e=i(e),t={},n("next"),n("throw"),n("return"),t[Symbol.asyncIterator]=function(){return this},t);function n(r){t[r]=e[r]&&function(t){return new Promise((function(n,o){(function(e,t,r,n){Promise.resolve(n).then((function(t){e({value:t,done:r})}),t)})(n,o,(t=e[r](t)).done,t.value)}))}}},__makeTemplateObject:function(e,t){return Object.defineProperty?Object.defineProperty(e,"raw",{value:t}):e.raw=t,e},__importStar:function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)Object.hasOwnProperty.call(e,r)&&s(t,e,r);return l(t,e),t},__importDefault:function(e){return e&&e.__esModule?e:{default:e}},__classPrivateFieldGet:function(e,t){if(!t.has(e))throw new TypeError("attempted to get private field on non-instance");return t.get(e)},__classPrivateFieldSet:function(e,t,r){if(!t.has(e))throw new TypeError("attempted to set private field on non-instance");return t.set(e,r),r}});function f(e,t){var r,n;if(0===t.length)return e;for(r=0,n=t.length;r<n;r++)e=(e<<5)-e+t.charCodeAt(r),e|=0;return e<0?-2*e:e}function h(e,t,r,n){var o,s=f(f(f(e,r),(o=t,Object.prototype.toString.call(o))),typeof t);if(null===t)return f(s,"null");if(void 0===t)return f(s,"undefined");if("object"==typeof t||"function"==typeof t){if(-1!==n.indexOf(t))return f(s,"[Circular]"+r);n.push(t);var i=function(e,t,r){return Object.keys(t).sort().reduce((function(e,n){return h(e,t[n],n,r)}),e)}(s,t,n);if(!("valueOf"in t)||"function"!=typeof t.valueOf)return i;try{return f(i,String(t.valueOf()))}catch(e){return f(i,"[valueOf exception]"+(e.stack||e.message))}}return f(s,t.toString())}var p=function(e){return function(e,t){for(;e.length<t;)e="0"+e;return e}(h(0,e,"",[]).toString(16),8)};var d,m=Object.freeze({__proto__:null,nanoid:(e=21)=>{let t="",r=e;for(;r--;)t+="ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW"[64*Math.random()|0];return t},customAlphabet:(e,t)=>()=>{let r="",n=t;for(;n--;)r+=e[Math.random()*e.length|0];return r}}),b=function(e,t){return e(t={exports:{}},t.exports),t.exports}((function(e,t){Object.defineProperty(t,"__esModule",{value:!0});const r=u,n=p,o=m;class s extends Error{constructor(e,t){super(e),this.code=t||0,this.message=e}toString(){return this.message}}class i{constructor(e,t=5e3){this._timeout=5e3,this._promises=new Map,this._callings=new Map,this._patterns=[],this._hashedFunctions=new Map,this._remoteObjects=new Map,this._remoteObjectClusters=new Map,this.extendedRPCs=[],this.send=e,this._timeout=t||5e3,this.on("_.Function.call",(e,t)=>r.__awaiter(this,void 0,void 0,(function*(){if(this._hashedFunctions.has(e)){let r=this._hashedFunctions.get(e);return yield Promise.resolve(r(...t))}}))),this.on("_.Function.release",e=>{this._hashedFunctions.has(e)&&this._hashedFunctions.delete(e)}),this.on("_.RemoteObject.set",(e,t,r)=>{try{const n=this._remoteObjects.get(e),o=t.split("."),s=o.pop();let i=n;for(let e of o)i=Reflect.get(i,e);Reflect.set(i,s,r)}catch(e){console.debug(e)}}),this.on("_.RemoteObject.get",e=>r.__awaiter(this,void 0,void 0,(function*(){try{const t=this._remoteObjects.get(e);return JSON.parse(JSON.stringify(yield Promise.resolve(t)))}catch(e){console.debug(e)}}))),this.on("_.RemoteObject.apply",(e,t,n)=>r.__awaiter(this,void 0,void 0,(function*(){try{const r=this._remoteObjects.get(e);let o,s;"$"===t.slice(-1)?(s=t.slice(0,-1).split("."),o=!0):(s=t.split("."),o=!1);const i=s.pop();let a=r;for(let e of s)a=Reflect.get(a,e);let c=yield Promise.resolve(Reflect.apply(a[i],a,n));return o?JSON.parse(JSON.stringify(c)):this.makeRemoteObject(c,e)}catch(e){}}))),this.on("_.RemoteObject.release",e=>((this._remoteObjectClusters.get(e)||[]).map(e=>this._remoteObjects.delete(e)),this._remoteObjectClusters.delete(e),!0))}decodeFunctions(e){const t=this,r=e=>{if(e===Object(e)){if("1.0"===e["@func"]&&Reflect.has(e,"hash"))return new Proxy(()=>{},{get(r,n){const o=String(n);return Reflect.has(r,n)?r[n]:"release"===o?()=>{t.notify("_.Function.release",[e.hash])}:void 0},apply(r,n,o){t.notify("_.Function.call",[e.hash,o])}});for(let t in e)e.hasOwnProperty(t)&&(e[t]=r(e[t]))}else if(Array.isArray(e))return e.map(e=>r(e));return e};return r(e)}encodeFunctions(e){const t=e=>{if("function"==typeof e){let t=n(e);return this._hashedFunctions.set(t,e),{"@func":"1.0",hash:t}}if(e===Object(e))for(let r in e)e.hasOwnProperty(r)&&(e[r]=t(e[r]));else if(Array.isArray(e))return e.map(e=>t(e));return e};return t(e)}makeRemoteObject(e,t){const r=o.nanoid();this._remoteObjects.set(r,e);const n=t||r,s=this._remoteObjectClusters.get(n)||[];return s.push(r),this._remoteObjectClusters.set(n,s),{"@remote":r}}extends(e){this.extendedRPCs.push(e)}getHandler(e){let t,r;if(this._callings.has(e))t=this._callings.get(e);else for(let{pattern:n,callback:o}of this._patterns)if(r=e.match(n),r){t=o;break}return[t,r]}receive(e){return r.__awaiter(this,void 0,void 0,(function*(){if("string"==typeof e)try{e=JSON.parse(e)}catch(e){return void this.sendError(new s("Parse error",-32700))}if("2.0"===e.jsonrpc){if(Reflect.has(e,"method")){let t,{method:r,params:n}=e;"$"===r.slice(-1)?(r=r.slice(0,-1),t=!0):t=!1;let[o,i]=this.getHandler(r);if(void 0===o&&this.extendedRPCs.length>0)for(let e of this.extendedRPCs)if([o,i]=e.getHandler(r),o)break;if(void 0===o)return void(e.id&&this.sendError(new s(`Method "${r}" not found`,-32601),e.id));n=this.decodeFunctions(n),Array.isArray(n)||(n=[n]);try{let r;i&&(n=[n,i]),r=yield Promise.resolve(o(...n)),t&&(r=this.makeRemoteObject(r)),e.id&&this.sendResult(r,e.id)}catch(t){if(!(t instanceof s))throw t;this.sendError(t,e.id)}}else if(Reflect.has(e,"error")){if(e.id&&this._promises.has(e.id)){let t=this._promises.get(e.id);t.reject(e.error),clearTimeout(t.timeout),this._promises.delete(e.id)}}else if(Reflect.has(e,"result")&&e.id&&this._promises.has(e.id)){let t=this._promises.get(e.id),r=e.result;if(r&&"object"==typeof r&&Reflect.has(r,"@remote")){const e=this,t=r["@remote"],n={set(r,n,o){const s=String(n),i=r.$$baseName?`${r.$$baseName}.${s}`:s;return e.call("_.RemoteObject.set",[t,i,o]),!0},get(e,t){const r=String(t);if("then"!==r&&"$$"!==r.slice(0,1))return Reflect.has(e,t)?e[t]:(Reflect.has(e.$$cache,t)||(e.$$cache[t]=new Proxy(Object.assign(()=>{},{$$baseName:e.$$baseName?`${e.$$baseName}.${r}`:r,$$cache:{}}),n)),e.$$cache[t])},apply:(r,n,o)=>e.call("_.RemoteObject.apply",[t,r.$$baseName,o])},o={};r=new Proxy(Object.assign(()=>{},{$$cache:o,release$:()=>(Object.keys(o).forEach(e=>delete o[e]),e.call("_.RemoteObject.release",[t])),get$:()=>e.call("_.RemoteObject.get",[t])}),n)}t.resolve(r),clearTimeout(t.timeout),this._promises.delete(e.id)}}else this.sendError(new s("Parse error",-32700))}))}sendResult(e,t){const r={jsonrpc:"2.0",result:e||null};return t&&(r.id=t),this.send(r),this}sendError(e,t){const r={jsonrpc:"2.0",error:{code:e.code,message:e.message}};return t&&(r.id=t),this.send(r),this}notify(e,t={}){t=this.encodeFunctions(t),this.send({jsonrpc:"2.0",method:e,params:t})}call(e,t={}){t=this.encodeFunctions(t);let r=this;return new Promise((n,i)=>{let a=o.nanoid(),c={id:a,jsonrpc:"2.0",method:e,params:t};r.send(c),r._promises.set(a,{resolve:n,reject:i,timeout:setTimeout(()=>{r._promises.delete(a),i(new s(`Call ${e} Timeout`,-32603))},r._timeout)})})}on(e,t){return"string"!=typeof e?this._patterns.push({pattern:e,callback:t}):this._callings.set(e,t),this}off(e){if("string"!=typeof e){for(let t in this._callings.keys())null!=t.match(e)&&this._callings.delete(t);let t=e.toString();this._patterns=this._patterns.filter(({pattern:e})=>e.toString()!==t)}else this._callings.has(e)&&this._callings.delete(e);return this}}i.Error=s,t.default=i}));const y=new((d=b)&&d.__esModule&&Object.prototype.hasOwnProperty.call(d,"default")?d.default:d)(e=>{const t={scope:"remote-gpui",context:e};window.parent.postMessage(t,"*")});class _{constructor(e="remote"){this.instanceId="",this.params={},this.globals={},this.globalsHash={},this.rpc=y,this.$scope=e}subscribeGlobal(t){return o(this,void 0,void 0,(function*(){const r={};this.globals[t]=yield y.call("remote.subscribeGlobal",t),e.reaction(()=>e.toJS(this.globals[t]),e=>o(this,void 0,void 0,(function*(){if(!r[t]){r[t]=!0;try{yield y.call("remote.updateGlobal",[t,e])}catch(e){}r[t]=!1}})))}))}}n([e.observable],_.prototype,"instanceId",void 0),n([e.observable],_.prototype,"params",void 0),n([e.observable],_.prototype,"globals",void 0);const g={set:(e,t,r)=>"$scope"!==String(t)&&(e[t]=r,!0),get:(e,t)=>(e[t]=e[t]||new Proxy(Object.assign(()=>{},{$scope:`${e.$scope}.${String(t)}`}),g),e[t]),apply:(e,t,r)=>y.call(e.$scope,r)};var v,O=(()=>{const e=new _,t=new Proxy(e,g);return y.on("remote.updateGlobal",(t,r)=>{if(!Reflect.has(e.globals,t))return;const n=p(r);e.globalsHash[t]!==n&&(e.globalsHash[t]=n,e.globals[t]=r)}),y.on("remote.updateParams",t=>{e.params=t}),y.call("remote.getParams").then(t=>{e.params=t}),window.addEventListener("message",e=>{let t=e.data;if("string"==typeof t)try{t=JSON.parse(e.data)}catch(e){}e.source===window.parent&&t&&"remote-gpui"===t.scope&&y.receive(t.context)}),t})();!function(e){e.ALERT="alert",e.DEBUG="debug",e.WARN="warn",e.ERROR="error",e.INFO="info",e.FATAL="fatal"}(v||(v={}));class w{constructor(e){this.name=e}log(e,t,r){return O.log(this.name,e,t,r)}alert(e,t){return this.log(v.ALERT,e,t)}debug(e,t){return this.log(v.DEBUG,e,t)}warn(e,t){return this.log(v.WARN,e,t)}error(e,t){return this.log(v.ERROR,e,t)}info(e,t){return this.log(v.INFO,e,t)}fatal(e,t){return this.log(v.FATAL,e,t)}}var j=new class{constructor(){this.logs={}}of(e="default"){return this.logs[e]=this.logs[e]||new w(e),this.logs[e]}};class R{constructor(e){this.name=e}on(e,t){return O.messageBus.on(this.name,e,t),this}off(e,t){return O.messageBus.off(this.name,e,t),this}emit(e,...t){return O.messageBus.emit(this.name,e,t),this}}var P=new class{constructor(){this.buses={}}of(e="default"){return this.buses[e]=this.buses[e]||new R(e),this.buses[e]}};return new class{get root(){return O.root}get params(){return O.params}get globals(){return O.globals}observe(e){return o(this,void 0,void 0,(function*(){return yield O.subscribeGlobal(e),O.globals[e]}))}react(e){return this.observe(e)}logger(e="default"){return j.of(e)}bus(e="default"){return P.of(e)}on(e,t){return O.rpc.on("remote."+e,t),this}off(e){return O.rpc.off("remote."+e),this}}}));
//# sourceMappingURL=index.js.map
