/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/hyperbone-model@0.5.10/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
var Command,_=require("underscore"),BackboneModel=require("backbone-model").Model,Collection=require("backbone-collection").Collection.extend({isHyperbone:!0,toJSON:function(){var t=[];return _.each(this.models,(function(e,i){e.isHyperbone?t.push(e.toJSON()):t.push(e)})),t}}),makeTemplate=require("uritemplate").parse,HyperboneModel=function(t,e){t||(t={}),this._links={},this.attributes={},this.cid=_.uniqueId("c"),this.isHyperbone=!0,this._prototypes||(this._prototypes={}),this.syncCommands||(this.syncCommands=!1),e||(e={}),t._prototypes&&(_.extend(this._prototypes,t._prototypes),delete t._prototypes),t.syncCommands&&(this.syncCommands=!0,this.syncEvents=[],delete t.syncCommands),e&&e.collection&&(this.collection=e.collection),this.parser&&(t=this.parser(t)),t=_.defaults({},t,_.result(this,"defaults")),this.set(t,{silent:!0}),this.syncCommands&&this.reinitCommandSync(),this.changed={},this.initialize.apply(this,arguments)};_.extend(HyperboneModel.prototype,BackboneModel.prototype,{reinit:function(t,e){t=_.defaults({},t,_.result(this,"defaults")),this.parser&&(t=this.parser(t)),this.set(t),this.syncCommands&&this.reinitCommandSync()},reinitCommandSync:function(){var t=this;_.each(t.syncEvents,(function(e){t.off(e.event,e.handler)})),t.syncEvents=[],_.each(t.attributes,(function(e,i){t._commands&&!_.isObject(e)&&_.each(t._commands.attributes,(function(n){var s=n.properties();if(s.get(i)===e){var r={event:"change:"+i,handler:function(t,e){s.get(i)!==e&&s.set(i,e)}};s.on(r.event,(function(e,n){t.get(i)!==n&&t.set(i,n)})),t.on(r.event,r.handler),t.syncEvents.push(r)}}))}))},parseHypermedia:function(t){var e=this,i=[];t._links&&this._links?(_.each(t._links,(function(t,n){this._links[n]?t.href!==this._links[n].href&&i.push((function(){e.trigger("change-rel:"+n)})):i.push((function(){e.trigger("add-rel:"+n)})),this._links[n]=t}),this),_.each(this._links,(function(n,s){t._links[s]||i.push((function(){delete e._links[s],e.trigger("remove-rel:"+s)}))}),this)):this._links=t._links||{},delete t._links,this._curies={};var n=this._links.curie?[this._links.curie]:this._links.curies?this._links.curies:null;return n&&_.each(n,(function(t){if(!t.templated)throw new Error("A curie without a template? What are you thinking?");this._curies[t.name]=makeTemplate(t.href)}),this),_.each(this._links,(function(t,e){_.isArray(t)&&1===t.length?this._links[e]=t[0]:_.isArray(t)?_.each(t,(function(t,e){t.templated&&(t.template=makeTemplate(t.href))})):t.templated&&(t.template=makeTemplate(t.href))}),this),_.each(this._links,(function(t,e){t.templated&&(t.template=makeTemplate(t.href))}),this),t._embedded&&(_.each(t._embedded,(function(e,i){t[i]=e})),delete t._embedded),t._commands&&(this._commands?_.each(this._commands.attributes,(function(n,s){t._commands[s]||i.push((function(){e.command(s).reset(),delete e._commands.attributes[s],e.trigger("remove-command:"+s)}))})):this._commands=new HyperboneModel,_.each(t._commands,(function(t,n){var s;if(s=this.command(n))_.each(t,(function(t,r){"properties"!==r?s.set(r,t):(_.each(s.properties().toJSON(),(function(e,i){t[i]||s.properties().unset(i,null)})),_.each(t,(function(t,e){s.properties().attributes[e]||0,s.properties().set(e,t)})),i.push((function(){e.trigger("change-command-structure:"+n,e,s)})))})),t.href||s.set("href",e.url(),{silent:!0}),s._isClean=!0,s._clean=s.properties().toJSON(),i.push((function(){e.trigger("clean:"+n)}));else{this._commands.set(n,new Command(t));var r=this.command(n);r._parentModel=e,r._hbId=n,_.each(r.properties().attributes,(function(t,i){r.properties().on("change:"+i,(function(t,s){e.trigger("change:"+i+":"+n,r,s),r._isClean=!1,e.trigger("dirty:"+n)}))})),t.href||r.set("href",e.url(),{silent:!0}),r._isClean=!0,r._clean=r.properties().toJSON(),i.push((function(){e.trigger("add-command:"+n),e.trigger("clean:"+n)}))}}),this),delete t._commands),_.each(i,(function(t){t()})),t},toJSON:function(){var t={};return _.each(this.attributes,(function(e,i){e&&e.isHyperbone?t[i]=e.toJSON():t[i]=e||0===e||""===e?e:""}),this),_.isEmpty(this._links)||(t._links=this.rels()),this._commands&&(t._commands=this._commands.toJSON()),t},url:function(t){if(t)return _.extend(this._links,{self:{href:t}}),this;if(this._links.self&&this._links.self.href)return this._links.self.href;throw new Error("Not a hypermedia resource")},get:function(t){if(this.attributes[t]||0===this.attributes[t]||""===this.attributes[t])return this.attributes[t];if(-1!==_.indexOf(t,".")||/([a-zA-Z_]+)\[([0-9]+)\]/.test(t)){var e=t.split(".");t=e.shift();var i=e.join(".");if(this.attributes[t])return this.attributes[t].get(i);if(!(e=t.match(/([a-zA-Z_]+)\[([0-9]+)\]/)))return null;var n=parseInt(e[2],10);if(t=e[1],_.isNumber(n)&&this.attributes[t])return i?this.attributes[t].at(n).get(i):this.attributes[t].at(n)}return null},set:function(t,e,i){var n,s,r,o,a,h,c,l,u,p,d,f=this;if(t&&(t._links||t._commands||t._embedded)&&(t=this.parseHypermedia(t)),null==t)return this;if("object"==typeof t?(n=t,i=e):(n={})[t]=e,i||(i={}),!this._validate(n,i))return!1;if(s=i.unset,o=i.silent,r=[],a=this._changing,p=i.noTraverse||!1,d=i.ignoreDotNotation||!1,this._changing=!0,a||(this._previousAttributes=_.clone(this.attributes),this.changed={}),c=this.attributes,h=this._previousAttributes,this.idAttribute in n&&(this.id=n[this.idAttribute]),_.isEmpty(this.attributes)||_.each(n,(function(t,e){if(_.isObject(t)&&c[e]&&c[e].isHyperbone)if(_.isArray(t)||c[e].models){var i;t=_.isArray(t)?t:[t];var s=_.reduce(t,(function(t,e){return _.isObject(e)?t:t+1}),0);if(0===s){if(c[e].attributes){i=this._prototypes[e]?this._prototypes[e]:HyperboneModel;var r=new(Collection.extend({model:i}));r.add(c[e]),c[e]=r}if(0===t.length||0===c[e].length)c[e].reset(t);else if(c[e].length===t.length)c[e].each((function(e,i){e.set(t[i])}));else if(c[e].length>t.length){var o=[];c[e].each((function(i,n){t[n]?i.set(t[n]):o.push((function(){c[e].remove(i)}))})),_.each(o,(function(t){t()}))}else _.each(t,(function(t,i){c[e].at(i)?c[e].at(i).set(t):c[e].add(t)}));delete n[e]}}else c[e].set(t),delete n[e]}),this),_.each(n,(function(t,e){if(-1===e.indexOf(".")||d){if(_.isObject(t)&&!_.isArray(t))t.isHyperbone||p||((t=new(l=this._prototypes[e]?this._prototypes[e]:HyperboneModel)(t))._parent=f),t.on&&(t._trigger||(t._trigger=t.trigger,t.trigger=function(t){return function(){var e=Array.prototype.slice.call(arguments,0);this._trigger.apply(this,e),e[0]=e[0]+":"+t,f.trigger.apply(f,e)}}(e)));else if(_.isArray(t)){var i=!0;if(_.each(t,(function(t){_.isObject(t)||(i=!1)})),i){l=this._prototypes[e]?this._prototypes[e]:HyperboneModel;var n=new(Collection.extend({model:l}));n.reset(t),n._trigger=n.trigger,n.trigger=function(t){return function(){var e=Array.prototype.slice.call(arguments,0);this._trigger.apply(this,e),e[0]=e[0]+":"+t,f.trigger.apply(f,e)}}(e),t=n}}}else{u=e.split("."),e=u.pop();var o=u.join(".");this.get(o).set(e,t)}_.isEqual(c[e],t)||r.push(e),_.isEqual(h[e],t)?delete this.changed[e]:this.changed[e]=t,s?delete c[e]:c[e]=t}),this),!o){r.length&&(this._pending=!0);for(var m=0,g=r.length;m<g;m++)this.trigger("change:"+r[m],this,c[r[m]],i)}if(a)return this;if(!o)for(;this._pending;)this._pending=!1,this.trigger("change",this,i);return this._pending=!1,this._changing=!1,this},rel:function(t,e){var i=this._links[t]||{};if(!i)throw new Error("No such rel found");if(i.templated){if(!e)throw new Error("No data provided to expand templated uri");return i.template.expand(e)}return this._links&&this._links[t]?this._links[t].href?this._links[t].href:this._links[t]:""},rels:function(){return this._links},fullyQualifiedRel:function(t){var e=t.split(":");return this._curies[e[0]].expand({rel:e[1]})},command:function(t){var e;if(this._links[t]&&this._commands){var i=this._links[t].href.split(/\//g);"#_commands"!==i[0]&&"#commands"!==i[0]&&"#command"!==i[0]||(i=i.slice(1)),e=this._commands.get(i.join("."))}else this._commands&&(e=this._commands.get(t));return e||null},getCommandProperty:function(t){var e=t.split(".");return this.command(e[0]).get("properties").get(e[1])},setCommandProperty:function(t,e){var i=t.split(".");return this.command(i[0]).get("properties").set(i[1],e),this}}),HyperboneModel.extend=BackboneModel.extend,Command=HyperboneModel.extend({defaults:{method:"",href:"",properties:{}},initialize:function(){var t=this;this.on("clean",(function(){t._isClean||(t.properties().set(t._clean),t._isClean=!0,t._parentModel.trigger("clean:"+t._hbId))}))},reset:function(){this.off(),this.properties().off()},clean:function(){return this.trigger("clean"),this},properties:function(){return this.get("properties")},property:function(t){return this.get("properties").get(t)},pushTo:function(t){var e=t.properties(),i=this.properties();return _.each(e.attributes,(function(t,n){e.set(n,i.get(n))})),this},pullFrom:function(t){var e=this.properties(),i=t.properties();return _.each(e.attributes,(function(t,n){i.get(n)&&e.set(n,i.get(n))})),this},pull:function(){var t=this,e=this.properties();_.each(e.attributes,(function(i,n){e.set(n,t._parentModel.get(n))}))},push:function(){var t=this,e=this.properties();_.each(e.attributes,(function(e,i){t._parentModel.set(i,e)}))}}),module.exports.Model=HyperboneModel,module.exports.Collection=Collection;
//# sourceMappingURL=/sm/1a4546ab7956097cce9e654005bc1758d8f54185cde2faaa28138471b0942835.map