exports.__esModule=!0,exports.assertIdValue=function(e){(0,a.invariant)((0,r.isIdValue)(e),11)},exports.defaultDataIdFromObject=R,exports.defaultNormalizedCacheFactory=function(e){return new m(e)},exports.enhanceErrorWithDocument=v,exports.WriteError=exports.StoreWriter=exports.StoreReader=exports.ObjectCache=exports.IntrospectionFragmentMatcher=exports.InMemoryCache=exports.HeuristicFragmentMatcher=void 0;var e=require("tslib"),t=require("apollo-cache"),r=require("apollo-utilities"),i=require("optimism"),a=require("ts-invariant"),n=!1;function o(){var e=!n;return(0,r.isTest)()||(n=!0),e}var s=function(){function e(){}return e.prototype.ensureReady=function(){return Promise.resolve()},e.prototype.canBypassInit=function(){return!0},e.prototype.match=function(e,t,r){var i=r.store.get(e.id),a="ROOT_QUERY"===e.id;if(!i)return a;var n=i.__typename,s=void 0===n?a&&"Query":n;return s&&s===t||(o(),"heuristic")},e}();exports.HeuristicFragmentMatcher=s;var c=function(){function e(e){e&&e.introspectionQueryResultData?(this.possibleTypesMap=this.parseIntrospectionResult(e.introspectionQueryResultData),this.isReady=!0):this.isReady=!1,this.match=this.match.bind(this)}return e.prototype.match=function(e,t,r){(0,a.invariant)(this.isReady,1);var i=r.store.get(e.id),n="ROOT_QUERY"===e.id;if(!i)return n;var o=i.__typename,s=void 0===o?n&&"Query":o;if((0,a.invariant)(s,2),s===t)return!0;var c=this.possibleTypesMap[t];return!!(s&&c&&c.indexOf(s)>-1)},e.prototype.parseIntrospectionResult=function(e){var t={};return e.__schema.types.forEach(function(e){"UNION"!==e.kind&&"INTERFACE"!==e.kind||(t[e.name]=e.possibleTypes.map(function(e){return e.name}))}),t},e}();exports.IntrospectionFragmentMatcher=c;var u=Object.prototype.hasOwnProperty,l=function(){function e(e){var t=this;void 0===e&&(e=Object.create(null)),this.data=e,this.depend=(0,i.wrap)(function(e){return t.data[e]},{disposable:!0,makeCacheKey:function(e){return e}})}return e.prototype.toObject=function(){return this.data},e.prototype.get=function(e){return this.depend(e),this.data[e]},e.prototype.set=function(e,t){t!==this.data[e]&&(this.data[e]=t,this.depend.dirty(e))},e.prototype.delete=function(e){u.call(this.data,e)&&(delete this.data[e],this.depend.dirty(e))},e.prototype.clear=function(){this.replace(null)},e.prototype.replace=function(e){var t=this;e?(Object.keys(e).forEach(function(r){t.set(r,e[r])}),Object.keys(this.data).forEach(function(r){u.call(e,r)||t.delete(r)})):Object.keys(this.data).forEach(function(e){t.delete(e)})},e}();function d(e){return new l(e)}var p=function(){function t(e){var t=this,a=void 0===e?{}:e,n=a.cacheKeyRoot,o=void 0===n?new i.KeyTrie(r.canUseWeakMap):n,s=a.freezeResults,c=void 0!==s&&s,u=this.executeStoreQuery,d=this.executeSelectionSet,p=this.executeSubSelectedArray;this.freezeResults=c,this.executeStoreQuery=(0,i.wrap)(function(e){return u.call(t,e)},{makeCacheKey:function(e){var t=e.query,r=e.rootValue,i=e.contextValue,a=e.variableValues,n=e.fragmentMatcher;if(i.store instanceof l)return o.lookup(i.store,t,n,JSON.stringify(a),r.id)}}),this.executeSelectionSet=(0,i.wrap)(function(e){return d.call(t,e)},{makeCacheKey:function(e){var t=e.selectionSet,r=e.rootValue,i=e.execContext;if(i.contextValue.store instanceof l)return o.lookup(i.contextValue.store,t,i.fragmentMatcher,JSON.stringify(i.variableValues),r.id)}}),this.executeSubSelectedArray=(0,i.wrap)(function(e){return p.call(t,e)},{makeCacheKey:function(e){var t=e.field,r=e.array,i=e.execContext;if(i.contextValue.store instanceof l)return o.lookup(i.contextValue.store,t,r,JSON.stringify(i.variableValues))}})}return t.prototype.readQueryFromStore=function(t){return this.diffQueryAgainstStore((0,e.__assign)((0,e.__assign)({},t),{returnPartialData:!1})).result},t.prototype.diffQueryAgainstStore=function(e){var t=e.store,i=e.query,n=e.variables,o=e.previousResult,s=e.returnPartialData,c=void 0===s||s,u=e.rootId,l=void 0===u?"ROOT_QUERY":u,d=e.fragmentMatcherFunction,p=e.config,f=(0,r.getQueryDefinition)(i);n=(0,r.assign)({},(0,r.getDefaultValues)(f),n);var h={store:t,dataIdFromObject:p&&p.dataIdFromObject,cacheRedirects:p&&p.cacheRedirects||{}},m=this.executeStoreQuery({query:i,rootValue:{type:"id",id:l,generated:!0,typename:"Query"},contextValue:h,variableValues:n,fragmentMatcher:d}),y=m.missing&&m.missing.length>0;return y&&!c&&m.missing.forEach(function(e){if(!e.tolerable)throw new a.InvariantError(8)}),o&&(0,r.isEqual)(o,m.result)&&(m.result=o),{result:m.result,complete:!y}},t.prototype.executeStoreQuery=function(e){var t=e.query,i=e.rootValue,a=e.contextValue,n=e.variableValues,o=e.fragmentMatcher,s=void 0===o?h:o,c=(0,r.getMainDefinition)(t),u=(0,r.getFragmentDefinitions)(t),l={query:t,fragmentMap:(0,r.createFragmentMap)(u),contextValue:a,variableValues:n,fragmentMatcher:s};return this.executeSelectionSet({selectionSet:c.selectionSet,rootValue:i,execContext:l})},t.prototype.executeSelectionSet=function(t){var i=this,n=t.selectionSet,o=t.rootValue,s=t.execContext,c=s.fragmentMap,u=s.contextValue,l=s.variableValues,d={result:null},p=[],f=u.store.get(o.id),h=f&&f.__typename||"ROOT_QUERY"===o.id&&"Query"||void 0;function m(e){var t;return e.missing&&(d.missing=d.missing||[],(t=d.missing).push.apply(t,e.missing)),e.result}return n.selections.forEach(function(t){var n;if((0,r.shouldInclude)(t,l))if((0,r.isField)(t)){var d=m(i.executeField(f,h,t,s));void 0!==d&&p.push(((n={})[(0,r.resultKeyNameFromField)(t)]=d,n))}else{var y=void 0;if((0,r.isInlineFragment)(t))y=t;else if(!(y=c[t.name.value]))throw new a.InvariantError(9);var v=y.typeCondition&&y.typeCondition.name.value,g=!v||s.fragmentMatcher(o,v,u);if(g){var S=i.executeSelectionSet({selectionSet:y.selectionSet,rootValue:o,execContext:s});"heuristic"===g&&S.missing&&(S=(0,e.__assign)((0,e.__assign)({},S),{missing:S.missing.map(function(t){return(0,e.__assign)((0,e.__assign)({},t),{tolerable:!0})})})),p.push(m(S))}}}),d.result=(0,r.mergeDeepArray)(p),this.freezeResults,d},t.prototype.executeField=function(e,t,i,a){var n=a.variableValues,o=a.contextValue,s=function(e,t,i,a,n,o){o.resultKey;var s=o.directives,c=i;(a||s)&&(c=(0,r.getStoreKeyName)(c,a,s));var u=void 0;if(e&&void 0===(u=e[c])&&n.cacheRedirects&&"string"==typeof t){var l=n.cacheRedirects[t];if(l){var d=l[i];d&&(u=d(e,a,{getCacheKey:function(e){var t=n.dataIdFromObject(e);return t&&(0,r.toIdValue)({id:t,typename:e.__typename})}}))}}if(void 0===u)return{result:u,missing:[{object:e,fieldName:c,tolerable:!1}]};(0,r.isJsonValue)(u)&&(u=u.json);return{result:u}}(e,t,i.name.value,(0,r.argumentsObjectFromField)(i,n),o,{resultKey:(0,r.resultKeyNameFromField)(i),directives:(0,r.getDirectiveInfoFromField)(i,n)});return Array.isArray(s.result)?this.combineExecResults(s,this.executeSubSelectedArray({field:i,array:s.result,execContext:a})):i.selectionSet?null==s.result?s:this.combineExecResults(s,this.executeSelectionSet({selectionSet:i.selectionSet,rootValue:s.result,execContext:a})):(f(i,s.result),this.freezeResults,s)},t.prototype.combineExecResults=function(){for(var e,t=[],r=0;r=0)return!0;r[e].push(t)}else r[e]=[t];return!1}exports.StoreWriter=g;var x={fragmentMatcher:new s,dataIdFromObject:R,addTypename:!0,resultCaching:!0,freezeResults:!1};function R(e){if(e.__typename){if(void 0!==e.id)return e.__typename+":"+e.id;if(void 0!==e._id)return e.__typename+":"+e._id}return null}var _=Object.prototype.hasOwnProperty,I=function(t){function r(e,r,i){var a=t.call(this,Object.create(null))||this;return a.optimisticId=e,a.parent=r,a.transaction=i,a}return(0,e.__extends)(r,t),r.prototype.toObject=function(){return(0,e.__assign)((0,e.__assign)({},this.parent.toObject()),this.data)},r.prototype.get=function(e){return _.call(this.data,e)?this.data[e]:this.parent.get(e)},r}(m),F=function(t){function n(a){void 0===a&&(a={});var n=t.call(this)||this;n.watches=new Set,n.typenameDocumentCache=new Map,n.cacheKeyRoot=new i.KeyTrie(r.canUseWeakMap),n.silenceBroadcast=!1,n.config=(0,e.__assign)((0,e.__assign)({},x),a),n.config.customResolvers&&(n.config.cacheRedirects=n.config.customResolvers),n.config.cacheResolvers&&(n.config.cacheRedirects=n.config.cacheResolvers),n.addTypename=!!n.config.addTypename,n.data=n.config.resultCaching?new l:new m,n.optimisticData=n.data,n.storeWriter=new g,n.storeReader=new p({cacheKeyRoot:n.cacheKeyRoot,freezeResults:a.freezeResults});var o=n,s=o.maybeBroadcastWatch;return n.maybeBroadcastWatch=(0,i.wrap)(function(e){return s.call(n,e)},{makeCacheKey:function(e){if(!e.optimistic&&!e.previousResult)return o.data instanceof l?o.cacheKeyRoot.lookup(e.query,JSON.stringify(e.variables)):void 0}}),n}return(0,e.__extends)(n,t),n.prototype.restore=function(e){return e&&this.data.replace(e),this},n.prototype.extract=function(e){return void 0===e&&(e=!1),(e?this.optimisticData:this.data).toObject()},n.prototype.read=function(e){if("string"==typeof e.rootId&&void 0===this.data.get(e.rootId))return null;var t=this.config.fragmentMatcher,r=t&&t.match;return this.storeReader.readQueryFromStore({store:e.optimistic?this.optimisticData:this.data,query:this.transformDocument(e.query),variables:e.variables,rootId:e.rootId,fragmentMatcherFunction:r,previousResult:e.previousResult,config:this.config})||null},n.prototype.write=function(e){var t=this.config.fragmentMatcher,r=t&&t.match;this.storeWriter.writeResultToStore({dataId:e.dataId,result:e.result,variables:e.variables,document:this.transformDocument(e.query),store:this.data,dataIdFromObject:this.config.dataIdFromObject,fragmentMatcherFunction:r}),this.broadcastWatches()},n.prototype.diff=function(e){var t=this.config.fragmentMatcher,r=t&&t.match;return this.storeReader.diffQueryAgainstStore({store:e.optimistic?this.optimisticData:this.data,query:this.transformDocument(e.query),variables:e.variables,returnPartialData:e.returnPartialData,previousResult:e.previousResult,fragmentMatcherFunction:r,config:this.config})},n.prototype.watch=function(e){var t=this;return this.watches.add(e),function(){t.watches.delete(e)}},n.prototype.evict=function(e){throw new a.InvariantError(7)},n.prototype.reset=function(){return this.data.clear(),this.broadcastWatches(),Promise.resolve()},n.prototype.removeOptimistic=function(e){for(var t=[],r=0,i=this.optimisticData;i instanceof I;)i.optimisticId===e?++r:t.push(i),i=i.parent;if(r>0){for(this.optimisticData=i;t.length>0;){var a=t.pop();this.performTransaction(a.transaction,a.optimisticId)}this.broadcastWatches()}},n.prototype.performTransaction=function(e,t){var r=this.data,i=this.silenceBroadcast;this.silenceBroadcast=!0,"string"==typeof t&&(this.data=this.optimisticData=new I(t,this.optimisticData,e));try{e(this)}finally{this.silenceBroadcast=i,this.data=r}this.broadcastWatches()},n.prototype.recordOptimisticTransaction=function(e,t){return this.performTransaction(e,t)},n.prototype.transformDocument=function(e){if(this.addTypename){var t=this.typenameDocumentCache.get(e);return t||(t=(0,r.addTypenameToDocument)(e),this.typenameDocumentCache.set(e,t),this.typenameDocumentCache.set(t,t)),t}return e},n.prototype.broadcastWatches=function(){var e=this;this.silenceBroadcast||this.watches.forEach(function(t){return e.maybeBroadcastWatch(t)})},n.prototype.maybeBroadcastWatch=function(e){e.callback(this.diff({query:e.query,variables:e.variables,previousResult:e.previousResult&&e.previousResult(),optimistic:e.optimistic}))},n}(t.ApolloCache);exports.InMemoryCache=F;