/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/sequelize-nested-set@1.6.2/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
const{cloneDeep:cloneDeep,warnDeprecated:warnDeprecated}=require("./helpers"),Sequelize=require("sequelize");module.exports=function(t,e,n,i={},o={}){const r=Sequelize.Op,s={lftColumnName:o.lftColumnName||"lft",rgtColumnName:o.rgtColumnName||"rgt",hasManyRoots:o.hasManyRoots||!1,rootColumnName:o.rootColumnName||"root_id",rootColumnType:o.rootColumnType||e.INTEGER,parentIdColumnName:o.parentIdColumnName||!1,parentIdColumnType:o.parentIdColumnType||e.INTEGER};void 0===o.levelColumnName?(warnDeprecated('Default value for levelColumnName option will be changed to FALSE. Set levelColumnName to "level" for easy upgrade'),s.levelColumnName="level"):s.levelColumnName=o.levelColumnName;const a={lft:{type:e.INTEGER,field:s.lftColumnName,allowNull:!1,defaultValue:1},rgt:{type:e.INTEGER,field:s.rgtColumnName,allowNull:!1,defaultValue:2},level:{type:s.levelColumnName?e.INTEGER:e.VIRTUAL,field:s.levelColumnName||void 0,allowNull:!s.levelColumnName},parentId:{type:s.parentIdColumnName&&s.parentIdColumnType?s.parentIdColumnType:e.VIRTUAL,field:s.parentIdColumnName||void 0,allowNull:!0}};s.hasManyRoots&&(a.rootId={type:s.rootColumnType,field:s.rootColumnName,allowNull:!1}),i={...a,...i};const l=t.define(n,i,o);return l.createRoot=async function(t,e={}){return s.hasManyRoots&&t.id&&!t.rootId&&(t.rootId=t.id),t.lft=1,t.rgt=2,t.level=0,t.parentId=null,t.rootId=t.rootId||0,await t.save(e),s.hasManyRoots&&(t.rootId||(t.rootId=t.id,await t.save())),t},l.fetchRoot=async function(t=1,e={}){(e=cloneDeep(e)).where=e.where||{},e.where.lft=1,e.where.rootId=t;return await l.findOne(e)||!1},l.fetchTree=async function(t=0,e=1,n={}){(n=cloneDeep(n)).where=n.where||{},n.where.lft={[r.gte]:1},n.where.rootId=e,t>0&&s.levelColumnName&&(n.where.level={[r.between]:[0,t]}),n.order=n.order||["lft"];let i=await l.findAll(n);return t>0&&!s.levelColumnName&&(i=l.generateAdditionalFields(i),i=i.filter((e=>e.level<=t))),i||!1},l.fetchRoots=async function(t={}){(t=cloneDeep(t)).where=t.where||{},t.where.lft=1;return await l.findAll(t)||!1},l.generateAdditionalFields=function(t,e=!1){if(s.levelColumnName&&s.parentIdColumnName&&!e)return t;let n=0,i=1/0,o=-1,r=0,a=0;const l={};return t.map((t=>{const s=cloneDeep(t);return i>s.lft&&(l[o]=r,o++,a=r,n=s.lft,i=s.rgt),i<s.lft-1&&(o-=s.lft-i-1,a=l[o-1]),o<0&&(o=0),n=s.lft,i=s.rgt,r=s.id,s.parentId=a,s.level=o,e&&s.save(),s}))},l.prototype.hasPrevSibling=async function(t={}){return this.isValidNode(await this.getPrevSibling(t))},l.prototype.hasNextSibling=async function(t={}){return this.isValidNode(await this.getNextSibling(t))},l.prototype.hasChildren=function(){return this.rgt-this.lft>1},l.prototype.hasParent=function(){return!this.isRoot()},l.prototype.getPrevSibling=async function(t={}){(t=cloneDeep(t)).where=t.where||{},t.where.rgt=this.lft-1,t.where.rootId=this.rootId;return await l.findOne(t)||!1},l.prototype.getNextSibling=async function(t={}){(t=cloneDeep(t)).where=t.where||{},t.where.lft=this.rgt+1,t.where.rootId=this.rootId;return await l.findOne(t)||!1},l.prototype.getSiblings=async function(t=!1,e={}){const n=await this.getParent(e);if(n){const i=await n.getChildren(e);if(i&&i.length)return t?i:i.filter((t=>!this.isEqualTo(t)))}return t?[this]:[]},l.prototype.getFirstChild=async function(t={}){(t=cloneDeep(t)).where=t.where||{},t.where.lft=this.lft+1,t.where.rootId=this.rootId;return await l.findOne(t)||!1},l.prototype.getLastChild=async function(t={}){(t=cloneDeep(t)).where=t.where||{},t.where.rgt=this.rgt-1,t.where.rootId=this.rootId;return await l.findOne(t)||!1},l.prototype.getChildren=async function(t={}){return await this.getDescendants(1,t)},l.prototype.getDescendants=async function(t=0,e={}){t=parseInt(t,10),(e=cloneDeep(e)).where=e.where||{},e.where.lft={[r.gt]:this.lft},e.where.rgt={[r.lt]:this.rgt},e.where.rootId=this.rootId,e.order=["lft"],s.levelColumnName&&(e.where.level=0===t?{[r.gte]:this.level+1}:{[r.between]:[this.level+1,this.level+t]});let n=await l.findAll(e);return t>0&&!s.levelColumnName&&n&&n.length&&(n=l.generateAdditionalFields([this].concat(n)),n=n.filter((e=>e.level>0&&e.level<=t))),n||!1},l.prototype.getParent=async function(t={}){if(this.isRoot())return!1;if(this.parentId)return await l.findByPk(this.parentId,t);const e=await this.getAncestors(1,t);return!(!e||!e.length)&&e[0]},l.prototype.getAncestors=async function(t=0,e={}){if(this.isRoot())return!1;t=parseInt(t,10),(e=cloneDeep(e)).where=e.where||{},e.where.lft={[r.lt]:this.lft},e.where.rgt={[r.gt]:this.rgt},e.where.rootId=this.rootId,e.order=["lft"],s.levelColumnName&&(e.where.level=0===t?{[r.lte]:this.level-1}:{[r.between]:[this.level-t,this.level-1]});let n=await l.findAll(e);if(t>0&&!s.levelColumnName&&n&&n.length){n=l.generateAdditionalFields(n);const e=n[n.length-1];n=n.filter((n=>n.level>=e.level-t+1))}return n||!1},l.prototype.getNumberChildren=async function(t={}){const e=await this.getChildren(t);return!1===e?0:e.length},l.prototype.getNumberDescendants=function(){return(this.rgt-this.lft-1)/2},l.prototype.insertAsParentOf=async function(e,n={}){if(this.isValidNode())throw"Cannot insert the node that has its place in the tree";if(this.isRoot())throw"Cannot insert the node as parent of root";if(e===this||this.isEqualTo(e))throw"Cannot insert node as parent of itself";const i=e.lft,o=e.rgt+2,a=e.rootId,h=e.level,c=async function(t){n={where:{},transaction:t,...n},await this.shiftRlValues(e.rgt+1,2,a,n);const c=cloneDeep(n);if(c.where.lft={[r.gte]:i},c.where.rgt={[r.lte]:o},c.where.rootId=a,await l.increment({lft:1,rgt:1,level:1},c),this.level=h,await this.insertNode(i,o,a,n),s.levelColumnName||s.parentIdColumnName){const t=await l.fetchTree(0,a);l.generateAdditionalFields(t,!0)}};return n.transaction?c(n.transaction):t.transaction(c)},l.prototype.insertAsPrevSiblingOf=async function(e,n={}){if(this.isValidNode())throw"Cannot insert the node that has its place in the tree";if(e===this||this.isEqualTo(e))throw"Cannot insert node as sibling of itself";const i=e.lft,o=e.lft+1,r=e.rootId,s=async t=>{n={transaction:t,...n},await this.shiftRlValues(i,2,r,n),this.level=e.level,this.parentId=e.parentId,await this.insertNode(i,o,r,n)};return n.transaction?s(n.transaction):t.transaction(s)},l.prototype.insertAsNextSiblingOf=async function(e,n={}){if(this.isValidNode())throw"Cannot insert the node that has its place in the tree";if(e===this||this.isEqualTo(e))throw"Cannot insert node as sibling of itself";const i=e.rgt+1,o=e.rgt+2,r=e.rootId,s=async t=>{n={transaction:t,...n},await this.shiftRlValues(i,2,r,n),this.level=e.level,this.parentId=e.parentId,await this.insertNode(i,o,r,n)};return n.transaction?s(n.transaction):t.transaction(s)},l.prototype.insertAsFirstChildOf=async function(e,n={}){if(this.isValidNode())throw"Cannot insert the node that has its place in the tree";if(e===this||this.isEqualTo(e))throw"Cannot insert node as child of itself";const i=e.lft+1,o=e.lft+2,r=e.rootId,s=async t=>{n={transaction:t,...n},await this.shiftRlValues(i,2,r,n),this.level=e.level+1,this.parentId=e.id,await this.insertNode(i,o,r,n)};return n.transaction?s(n.transaction):t.transaction(s)},l.prototype.insertAsLastChildOf=async function(e,n={}){if(this.isValidNode())throw"Cannot insert the node that has its place in the tree";if(e===this||this.isEqualTo(e))throw"Cannot insert node as child of itself";const i=e.rgt,o=e.rgt+1,r=e.rootId,s=async t=>{n={transaction:t,...n},await this.shiftRlValues(i,2,r,n),this.level=e.level+1,this.parentId=e.id,await this.insertNode(i,o,r,n)};return n.transaction?s(n.transaction):t.transaction(s)},l.prototype.moveBetweenTrees=async function(e,n,i,o={}){const s=async s=>{o={transaction:s,...o};const a=e.rootId,h=this.rootId,c=this.lft,d=this.rgt,f=this.level;switch(await this.shiftRlValues(n,d-c-1,a,o),this.rootId=a,this.detach(),await this.save(o),i){case"moveAsPrevSiblingOf":await this.insertAsPrevSiblingOf(e,o);break;case"moveAsFirstChildOf":await this.insertAsFirstChildOf(e,o);break;case"moveAsNextSiblingOf":await this.insertAsNextSiblingOf(e,o);break;case"moveAsLastChildOf":await this.insertAsLastChildOf(e,o);break;default:throw`Unknown move operation: ${i}.`}let u=d-c;this.rgt=this.lft+u,await this.save(o);const p=this.level-f,w=cloneDeep(o);w.where=w.where||{},w.where.lft={[r.gt]:c},w.where.rgt={[r.lt]:d},w.where.rootId=h,u=this.lft-c,await l.update({lft:t.literal(`lft + ${u}`),rgt:t.literal(`rgt + ${u}`),level:t.literal(`level + ${p}`),rootId:a},w);const g=d+1,v=c-d-1;await this.shiftRlValues(g,v,h,o)};return o.transaction?s(o.transaction):t.transaction(s)},l.prototype.moveAsPrevSiblingOf=async function(t,e={}){if(t===this||this.isAncestorOf(t)||this.isEqualTo(t))throw"Cannot move node as previous sibling of itself";if(t.rootId!=this.rootId)await this.moveBetweenTrees(t,t.lft,"moveAsPrevSiblingOf",e);else{const n=this.level;this.level=t.level,this.parentId=t.parentId,await this.updateNode(t.lft,this.level-n,e)}},l.prototype.moveAsNextSiblingOf=async function(t,e={}){if(t===this||this.isAncestorOf(t)||this.isEqualTo(t))throw"Cannot move node as next sibling of itself";if(t.rootId!=this.rootId)await this.moveBetweenTrees(t,t.rgt+1,"moveAsNextSiblingOf",e);else{const n=this.level;this.level=t.level,this.parentId=t.parentId,await this.updateNode(t.rgt+1,this.level-n,e)}},l.prototype.moveAsFirstChildOf=async function(t,e={}){if(t===this||this.isAncestorOf(t)||this.isEqualTo(t))throw"Cannot move node as first child of itself or into a descendant";if(t.rootId!=this.rootId)await this.moveBetweenTrees(t,t.lft+1,"moveAsFirstChildOf",e);else{const n=this.level;this.level=t.level+1,this.parentId=t.id,await this.updateNode(t.lft+1,this.level-n,e)}},l.prototype.moveAsLastChildOf=async function(t,e={}){if(t===this||this.isAncestorOf(t)||this.isEqualTo(t))throw"Cannot move node as last child of itself or into a descendant";if(t.rootId!=this.rootId)await this.moveBetweenTrees(t,t.rgt,"moveAsLastChildOf",e);else{const n=this.level;this.level=t.level+1,this.parentId=t.id,await this.updateNode(t.rgt,this.level-n,e)}},l.prototype.makeRoot=async function(e={}){if(this.isRoot()||!s.hasManyRoots)throw"Cannot make the node root because it is already root or you have disabled hasManyRoots";const n=this.rgt,i=this.lft,o=this.rootId,a=this.level;let h=this.id;isNaN(parseInt(e))||(warnDeprecated("Using makeRoot() with 2 params is deprecated from version 1.2.0. Please delete redundant newRootId"),h=e,arguments.length>1&&(e=arguments[1]));const c=async s=>{e={where:{},transaction:s,...e};const c=1-i,d=cloneDeep(e);d.where.lft={[r.gt]:i},d.where.rgt={[r.lt]:n},d.where.rootId=o,await l.update({lft:t.literal(`lft + ${c}`),rgt:t.literal(`rgt + ${c}`),level:t.literal(`level - ${a}`),rootId:h},d);const f=n+1,u=i-n-1;await this.shiftRlValues(f,u,this.rootId,e),this.lft=1,this.rgt=n-i+1,this.rootId=h,this.level=0,this.parentId=null,await this.save(e)};return e.transaction?c(e.transaction):t.transaction(c)},l.prototype.addChild=async function(t,e={}){await t.insertAsLastChildOf(this,e)},l.prototype.isLeaf=function(){return this.rgt-this.lft==1},l.prototype.isRoot=function(){return 1===parseInt(this.lft)},l.prototype.isEqualTo=function(t){return parseInt(t.lft)===parseInt(this.lft)&&parseInt(t.rgt)===parseInt(this.rgt)&&t.rootId==this.rootId},l.prototype.isDescendantOf=function(t){return t.lft<this.lft&&t.rgt>this.rgt&&t.rootId==this.rootId},l.prototype.isDescendantOfOrEqualTo=function(t){return t.lft<=this.lft&&t.rgt>=this.rgt&&t.rootId==this.rootId},l.prototype.isAncestorOf=function(t){return t.lft>this.lft&&t.rgt<this.rgt&&t.rootId==this.rootId},l.prototype.isValidNode=function(t=null){return null!==t?t.rgt>t.lft&&!t.isNewRecord:this.rgt>this.lft&&!this.isNewRecord},l.prototype.detach=function(){this.lft=0,this.rgt=0},l.prototype.delete=async function(e={}){const n=async t=>{e={where:{},transaction:t,...e};const n=this.rootId,i=cloneDeep(e);i.where[r.and]=[{lft:{[r.gte]:this.lft}},{lft:{[r.lte]:this.rgt}}],i.where.rootId=n,await l.destroy(i);const o=this.rgt+1,s=this.lft-this.rgt-1;await this.shiftRlValues(o,s,n,e)};return e.transaction?n(e.transaction):t.transaction(n)},l.prototype.insertNode=async function(t=0,e=0,n=1,i={}){this.lft=t,this.rgt=e,this.rootId=n,await this.save(i)},l.prototype.updateNode=async function(e,n,i={}){let o=this.lft,a=this.rgt;const h=this.rootId,c=a-o+1,d=async t=>{i={where:{},transaction:t,...i},await this.shiftRlValues(e,c,h,i),o>=e&&(o+=c,a+=c);const d=cloneDeep(i);d.by=n,d.where.lft={[r.gt]:o},d.where.rgt={[r.lt]:a},d.where.rootId=h,s.levelColumnName&&await l.increment("level",d),await this.shiftRlRange(o,a,e-o,h,i),await this.shiftRlValues(a+1,-c,h,i),await this.save(i)};return i.transaction?d(i.transaction):t.transaction(d)},l.prototype.shiftRlValues=async function(e,n,i=1,o={}){const s=async t=>{o={where:{},transaction:t,...o};const s=cloneDeep(o);s.by=n,s.where.lft={[r.gte]:e},s.where.rootId=i;const a=l.increment("lft",s),h=cloneDeep(o);h.by=n,h.where.rgt={[r.gte]:e},h.where.rootId=i;const c=l.increment("rgt",h);return Promise.all([a,c])};return o.transaction?s(o.transaction):t.transaction(s)},l.prototype.shiftRlRange=async function(e,n,i,o=1,s={}){const a=async t=>{s={where:{},transaction:t,...s};const a=cloneDeep(s);a.by=i,a.where.lft={[r.between]:[e,n]},a.where.rootId=o;const h=l.increment("lft",a),c=cloneDeep(s);c.by=i,c.where.rgt={[r.between]:[e,n]},c.where.rootId=o;const d=l.increment("rgt",c);return Promise.all([h,d])};return s.transaction?a(s.transaction):t.transaction(a)},l};
//# sourceMappingURL=/sm/c5e815b53458468b2b025e4fc612371444b400b5d2dcc22b053baae4f59047f6.map