/**
 * @name ranges-push
 * @fileoverview Gather string index ranges
 * @version 7.1.3
 * @author Roy Revelt
 * @license MIT
 * {@link https://codsen.com/os/ranges-push/}
 */

import{collWhitespace as m}from"string-collapse-leading-whitespace";import{existy as a,isStr as u,isInt as g,isNum as b}from"codsen-utils";import{rSort as h}from"ranges-sort";var c="7.1.3";var y={mergeType:1,progressFn:null,joinRangesThatTouchEdges:!0};function f(p,t){function r(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}if(!Array.isArray(p)||!p.length)return null;let n;if(t)if(r(t)){if(n={...y,...t},n.progressFn&&r(n.progressFn)&&!Object.keys(n.progressFn).length)n.progressFn=null;else if(n.progressFn&&typeof n.progressFn!="function")throw new Error(`ranges-merge: [THROW_ID_01] resolvedOpts.progressFn must be a function! It was given of a type: "${typeof n.progressFn}", equal to ${JSON.stringify(n.progressFn,null,4)}`);if(![1,2,"1","2"].includes(n.mergeType))throw new Error(`ranges-merge: [THROW_ID_02] resolvedOpts.mergeType was customised to a wrong thing! It was given of a type: "${typeof n.mergeType}", equal to ${JSON.stringify(n.mergeType,null,4)}`);if(typeof n.joinRangesThatTouchEdges!="boolean")throw new Error(`ranges-merge: [THROW_ID_04] resolvedOpts.joinRangesThatTouchEdges was customised to a wrong thing! It was given of a type: "${typeof n.joinRangesThatTouchEdges}", equal to ${JSON.stringify(n.joinRangesThatTouchEdges,null,4)}`)}else throw new Error(`ranges-merge: [THROW_ID_03] the second input argument must be a plain object. It was given as:
${JSON.stringify(t,null,4)} (type ${typeof t})`);else n={...y};let i=p.filter(e=>Array.isArray(e)).map(e=>[...e]).filter(e=>e[2]!==void 0||e[0]!==e[1]),s,o,l;n.progressFn?s=h(i,{progressFn:e=>{l=Math.floor(e/5),l!==o&&(o=l,n.progressFn!=null&&n.progressFn(l))}}):s=h(i);let d=s.length-1;for(let e=d;e>0;e--)n.progressFn&&(l=Math.floor((1-e/d)*78)+21,l!==o&&l>o&&(o=l,n.progressFn(l))),(s[e][0]<=s[e-1][0]||!n.joinRangesThatTouchEdges&&s[e][0]<s[e-1][1]||n.joinRangesThatTouchEdges&&s[e][0]<=s[e-1][1])&&(s[e-1][0]=Math.min(s[e][0],s[e-1][0]),s[e-1][1]=Math.max(s[e][1],s[e-1][1]),s[e][2]!==void 0&&(s[e-1][0]>=s[e][0]||s[e-1][1]<=s[e][1])&&s[e-1][2]!==null&&(s[e][2]===null&&s[e-1][2]!==null?s[e-1][2]=null:s[e-1][2]!=null?+(n||{})?.mergeType==2&&s[e-1][0]===s[e][0]?s[e-1][2]=s[e][2]:s[e-1][2]+=s[e][2]:s[e-1][2]=s[e][2]),s.splice(e,1),e=s.length);return s.length?s:null}var I=c,R={limitToBeAddedWhitespace:!1,limitLinebreaksCount:1,mergeType:1},$=class{constructor(t){let r={...R,...t};if(r.mergeType&&r.mergeType!==1&&r.mergeType!==2)if(u(r.mergeType)&&r.mergeType.trim()==="1")r.mergeType=1;else if(u(r.mergeType)&&r.mergeType.trim()==="2")r.mergeType=2;else throw new Error(`ranges-push: [THROW_ID_02] opts.mergeType was customised to a wrong thing! It was given of a type: "${typeof r.mergeType}", equal to ${JSON.stringify(r.mergeType,null,4)}`);this.opts=r,this.ranges=[]}ranges;opts;add(t,r,n){if(t==null&&r==null)return;if(a(t)&&!a(r)){if(Array.isArray(t)){if(t.length){if(t.some(o=>Array.isArray(o))){t.forEach(o=>{Array.isArray(o)&&this.add(...o)});return}t.length&&g(+t[0])&&g(+t[1])&&this.add(...t)}return}throw new TypeError(`ranges-push/Ranges/add(): [THROW_ID_12] the first input argument, "from" is set (${JSON.stringify(t,null,0)}) but second-one, "to" is not (${JSON.stringify(r,null,0)})`)}else if(!a(t)&&a(r))throw new TypeError(`ranges-push/Ranges/add(): [THROW_ID_13] the second input argument, "to" is set (${JSON.stringify(r,null,0)}) but first-one, "from" is not (${JSON.stringify(t,null,0)})`);let i=+t,s=+r;if(g(i)&&g(s)){if(a(n)&&!u(n)&&!b(n))throw new TypeError(`ranges-push/Ranges/add(): [THROW_ID_08] The third argument, the value to add, was given not as string but ${typeof n}, equal to:
${JSON.stringify(n,null,4)}`);if(a(this.ranges)&&Array.isArray(this.last())&&i===this.last()[1]){if(this.last()[1]=s,this.last()[2],this.last()[2]!==null&&a(n)){let o=this.last()[2]&&this.last()[2].length&&(!this.opts?.mergeType||this.opts.mergeType===1)?`${this.last()[2]}${n}`:n;this.opts.limitToBeAddedWhitespace&&(o=m(o,this.opts.limitLinebreaksCount)),u(o)&&!o.length||(this.last()[2]=o)}}else{this.ranges||(this.ranges=[]);let o=n!==void 0&&!(u(n)&&!n.length)?[i,s,n&&this.opts.limitToBeAddedWhitespace?m(n,this.opts.limitLinebreaksCount):n]:[i,s];this.ranges.push(o)}}else throw g(i)&&i>=0?new TypeError(`ranges-push/Ranges/add(): [THROW_ID_10] "to" value, the second input argument, must be a natural number or zero! Currently it's of a type "${typeof s}" equal to: ${JSON.stringify(s,null,4)}`):new TypeError(`ranges-push/Ranges/add(): [THROW_ID_09] "from" value, the first input argument, must be a natural number or zero! Currently it's of a type "${typeof i}" equal to: ${JSON.stringify(i,null,4)}`)}push(t,r,n){this.add(t,r,n)}current(){return Array.isArray(this.ranges)&&this.ranges.length?(this.ranges=f(this.ranges,{mergeType:this.opts.mergeType}),this.ranges&&this.opts.limitToBeAddedWhitespace?this.ranges.map(t=>a(t[2])?[t[0],t[1],m(t[2],this.opts.limitLinebreaksCount)]:t):this.ranges):null}wipe(){this.ranges=[]}replace(t){if(Array.isArray(t)&&t.length)if(Array.isArray(t[0])&&g(t[0][0]))this.ranges=Array.from(t);else throw new Error(`ranges-push/Ranges/replace(): [THROW_ID_11] Single range was given but we expected array of arrays! The first element, ${JSON.stringify(t[0],null,4)} should be an array and its first element should be an integer, a string index.`);else this.ranges=[]}last(){return Array.isArray(this.ranges)&&this.ranges.length?this.ranges[this.ranges.length-1]:null}};export{$ as Ranges,R as defaults,I as version};
