openalgo-charts 2.5.2 → 2.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -17
- package/dist/draw/index.d.ts +20 -1
- package/dist/index.d.ts +54 -4
- package/dist/openalgo-charts.draw.mjs +1 -1
- package/dist/openalgo-charts.draw.mjs.map +1 -1
- package/dist/openalgo-charts.mjs +1 -1
- package/dist/openalgo-charts.mjs.map +1 -1
- package/dist/openalgo-charts.standalone.js +1 -1
- package/dist/openalgo-charts.standalone.js.map +1 -1
- package/dist/openalgo-charts.widget.mjs +1 -1
- package/dist/openalgo-charts.widget.mjs.map +1 -1
- package/dist/widget/index.d.ts +160 -7
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const t="#787b86",e=[[.236,"#f23645"],[.25,"#7e57c2"],[.382,"#ff9800"],[.5,"#4caf50"],[.618,"#089981"],[.75,"#26c6da"],[.786,"#00bcd4"],[1.272,"#2962ff"],[1.414,"#9c27b0"],[1.618,"#e91e63"],[2.618,"#ff5252"],[3.618,"#9575cd"],[4.236,"#64b5f6"]],i=1e-6;function n(n){if(!Number.isFinite(n))return t;for(const[t,r]of e)if(Math.abs(t-n)<=i)return r;return t}const r=["#2962ff","#f23645","#089981","#ff9800","#9c27b0","#00bcd4","#e91e63","#4caf50"];function o(t){const e=r.length,i=Number.isFinite(t)?Math.trunc(t):0;return r[(i%e+e)%e]}function s(t){return`${(100*t).toFixed(1)}%`}function l(t){if(!Number.isFinite(t)||t<=0)return String(t);if(t>=1)return`${t}x1`;const e=1/t;return Math.abs(e-Math.round(e))<=i?`1x${Math.round(e)}`:`${t}x1`}function a(t){for(const e of t)Object.freeze(e);return Object.freeze(t)}function c(t){return t.map(t=>({...t}))}const h=a([0,.236,.382,.5,.618,.786,1].map(t=>({ratio:t,color:n(t)}))),d=a([.236,.382,.5,.618,.786,1].map(t=>({ratio:t,color:n(t)}))),u=a([0,.25,.382,.5,.618,.75,1].map(t=>({ratio:t,color:n(t)}))),p=a([.125,.25,.5,1,2,4,8].map((t,e)=>({ratio:t,color:o(e),label:l(t)}))),f=a([0,1,2,3,5,8,13,21,34,55].map(t=>({ratio:t})));function x(t,e,i,n,r){if(t.x===e.x)return;const o=n?-1/0:t.x,s=r?1/0:e.x;return i<Math.min(o,s)-1e-7||i>Math.max(o,s)+1e-7?void 0:t.y+(e.y-t.y)*(i-t.x)/(e.x-t.x)}function y(t,e){if(void 0===e)return;const i=t.fromY(e);return null!==i&&Number.isFinite(i)?{price:i}:void 0}function g(t=!1,e=!1){return(i,n)=>{if(!(i.pts.length<2||void 0!==n&&"line"!==n))return y(i,x(i.pts[0],i.pts[1],i.x,i.drawing.style.extendLeft??t,i.drawing.style.extendRight??e))}}function m(t=!1){return(e,i)=>{if(!(!e.pts[0]||t&&e.x<e.pts[0].x||void 0!==i&&"line"!==i))return{price:e.drawing.points[0].price}}}function b(){return[{id:"band",title:"Channel band"},{id:"base",title:"First boundary"},{id:"boundary",title:"Second boundary"},{id:"middle",title:"Middle"}]}function v(t){return(e,i="band")=>{if(e.pts.length<("disjoint"===t?4:3))return;const[n,r,o]=e.pts,s=o.y-(n.y+r.y)/2,l="disjoint"===t?[o,e.pts[3]]:"flat"===t?[{x:n.x,y:o.y},{x:r.x,y:o.y}]:[{x:n.x,y:n.y+s},{x:r.x,y:r.y+s}],a="parallel"!==t&&!0===e.drawing.style.extendLeft,c="parallel"!==t&&!0===e.drawing.style.extendRight,h=(i,n)=>"parallel"!==t&&i.x>n.x?x(n,i,e.x,a,c):x(i,n,e.x,a,c),d=h(n,r),u=h(l[0],l[1]);if("base"===i)return y(e,d);if("boundary"===i)return y(e,u);if(void 0===d||void 0===u)return;if("middle"===i)return y(e,(d+u)/2);if("band"!==i)return;const p=y(e,d),f=y(e,u);return p&&f?{price:Math.min(p.price,f.price),upperPrice:Math.max(p.price,f.price)}:void 0}}function w(t,e){return(t.style.levels??e).filter(t=>!1!==t.enabled&&Number.isFinite(t.ratio))}function M(t){return e=>[...new Map(w(e,t).map(t=>{const e=`ratio:${t.ratio}`;return[e,{id:e,title:t.label??s(t.ratio)}]})).values()]}function _(t,e){return(i,n)=>{const r=w(i.drawing,e).find(t=>`ratio:${t.ratio}`===n);if(!r||i.pts.length<(2===t?2:3))return;const[o,s,l]=i.pts,a=!0===i.drawing.style.extendLeft,c=!0===i.drawing.style.extendRight;if("channel"===t){const t=t=>({x:t.x+(l.x-s.x)*r.ratio,y:t.y+(l.y-s.y)*r.ratio});return y(i,x(t(o),t(s),i.x,a,c))}const h=i.pts[t-1];if(!a&&i.x<Math.min(o.x,h.x)||!c&&i.x>Math.max(o.x,h.x))return;const d=i.drawing.points;return{price:(2===t?d[0].price:d[2].price)+(d[1].price-d[0].price)*r.ratio}}}function k(t,e,i,n){const r=n.x-i.x,o=n.y-i.y,s=r*r+o*o;if(0===s)return Math.hypot(t-i.x,e-i.y);let l=((t-i.x)*r+(e-i.y)*o)/s;return l=l<0?0:l>1?1:l,Math.hypot(t-(i.x+l*r),e-(i.y+l*o))}function S(t,e,i,n){const r=n.x-i.x,o=n.y-i.y,s=Math.hypot(r,o);return 0===s?Math.hypot(t-i.x,e-i.y):Math.abs(o*(t-i.x)-r*(e-i.y))/s}function P(t,e){return Math.abs(t-e)}function T(t,e){return Math.abs(t-e)}function L(t,e,i){let n=1/0;for(let r=1;r<i.length;r++){const o=k(t,e,i[r-1],i[r]);o<n&&(n=o)}return n}function F(t,e){return{x0:Math.min(t.x,e.x),y0:Math.min(t.y,e.y),x1:Math.max(t.x,e.x),y1:Math.max(t.y,e.y)}}function I(t){let e=1/0,i=1/0,n=-1/0,r=-1/0;for(const o of t)o.x<e&&(e=o.x),o.y<i&&(i=o.y),o.x>n&&(n=o.x),o.y>r&&(r=o.y);return{x0:e,y0:i,x1:n,y1:r}}function A(t,e,i,n,r){const o=F(i,n);if(t>=o.x0&&t<=o.x1&&e>=o.y0&&e<=o.y1&&r)return 0;const s=[{x:o.x0,y:o.y0},{x:o.x1,y:o.y0},{x:o.x1,y:o.y1},{x:o.x0,y:o.y1}];let l=1/0;for(let i=0;i<4;i++){const n=k(t,e,s[i],s[(i+1)%4]);n<l&&(l=n)}return l}function N(t,e,i,n,r){const o=F(i,n),s=(o.x0+o.x1)/2,l=(o.y0+o.y1)/2,a=Math.max(1e-6,(o.x1-o.x0)/2),c=Math.max(1e-6,(o.y1-o.y0)/2),h=(t-s)/a,d=(e-l)/c,u=Math.hypot(h,d);return r&&u<=1?0:Math.abs(u-1)*Math.min(a,c)}function C(t,e,i,n,r){const o=e.x-t.x;if(0===o)return[t,e];const s=(e.y-t.y)/o;return[n?{x:0,y:t.y+(0-t.x)*s}:t,r?{x:i,y:t.y+(i-t.x)*s}:e]}function z(t){const e=function(t){const e=t.trim();if(e.startsWith("#")){const t=e.slice(1);if(3===t.length||4===t.length){const[e,i,n,r]=[...t].map(t=>parseInt(t+t,16));return[e,i,n].some(Number.isNaN)?null:{r:e,g:i,b:n,a:4===t.length?r/255:1}}if(6===t.length||8===t.length){const e=parseInt(t.slice(0,6),16);if(Number.isNaN(e))return null;const i=8===t.length?parseInt(t.slice(6,8),16)/255:1;return{r:e>>16&255,g:e>>8&255,b:255&e,a:Number.isNaN(i)?1:i}}return null}const i=/^rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*(?:,\s*([\d.]+)\s*)?\)$/i.exec(e);return null===i?null:{r:Number(i[1]),g:Number(i[2]),b:Number(i[3]),a:void 0===i[4]?1:Number(i[4])}}(t);if(null===e)return.5;const i=t=>{const e=t/255;return e<=.03928?e/12.92:Math.pow((e+.055)/1.055,2.4)};return.2126*i(e.r)+.7152*i(e.g)+.0722*i(e.b)}function O(t){return z(t)>.45?"#10131a":"#ffffff"}function D(t,e,i,n,r,o){const s=t.roundRect;"function"==typeof s?s.call(t,e,i,n,r,Math.max(0,Math.min(o,n/2,r/2))):t.rect(e,i,n,r)}const $=[{value:"solid",label:"Solid"},{value:"dashed",label:"Dashed"},{value:"dotted",label:"Dotted"}],H=[{value:"left",label:"Left"},{value:"center",label:"Center"},{value:"right",label:"Right"}],W=[{value:"top",label:"Top"},{value:"middle",label:"Middle"},{value:"bottom",label:"Bottom"}],B=[{value:"inside",label:"Inside"},{value:"outside",label:"Outside"}],V=[{value:"ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif",label:"Sans"},{value:"ui-serif, Georgia, Times New Roman, serif",label:"Serif"},{value:"ui-monospace, Menlo, Consolas, monospace",label:"Mono"}],R={path:"style.color",label:"Color",kind:"color",group:"line"},j={path:"style.lineWidth",label:"Width",kind:"number",min:1,max:12,step:.5,group:"line"},E={path:"style.lineStyle",label:"Line style",kind:"lineStyle",options:$,group:"line"},U=[R,j,E],J=[{path:"style.fill",label:"Fill",kind:"boolean",group:"fill"},{path:"style.fillColor",label:"Fill color",kind:"color",group:"fill"},{path:"style.fillOpacity",label:"Fill opacity",kind:"opacity",min:0,max:1,step:.01,group:"fill"}],q=[{path:"style.extendLeft",label:"Extend left",kind:"boolean",group:"behavior"},{path:"style.extendRight",label:"Extend right",kind:"boolean",group:"behavior"}],Y={path:"style.showLabels",label:"Show labels",kind:"boolean",group:"behavior"},K=[{path:"style.levels",label:"Levels",kind:"levels",group:"levels"},{...Y,group:"levels"}],X=[{path:"text.color",label:"Text color",kind:"color",group:"text"},{path:"text.fontSize",label:"Font size",kind:"number",min:6,max:96,step:1,group:"text"},{path:"text.fontFamily",label:"Font",kind:"select",options:V,group:"text"},{path:"text.bold",label:"Bold",kind:"boolean",group:"text"},{path:"text.italic",label:"Italic",kind:"boolean",group:"text"}],G={path:"text.value",label:"Text",kind:"text",group:"text"},Z=[G,...X,{path:"text.align",label:"Align",kind:"select",options:H,group:"text"},{path:"text.valign",label:"Vertical align",kind:"select",options:W,group:"text"},{path:"text.wrap",label:"Wrap",kind:"boolean",group:"text"},{path:"text.wrapWidth",label:"Wrap width",kind:"number",min:40,max:2e3,step:10,group:"text"},{path:"text.background",label:"Background",kind:"boolean",group:"text"},{path:"text.backgroundColor",label:"Background color",kind:"color",group:"text"},{path:"text.backgroundOpacity",label:"Background opacity",kind:"opacity",min:0,max:1,step:.01,group:"text"},{path:"text.border",label:"Border",kind:"boolean",group:"text"},{path:"text.borderColor",label:"Border color",kind:"color",group:"text"}],Q=[G,...X,{path:"text.align",label:"Align",kind:"select",options:H,group:"text"},{path:"text.valign",label:"Vertical align",kind:"select",options:W,group:"text"},{path:"text.wrap",label:"Wrap",kind:"boolean",group:"text"},{path:"text.position",label:"Label position",kind:"select",options:B,group:"text"}],tt=[G,...X,{path:"text.backgroundColor",label:"Plate color",kind:"color",group:"text"},{path:"text.backgroundOpacity",label:"Plate opacity",kind:"opacity",min:0,max:1,step:.01,group:"text"},{path:"text.border",label:"Border",kind:"boolean",group:"text"},{path:"text.borderColor",label:"Border color",kind:"color",group:"text"}];function et(t,e={}){const i=new Set,n=[];for(const e of t)for(const t of Array.isArray(e)?e:[e])i.has(t.path)||(i.add(t.path),n.push(t));return!0===e.textIsContent?{fields:n,textIsContent:!0}:{fields:n}}const it=new Set(["style","text","props"]),nt=new Set(["locked","visible","zIndex"]);function rt(t){const e=t.split(".");return 1===e.length&&nt.has(e[0])?{root:e[0],key:null}:2===e.length&&it.has(e[0])&&""!==e[1]?{root:e[0],key:e[1]}:null}function ot(t,e){const i=rt(e);if(null===i)return;if(null===i.key)return t[i.root];const n=t[i.root],r=n?.[i.key];return Array.isArray(r)?r.map(t=>({...t})):r}function st(t,e){const i={};for(const n of e.fields)i[n.path]=ot(t,n.path)??n.defaultValue;return i}const lt=new Set(["solid","dashed","dotted"]);function at(t){return"boolean"==typeof t?t:"true"===t||1===t||"1"===t||"false"!==t&&0!==t&&"0"!==t&&void 0}function ct(t){const e="number"==typeof t?t:"string"==typeof t&&""!==t.trim()?Number(t):NaN;return Number.isFinite(e)?e:void 0}function ht(t){if("object"!=typeof t||null===t)return null;const e=t,i=ct(e.ratio);if(void 0===i)return null;const n={ratio:i};"string"==typeof e.color&&""!==e.color&&(n.color=e.color);const r=at(e.enabled);return void 0!==r&&(n.enabled=r),"string"==typeof e.label&&""!==e.label&&(n.label=e.label),n}function dt(t,e){if(null!=e)switch(t.kind){case"color":case"text":return"string"==typeof e?e:String(e);case"number":{const i=ct(e);if(void 0===i)return;const n=t.min??-1/0,r=t.max??1/0;return i<n?n:i>r?r:i}case"opacity":{const t=ct(e);return void 0===t?void 0:t<0?0:t>1?1:t}case"boolean":return at(e);case"lineStyle":return"string"==typeof e&<.has(e)?e:void 0;case"select":if("string"!=typeof e)return;return void 0===t.options||t.options.some(t=>t.value===e)?e:void 0;case"levels":{if(!Array.isArray(e))return;const t=[];for(const i of e){const e=ht(i);null!==e&&t.push(e)}return t}default:return}}function ut(t,e,i){const n=void 0===i?null:new Map(i.fields.map(t=>[t.path,t])),r={},o={};for(const i of Object.keys(e)){const s=rt(i);if(null===s)continue;let l=e[i];if(null!==n){const t=n.get(i);if(void 0===t)continue;l=dt(t,l)}if(null===s.key){if(void 0===l)continue;"zIndex"===s.root?"number"==typeof l&&Number.isFinite(l)&&(r.zIndex=l):"boolean"==typeof l&&(r[s.root]=l);continue}const a=o[s.root]??{...t[s.root]};o[s.root]=a,void 0===l?delete a[s.key]:a[s.key]=l}if(void 0!==o.style&&(r.style=o.style),void 0!==o.props&&(r.props=o.props),void 0!==o.text){const t=o.text;"string"!=typeof t.value&&(t.value=""),r.text=t}return r}function pt(t,e,i){return t<e?e:t>i?i:t}function ft(t,e){const i=t.length;if(!(e>0)||i<3)return t.map(t=>({x:t.x,y:t.y}));const n=new Uint8Array(i);n[0]=1,n[i-1]=1;const r=[0,i-1];for(;r.length>0;){const i=r.pop(),o=r.pop(),s=t[o],l=t[i];let a=-1,c=e;for(let e=o+1;e<i;e++){const i=k(t[e].x,t[e].y,s,l);i>c&&(c=i,a=e)}a<0||(n[a]=1,r.push(o,a,a,i))}const o=[];for(let e=0;e<i;e++)1===n[e]&&o.push({x:t[e].x,y:t[e].y});return o}function xt(t,e,i=.5){const n=e.length;if(n<2)return;if(t.moveTo(e[0].x,e[0].y),2===n)return void t.lineTo(e[1].x,e[1].y);const r=pt(i,0,1)/3;for(let i=0;i<n-1;i++){const o=e[0===i?0:i-1],s=e[i],l=e[i+1],a=e[i+2<n?i+2:n-1];t.bezierCurveTo(s.x+(l.x-o.x)*r,s.y+(l.y-o.y)*r,l.x-(a.x-s.x)*r,l.y-(a.y-s.y)*r,l.x,l.y)}}function yt(t,e,i=.6){return t*(1+2*((Number.isFinite(e)?pt(e,0,1):.5)-.5)*pt(i,0,1))}const gt=new WeakMap;function mt(t,e){const i=gt.get(t);if(!i)return t.measureText(e).width;const n=t.font+"\0"+e,r=i.get(n);if(void 0!==r)return r;const o=t.measureText(e).width;return i.size<1024&&i.set(n,o),o}const bt=(t,e)=>({x:(t.x+e.x)/2,y:(t.y+e.y)/2}),vt=(t,e,i)=>({x:t.x+(e.x-t.x)*i,y:t.y+(e.y-t.y)*i}),wt=t=>Number.isFinite(t.x)&&Number.isFinite(t.y);function Mt(t,e){return{x:e.timeScale.indexToX(e.dataLayer.timeToIndexFloat(t.time)),y:e.priceScale.priceToY(t.price)}}function _t(t,e,i,n,r){const o=t.props?.[e];return"number"==typeof o&&Number.isFinite(o)?Math.max(n,Math.min(r,o)):i}function kt(t,e){return(t.style.levels??e).filter(t=>!1!==t.enabled&&Number.isFinite(t.ratio))}function St(t,e,i,n=0,r=1){if(!wt(t)||!wt(e))return[];const o=e.x-t.x,s=e.y-t.y;if(0===o&&0===s)return t.x>=0&&t.x<=i.plotWidth&&t.y>=0&&t.y<=i.plotHeight?[t,t]:[];for(const[e,l,a]of[[t.x,o,i.plotWidth],[t.y,s,i.plotHeight]]){if(0===l){if(e<0||e>a)return[];continue}const t=-e/l,i=(a-e)/l;if((n=Math.max(n,Math.min(t,i)))>(r=Math.min(r,Math.max(t,i))))return[]}return[vt(t,e,n),vt(t,e,r)]}function Pt(t,e,i){if(t.x===e.x)return St(t,e,i.rc);const n=e.x>=t.x;return St(t,e,i.rc,!0===(n?i.drawing.style.extendLeft:i.drawing.style.extendRight)?-1/0:0,!0===(n?i.drawing.style.extendRight:i.drawing.style.extendLeft)?1/0:1)}function Tt(t,e){if(!t.every(wt))return[];let i=t.slice();for(const[t,n,r]of[["x",0,1],["x",e.plotWidth,-1],["y",0,1],["y",e.plotHeight,-1]]){const e=i;i=[];for(let o=0;o<e.length;o++){const s=e[o],l=e[(o+1)%e.length],a=(s[t]-n)*r>=0,c=(l[t]-n)*r>=0;a&&i.push(s),a!==c&&i.push(vt(s,l,(n-s[t])/(l[t]-s[t])))}}return i}function Lt(t,e,i){let n=!1;for(let r=0,o=i.length-1;r<i.length;o=r++){const s=i[r],l=i[o];s.y>e!=l.y>e&&t<(l.x-s.x)*(e-s.y)/(l.y-s.y)+s.x&&(n=!n)}return n}function Ft(t,e,i,n,r){const o=Math.max(2,Math.min(96,Math.ceil(Math.abs(r)*Math.max(Math.abs(e),Math.abs(i))/4)));return Array.from({length:o+1},(s,l)=>{const a=n+r*l/o;return{x:t.x+e*Math.cos(a),y:t.y+i*Math.sin(a)}})}const It=2*Math.PI;function At(t,e){return wt(t)&&Number.isFinite(t.x*e)&&Number.isFinite(t.y*e)}function Nt(t,e){const i=t.arc;return void 0===i?t.points.length>=2&&t.points.every(t=>At(t,e)):function(t){return wt(t.center)&&[t.rx,t.ry,t.start,t.sweep,t.start+t.sweep].every(Number.isFinite)&&t.rx>0&&t.ry>0}(i)&&At(i.center,e)&&Number.isFinite(i.rx*e)&&Number.isFinite(i.ry*e)}function Ct(t,e){return{x:t.center.x+t.rx*Math.cos(e),y:t.center.y+t.ry*Math.sin(e)}}function zt(t,e){if(0===e.sweep)return!1;const i=((t-e.start)*Math.sign(e.sweep)%It+It)%It;return Math.abs(e.sweep)>=It-1e-12||i<=Math.abs(e.sweep)+1e-12}function Ot(t,e,i){if(i.rx===i.ry&&Math.abs(i.sweep)>=It-1e-12)return Math.abs(Math.hypot(t-i.center.x,e-i.center.y)-i.rx);const n=Ct(i,i.start),r=Ct(i,i.start+i.sweep),o=Math.hypot(t-n.x,e-n.y),s=Math.hypot(t-r.x,e-r.y),l=Math.min(o,s);if(i.rx===i.ry)return zt(Math.atan2(e-i.center.y,t-i.center.x),i)?Math.abs(Math.hypot(t-i.center.x,e-i.center.y)-i.rx):l;const a=n=>{const r=Ct(i,i.start+i.sweep*n);return Math.hypot(t-r.x,e-r.y)};let c=l,h=s<o?32:0;for(let t=0;t<=32;t++){const e=a(t/32);e<c&&(c=e,h=t)}let d=Math.max(0,(h-1)/32),u=Math.min(1,(h+1)/32);const p=Math.min(80,Math.max(32,2*Math.ceil(Math.log2(Math.max(i.rx,i.ry)))+8));for(let t=0;t<p;t++){const t=d+(u-d)/3,e=u-(u-d)/3;a(t)<a(e)?u=e:d=t}return Math.min(c,a((d+u)/2))}function Dt(t,e,i){const n=(t-i.center.x)/i.rx,r=(e-i.center.y)/i.ry;if(n*n+r*r>1)return!1;if(Math.abs(i.sweep)>=It-1e-12)return!0;if(!0===i.sector)return zt(Math.atan2(r,n),i);const o=i.start+i.sweep/2;return n*Math.cos(o)+r*Math.sin(o)>=Math.cos(Math.abs(i.sweep)/2)}function $t(t,e){const{ctx:i,rc:n,style:r}=t,o=n.dpr;i.save(),i.strokeStyle=r.color,i.lineWidth=Math.max(1,r.lineWidth*o),i.lineJoin="round",i.lineCap="round",i.setLineDash("dashed"===r.lineStyle?[6*o,4*o]:"dotted"===r.lineStyle?[o,3*o]:[]);for(const t of e.paths)if(Nt(t,o)){if(i.beginPath(),void 0!==t.arc){const e=t.arc;!0===e.sector&&i.moveTo(e.center.x*o,e.center.y*o),e.rx===e.ry?i.arc(e.center.x*o,e.center.y*o,e.rx*o,e.start,e.start+e.sweep,e.sweep<0):i.ellipse(e.center.x*o,e.center.y*o,e.rx*o,e.ry*o,0,e.start,e.start+e.sweep,e.sweep<0)}else{i.moveTo(t.points[0].x*o,t.points[0].y*o);for(let e=1;e<t.points.length;e++)i.lineTo(t.points[e].x*o,t.points[e].y*o)}!0===t.closed&&i.closePath(),!0===t.fill&&!0===r.fill&&(i.save(),i.globalAlpha*=Math.max(0,Math.min(1,r.fillOpacity??.12)),i.fillStyle=r.fillColor??r.color,i.fill("evenodd"),i.restore()),!1!==t.stroke&&(i.strokeStyle=t.color??r.color,i.stroke())}const s=t.drawing.text,l=(s?.fontSize??11)*o;i.font=`${!0===s?.italic?"italic ":""}${!0===s?.bold?"700 ":""}${l}px ${s?.fontFamily||"ui-sans-serif, system-ui, sans-serif"}`,i.textAlign="left",i.textBaseline="bottom";const a=[],c=l/o*1.3;for(const t of e.labels??[]){if(!At(t.at,o))continue;let{x:e,y:h}=t.at;if(e>=0&&e<=n.plotWidth&&h>=0&&h<=n.plotHeight){const r=mt(i,t.text)/o;e=Math.max(0,Math.min(e,n.plotWidth-r));let s=!1;for(const t of[0,-1,1,-2,2,-3,3,-4]){const i=h+t*c;if(!(i<l/o||i>n.plotHeight)&&!a.some(t=>e<t.x+t.width+3&&e+r+3>t.x&&Math.abs(i-t.y)<c-.01)){h=i,a.push({x:e,y:h,width:r}),s=!0;break}}if(!s)continue}i.fillStyle=s?.color??t.color??r.color,i.fillText(t.text,e*o,h*o)}i.restore()}function Ht(t,e){return{...t,draw(t){const i=t.pts.map(e=>({x:e.x/t.rc.dpr,y:e.y/t.rc.dpr}));$t(t,e({pts:i,drawing:{...t.drawing,style:t.style},rc:t.rc}))},distance:(t,i,n)=>t<0||i<0||t>n.rc.plotWidth||i>n.rc.plotHeight?null:function(t,e,i,n,r=1){let o=1/0;for(const s of i.paths){if(!Nt(s,r))continue;if(void 0!==s.arc){const i=s.arc;if(!0===s.fill&&!0===n.style.fill&&(n.style.fillOpacity??.12)>0&&Dt(t,e,i))return 0;if(!1!==s.stroke&&(o=Math.min(o,Ot(t,e,i)),!0===i.sector||!0===s.closed)){const n=Ct(i,i.start),r=Ct(i,i.start+i.sweep);!0===i.sector?(o=Math.min(o,k(t,e,i.center,n)),!0===s.closed&&(o=Math.min(o,k(t,e,r,i.center)))):o=Math.min(o,k(t,e,r,n))}continue}const i=s.points;if(!0===s.fill&&!0===n.style.fill&&(n.style.fillOpacity??.12)>0&&Lt(t,e,i))return 0;if(!1!==s.stroke){for(let n=1;n<i.length;n++)o=Math.min(o,k(t,e,i[n-1],i[n]));!0===s.closed&&(o=Math.min(o,k(t,e,i[i.length-1],i[0])))}}return Number.isFinite(o)?o:null}(t,i,e(n),n.drawing,n.rc.dpr)}}const Wt=et([U,J,q]),Bt=[{ratio:0},{ratio:.5},{ratio:1}],Vt=[0,.382,.618,1,1.272,1.618,2,2.618,3.618,4.236].map(t=>({ratio:t})),Rt=[0,.382,.5,.618,1].map(t=>({ratio:t,color:n(t)})),jt=(t,e)=>({points:[t,e]});function Et(t,e,i){const n=e.x-t.x;if(0===n)return[t,e];const r=!0===i.drawing.style.extendLeft?0:Math.min(t.x,e.x),o=!0===i.drawing.style.extendRight?i.rc.plotWidth:Math.max(t.x,e.x),s=i=>({x:i,y:t.y+(e.y-t.y)*(i-t.x)/n});return n>0?[s(r),s(o)]:[s(o),s(r)]}function Ut(t,e){const[i,n]=t.pts,r=Pt(i,n,t),o=Pt(e[0],e[1],t),s=[];if(!0===t.drawing.style.fill){const r=Et(i,n,t),o=Et(e[0],e[1],t);s.push({points:Tt([r[0],r[1],o[1],o[0]],t.rc),closed:!0,fill:!0,stroke:!1})}return s.push({points:r},{points:o}),{paths:s}}const Jt=Ht({id:"disjoint-channel",name:"Disjoint Channel",points:4,alertValue:v("disjoint"),alertLevels:b,defaultStyle:{fill:!0},settings:Wt},t=>t.pts.length<4?{paths:[]}:Ut(t,t.pts.slice(2,4))),qt=Ht({id:"flat-top-bottom",name:"Flat Top/Bottom",points:3,alertValue:v("flat"),alertLevels:b,defaultStyle:{fill:!0},settings:Wt,constrain(t){const e=t.map(t=>({...t}));return e.length>=3&&(e[2].time=(e[0].time+e[1].time)/2),e}},t=>t.pts.length<3?{paths:[]}:Ut(t,[{x:t.pts[0].x,y:t.pts[2].y},{x:t.pts[1].x,y:t.pts[2].y}]));function Yt(t,e,i){let n=0,r=t.length;for(;n<r;){const o=n+r>>>1;t[o].time<e||i&&t[o].time===e?n=o+1:r=o}return n}const Kt=Ht({id:"regression-channel",name:"Regression Channel",points:2,defaultStyle:{fill:!0,showLabels:!0},settings:et([U,J,q,Y,X,{path:"props.deviation",label:"Deviation multiplier",kind:"number",min:.01,max:20,step:.1,group:"behavior"}])},t=>{if(t.pts.length<2)return{paths:[]};const e=t.rc.bars?.()??[],[i,n]=t.drawing.points,r=Yt(e,Math.min(i.time,n.time),!1),o=Yt(e,Math.max(i.time,n.time),!0);let s=0,l=0,a=0,c=0,h=0,d=0,u=0,p=0,f=0,x=0;for(let i=r;i<o;i++){const n=e[i],r=n.close,o=t.rc.dataLayer.timeToIndex?.(n.time)??t.rc.dataLayer.timeToIndexFloat(n.time);if(!Number.isFinite(o)||!Number.isFinite(r))continue;0===s&&(u=n.time,f=o),p=n.time,x=o,s++;const y=o-l,g=r-a;l+=y/s,a+=g/s,c+=y*(o-l),h+=y*(r-a),d+=g*(r-a)}if(0===s)return{paths:[],labels:!1===t.drawing.style.showLabels?[]:[{at:t.pts[0],text:"No bars in range"}]};const y=c>0?h/c:0,g=Math.sqrt(Math.max(0,d-y*h)/s)*_t(t.drawing,"deviation",2,.01,20),m=d>0?Math.max(0,Math.min(1,y*h/d)):1,b=(e,i,n)=>{return Mt({time:e,price:(r=i,a+y*(r-l)+n)},t.rc);var r},v=[0,g,-g].map(e=>({points:Pt(b(u,f,e),b(p,x,e),t)}));if(!0===t.drawing.style.fill){const e=Et(b(u,f,g),b(p,x,g),t),i=Et(b(u,f,-g),b(p,x,-g),t);v.push({points:Tt([e[0],e[1],i[1],i[0]],t.rc),closed:!0,fill:!0,stroke:!1})}return{paths:v,labels:!1===t.drawing.style.showLabels?[]:[{at:b(u,f,g),text:`R^2 ${m.toFixed(3)} ${s} bars`}]}});function Xt(t,e,i){return Ht({id:t,name:e,points:3,defaultStyle:{fill:!0,showLabels:!0,levels:c(Bt)},settings:et([U,J,K,X])},t=>{if(t.pts.length<3)return{paths:[]};const[e,n,r]=t.pts,o=bt(n,r),[l,a]=t.drawing.points,c={time:"schiff"===i?l.time:(l.time+a.time)/2,price:(l.price+a.price)/2},h="standard"===i?e:Mt(c,t.rc),d="inside"===i?o:h,u="inside"===i?{x:r.x-h.x,y:r.y-h.y}:{x:o.x-h.x,y:o.y-h.y},p=e=>St(e,{x:e.x+u.x,y:e.y+u.y},t.rc,0,1/0),f=[],x=[],y=kt(t.drawing,Bt);for(const e of y){const i=e.color,l=0===e.ratio?[d]:[vt(o,n,e.ratio),vt(o,r,e.ratio)];for(const n of l){const r=p(n);f.push({points:r,color:i}),!1!==t.drawing.style.showLabels&&2===r.length&&x.push({at:{x:r[0].x+4,y:r[0].y-4},text:e.label??s(e.ratio),color:i})}}const g=y.reduce((t,e)=>Math.max(t,Math.abs(e.ratio)),0);if(!0===t.drawing.style.fill&&g>0){const e=vt(o,n,g),i=vt(o,r,g),s=Math.hypot(u.x,u.y);if(s>0){const n=(Math.max(Math.hypot(e.x,e.y),Math.hypot(i.x,i.y))+Math.hypot(t.rc.plotWidth,t.rc.plotHeight))/s+1,r=t=>({x:t.x+u.x*n,y:t.y+u.y*n});f.unshift({points:Tt([e,r(e),r(i),i],t.rc),closed:!0,fill:!0,stroke:!1})}}return f.push(jt(n,r)),"inside"===i&&f.push(jt(h,r),jt(e,n)),{paths:f,labels:x}})}const Gt=Ht({id:"info-line",name:"Info Line",points:2,angleLock:!0,defaultStyle:{showLabels:!0},settings:et([U,q,Y,X])},t=>{if(t.pts.length<2)return{paths:[]};const[e,i]=t.pts,[n,r]=t.drawing.points,o=r.price-n.price,s=o>=0?"+":"",l=0===n.price?"n/a":`${s}${(o/Math.abs(n.price)*100).toFixed(2)}%`,a=Math.abs(Math.round(t.rc.dataLayer.timeToIndexFloat(r.time)-t.rc.dataLayer.timeToIndexFloat(n.time)));return{paths:[{points:Pt(e,i,t)}],labels:!1===t.drawing.style.showLabels?[]:[{at:{x:(e.x+i.x)/2,y:(e.y+i.y)/2-8},text:`${s}${t.rc.priceScale.format(o)} (${l}) ${a} bars`}]}}),Zt=Ht({id:"trend-angle",name:"Trend Angle",points:2,angleLock:!0,alertValue:g(),defaultStyle:{showLabels:!0},settings:et([U,Y,X])},t=>{if(t.pts.length<2)return{paths:[]};const[e,i]=t.pts,n=Math.atan2(i.y-e.y,i.x-e.x),r=Math.max(12,Math.min(40,Math.hypot(i.x-e.x,i.y-e.y)/3));return{paths:[jt(e,i),jt(e,{x:e.x+1.5*r,y:e.y}),{points:Ft(e,r,r,0,n)}],labels:!1===t.drawing.style.showLabels?[]:[{at:{x:e.x+r+6,y:e.y-6},text:`${(180*-n/Math.PI).toFixed(1)} deg`}]}}),Qt=Ht({id:"fib-extension-two-point",name:"Fib Extension (Two Point)",points:2,alertValue:_(2,Vt),alertLevels:M(Vt),defaultStyle:{levels:c(Vt),showLabels:!0,fill:!0,fillOpacity:.06},settings:et([U,K,J,q,X])},t=>{if(t.pts.length<2)return{paths:[]};const[e,i]=t.pts,[r,o]=t.drawing.points,l=[],a=[],c=kt(t.drawing,Vt);if(!0===t.drawing.style.fill&&e.x!==i.x){const n=c.map(e=>t.rc.priceScale.priceToY(r.price+(o.price-r.price)*e.ratio)).filter(Number.isFinite).sort((t,e)=>t-e),s=!0===t.drawing.style.extendLeft?0:Math.min(e.x,i.x),a=!0===t.drawing.style.extendRight?t.rc.plotWidth:Math.max(e.x,i.x);for(let e=1;e<n.length;e++)n[e]!==n[e-1]&&l.push({points:Tt([{x:s,y:n[e-1]},{x:a,y:n[e-1]},{x:a,y:n[e]},{x:s,y:n[e]}],t.rc),closed:!0,fill:!0,stroke:!1})}l.push(jt(e,i));for(const h of c){const c=r.price+(o.price-r.price)*h.ratio,d=t.rc.priceScale.priceToY(c),u=h.color??n(h.ratio);l.push({points:Pt({x:e.x,y:d},{x:i.x,y:d},t),color:u}),!1!==t.drawing.style.showLabels&&a.push({at:{x:Math.min(e.x,i.x)+4,y:d-3},text:`${h.label??s(h.ratio)} ${t.rc.priceScale.format(c)}`,color:u})}return{paths:l,labels:a}});function te(t,e){if(t.pts.length<2)return{paths:[]};const[i,r]=t.pts,o=[],s=[];o.push({points:[{x:i.x,y:r.y},r,{x:r.x,y:i.y}]});const l=1.2*(t.drawing.text?.fontSize??11),a=[],c=(n,r,o,c,h)=>{const d=Math.min(i.x,r.x),u=Math.max(i.x,r.x),p=Math.min(i.y,r.y),f=Math.max(i.y,r.y);if(!e||!1===t.drawing.style.showLabels||a.length>=32)return;const x=e(c);if(!(!Number.isFinite(x)||x<=0||!Number.isFinite(l)||l<=0||(o?f-p<l:u-d<x)))for(const e of[0,-l,l]){let y,g;o?(y=r.x>=i.x?r.x+4:r.x-x-4,(y<0||y+x>t.rc.plotWidth)&&(y=r.x>=i.x?r.x-x-4:r.x+4),g=Math.max(p+l,Math.min(f,n.y+l/2+e))):(y=Math.max(d,Math.min(u-x,n.x-x/2+e)),g=r.y<i.y?r.y-4:r.y+l+4,(g-l<0||g>t.rc.plotHeight)&&(g=r.y<i.y?r.y+l+4:r.y-4));const m={x:y,y:g-l,width:x,height:l};if(!(y<0||y+x>t.rc.plotWidth||m.y<0||g>t.rc.plotHeight)&&!a.some(t=>y<t.x+t.width+3&&y+x+3>t.x&&m.y<t.y+t.height+3&&m.y+l+3>t.y))return a.push(m),void s.push({at:{x:y,y:g},text:c,color:h})}};for(const e of kt(t.drawing,Rt)){const s=[{x:r.x,y:i.y+(r.y-i.y)*e.ratio}];1!==e.ratio&&s.push({x:i.x+(r.x-i.x)*e.ratio,y:r.y}),s.forEach((s,l)=>{const a=St(i,s,t.rc,0,1/0),h=e.color??n(e.ratio);if(o.push({points:a,color:h}),2===a.length){const t={x:i.x+(Math.sign(s.x-i.x)||Math.sign(r.x-i.x))*Math.abs(r.x-i.x),y:i.y+(Math.sign(s.y-i.y)||Math.sign(r.y-i.y))*Math.abs(r.y-i.y)},n=Math.abs(e.ratio),o=vt(i,s,1/Math.max(1,n));c(o,t,n<=1?0===l:0!==l,e.label??String(e.ratio),h)}})}return{paths:o,labels:s}}const ee={...Ht({id:"fib-speed-resistance-fan",name:"Fib Speed Resistance Fan",points:2,defaultStyle:{levels:c(Rt),showLabels:!0},settings:et([U,K,X])},t=>te(t)),draw(t){const{ctx:e,rc:i}=t,n=t.drawing.text;let r;e.save();try{e.font=`${!0===n?.italic?"italic ":""}${!0===n?.bold?"700 ":""}${(n?.fontSize??11)*i.dpr}px ${n?.fontFamily||"ui-sans-serif, system-ui, sans-serif"}`,r=te({rc:i,drawing:{...t.drawing,style:t.style},pts:t.pts.map(t=>({x:t.x/i.dpr,y:t.y/i.dpr}))},t=>e.measureText(t).width/i.dpr)}finally{e.restore()}$t(t,r)}},ie=Ht({id:"icon-stamp",name:"Icon Stamp",points:1,defaultStyle:{fill:!0,fillOpacity:.8},settings:et([U,J,{path:"props.shape",label:"Shape",kind:"select",group:"behavior",options:["star","diamond","circle","arrow up","arrow down","check","cross"].map(t=>({value:t.replace(" ","-"),label:t[0].toUpperCase()+t.slice(1)}))},{path:"props.size",label:"Size",kind:"number",min:8,max:160,step:2,group:"behavior"}])},t=>{if(t.pts.length<1)return{paths:[]};const e=t.pts[0],i=_t(t.drawing,"size",28,8,160)/2,n=(t,n)=>({x:e.x+t*i,y:e.y+n*i}),r=t.drawing.props?.shape;let o;if("diamond"===r)o=[n(0,-1),n(1,0),n(0,1),n(-1,0)];else if("circle"===r)o=Ft(e,i,i,0,2*Math.PI);else if("arrow-up"===r||"arrow-down"===r){const t="arrow-up"===r?1:-1;o=[[0,-1],[1,0],[.35,0],[.35,1],[-.35,1],[-.35,0],[-1,0]].map(([e,i])=>n(e,i*t))}else o="check"===r?[[-1,0],[-.65,-.3],[-.2,.2],[.75,-.85],[1,-.55],[-.2,.85]].map(([t,e])=>n(t,e)):"cross"===r?[[-1,-.65],[-.65,-1],[0,-.35],[.65,-1],[1,-.65],[.35,0],[1,.65],[.65,1],[0,.35],[-.65,1],[-1,.65],[-.35,0]].map(([t,e])=>n(t,e)):Array.from({length:10},(t,e)=>{const i=-Math.PI/2+e*Math.PI/5,r=e%2==0?1:.45;return n(Math.cos(i)*r,Math.sin(i)*r)});return{paths:[{points:o,closed:!0,fill:!0}]}}),ne=[Jt,qt,Kt,Xt("pitchfork","Pitchfork","standard"),Xt("schiff-pitchfork","Schiff Pitchfork","schiff"),Xt("modified-schiff-pitchfork","Modified Schiff Pitchfork","modified"),Xt("inside-pitchfork","Inside Pitchfork","inside"),Gt,Zt,Qt,ee,ie],re=2*Math.PI,oe=(1+Math.sqrt(5))/2,se=t=>t.map(t=>({ratio:t})),le=se([.236,.382,.5,.618,.786,1,1.618,2.618]),ae=le.slice(0,6),ce=se([0,.382,.618,1,1.382,1.618,2,2.382,2.618,3]),he=se([0,.25,.382,.5,.618,.75,1]),de=se(Array.from({length:12},(t,e)=>e+1)),ue=se([.236,.382,.5,.618,.786,1,1.618,2.618,4.236,6.854,11.09]),pe=et([U,K,X]),fe=et([U,J,K,X]),xe=(t,e,i)=>({points:[t,e],color:i}),ye=(t,e,i=e,n=0,r=re,o)=>({points:[],arc:{center:t,rx:e,ry:i,start:n,sweep:r},color:o}),ge=(t,e=2)=>t.pts.length>=e&&t.pts.every(wt),me=(t,e,i)=>!1===t.drawing.style.showLabels?[]:[{at:i,text:e.label??String(e.ratio),color:e.color}];function be(t){return ge(t)?{paths:[xe(t.pts[0],t.pts[1])]}:{paths:[]}}const ve=Ht({id:"trend-fib-time",name:"Trend Fib Time",points:3,defaultStyle:{levels:c(ce)},settings:pe},t=>{if(!ge(t,3))return be(t);const[e,i,n]=t.drawing.points,r=e=>t.rc.dataLayer.timeToIndexFloat(e),o=r(i.time)-r(e.time),s=r(n.time),l=[],a=[];for(const e of kt(t.drawing,ce)){const i=t.rc.timeScale.indexToX(s+o*e.ratio);!Number.isFinite(i)||i<0||i>t.rc.plotWidth||(l.push(xe({x:i,y:0},{x:i,y:t.rc.plotHeight},e.color)),a.push(...me(t,e,{x:i+3,y:15})))}return{paths:l,labels:a}});function we(t,e,i){const n="wedge"===i?ae:le;return Ht({id:t,name:e,points:"wedge"===i?3:2,defaultStyle:{fill:!1,levels:c(n)},settings:fe},t=>{if(!ge(t,"wedge"===i?3:2))return be(t);const[e,r,o]=t.pts,s=Math.hypot(r.x-e.x,r.y-e.y);if(!Number.isFinite(s)||0===s)return be(t);let l=0,a=re;const c=[],h=[];if("circle"!==i)if(l=Math.atan2(r.y-e.y,r.x-e.x),c.push(xe(e,r)),"arc"===i)l-=Math.PI/2,a=Math.PI;else{const t=Math.atan2(o.y-e.y,o.x-e.x);a=(t-l+3*Math.PI)%re-Math.PI,c.push(xe(e,{x:e.x+s*Math.cos(t),y:e.y+s*Math.sin(t)}))}let d=0;for(const r of kt(t.drawing,n)){const n=s*r.ratio;if(!(n>0&&Number.isFinite(n)))continue;d=Math.max(d,n),c.push(ye(e,n,n,l,a,r.color));const o="circle"===i?0:l+a/2;h.push(...me(t,r,{x:e.x+n*Math.cos(o)+3,y:e.y+n*Math.sin(o)}))}if(d>0&&!0===t.drawing.style.fill){const t=ye(e,d,d,l,a);t.arc.sector="circle"!==i,c.unshift({...t,fill:!0,stroke:!1,closed:!0})}return{paths:c,labels:h}})}const Me=Ht({id:"fib-spiral",name:"Fib Spiral",points:2,settings:et([U])},t=>{if(!ge(t))return{paths:[]};const[e,i]=t.pts,n=Math.hypot(i.x-e.x,i.y-e.y);if(!Number.isFinite(n)||n<.5)return be(t);const r=Math.log(oe)/(Math.PI/2),o=Math.atan2(i.y-e.y,i.x-e.x),s=Math.max(-16*Math.PI,Math.log(.5/n)/r),l=[],a=[s];for(let t=Math.ceil(s/(Math.PI/2));t<2;t++)a.push(t*Math.PI/2);a.push(Math.PI);for(let t=1;t<a.length;t++){const i=a[t-1],s=a[t],c=Math.max(2,Math.min(32,Math.ceil((s-i)*Math.sqrt(n*Math.exp(r*s)/.8))));for(let a=1===t?0:1;a<=c;a++){const t=i+(s-i)*a/c,h=n*Math.exp(r*t);l.push({x:e.x+h*Math.cos(o+t),y:e.y+h*Math.sin(o+t)})}}return{paths:[{points:l}]}}),_e=Ht({id:"gann-square",name:"Gann Square",points:2,defaultStyle:{fill:!1,levels:c(he)},settings:fe},t=>{if(!ge(t))return{paths:[]};const[e,i]=t.pts,[n,r]=t.drawing.points,o=i.x-e.x,s=i.y-e.y,l=Math.min(e.x,i.x),a=Math.max(e.x,i.x),c=Math.min(e.y,i.y),h=Math.max(e.y,i.y),d=[],u=[];!0===t.drawing.style.fill&&d.push({points:[{x:l,y:c},{x:a,y:c},{x:a,y:h},{x:l,y:h}],closed:!0,fill:!0,stroke:!1});const p=t.rc.dataLayer.timeToIndexFloat(n.time),f=t.rc.dataLayer.timeToIndexFloat(r.time);for(const e of kt(t.drawing,he)){const i=t.rc.timeScale.indexToX(p+(f-p)*e.ratio),o=t.rc.priceScale.priceToY(n.price+(r.price-n.price)*e.ratio);d.push(xe({x:l,y:o},{x:a,y:o},e.color),xe({x:i,y:c},{x:i,y:h},e.color)),u.push(...me(t,e,{x:l+3,y:o-3}))}for(const[i,n,r]of[[1,1/3,"3x1"],[1,.5,"2x1"],[1,1,"1x1"],[.5,1,"1x2"],[1/3,1,"1x3"]]){const l={x:e.x+o*i,y:e.y+s*n};d.push(xe(e,l)),!1!==t.drawing.style.showLabels&&u.push({at:{x:l.x+3,y:l.y},text:r})}if(0!==o&&0!==s)for(const t of[.25,.5,.75,1])d.push(ye(e,Math.abs(o)*t,Math.abs(s)*t,o>0?0:Math.PI,Math.sign(o*s)*Math.PI/2));return{paths:d,labels:u}});function ke(t,e,i,n,r,o){const s=[-Math.PI,0];for(const r of[i,n]){const i=(r-t.x)/e;Math.abs(i)<1&&s.push(-Math.acos(i))}for(const i of[r,o]){const n=(i-t.y)/e;if(n>-1&&n<0){const t=Math.asin(n);s.push(t,-Math.PI-t)}}s.sort((t,e)=>t-e);const l=[];for(let a=1;a<s.length;a++){const c=s[a-1],h=s[a]-c;if(h<1e-12)continue;const d=c+h/2,u=t.x+e*Math.cos(d),p=t.y+e*Math.sin(d);u<i||u>n||p<r||p>o||l.push(ye(t,e,e,c,h))}return l}const Se=Ht({id:"dedekind-tessellation",name:"Dedekind Tessellation",points:2,settings:et([U,{path:"props.maxCurvature",label:"Max curvature",kind:"number",min:1,max:64,step:1,group:"behavior"}])},t=>{if(!ge(t))return{paths:[]};const[e,i]=t.pts,n=Math.min(e.x,i.x),r=Math.max(e.x,i.x),o=Math.min(e.y,i.y),s=Math.max(e.y,i.y),l=s-o;if(l<.75||r-n<.75||!Number.isFinite(l))return be(t);const a=Math.max(0,n),c=Math.min(t.rc.plotWidth,r),h=Math.max(0,o),d=Math.min(t.rc.plotHeight,s);if(a>c||h>d)return{paths:[]};const u=[{x:n,y:o},{x:r,y:o},{x:r,y:s},{x:n,y:s}],p=u.map((e,i)=>({points:St(e,u[(i+1)%4],t.rc)})),f=(a-n)/l,x=(c-n)/l,y=Math.ceil(f-.5)+.5,g=Math.min(2048,Math.floor(x-y)+1);for(let t=0;t<g;t++){const e=n+(y+t)*l;p.push(xe({x:e,y:d},{x:e,y:h}))}const m=Math.min(Math.round(_t(t.drawing,"maxCurvature",24,1,64)),Math.floor(l/.75));for(let t=1;t<=m&&p.length<4096;t++){if(t%2==0&&t%8!=0)continue;const e=l/t;if(!(s-e>d))for(let i=0;i<t&&p.length<4096;i++){const r=i*i-1;if(r%t!==0||t%8==0&&Math.abs(r/t)%2!=1)continue;const o=Math.ceil(f-1/t-i/t),u=Math.floor(x+1/t-i/t),y=Math.min(4096-p.length,u-o+1);for(let r=0;r<y;r++){const u={x:n+(o+r+i/t)*l,y:s};p.push(...ke(u,e,a,c,h,d))}}}return{paths:p}});function Pe(t,e,i,n){const r=i?ue:de;return Ht({id:t,name:e,points:2,defaultStyle:{levels:c(r)},settings:et([U,K,X,...i?[]:[{path:"props.waveCount",label:"Wave limit",kind:"number",min:1,max:12,step:1,group:"behavior"}],...n?[{path:"props.mach",label:"Mach number",kind:"number",min:1.01,max:20,step:.5,group:"behavior"}]:[]])},t=>{if(!ge(t))return{paths:[]};const[e,o]=t.pts,s=bt(e,o),l=Math.hypot(o.x-e.x,o.y-e.y)/2;if(!Number.isFinite(l)||l<.5)return be(t);const a=(o.x-e.x)/(2*l),c=(o.y-e.y)/(2*l),h=n?_t(t.drawing,"mach",2,1.01,20):1,d={x:s.x-h*l*a,y:s.y-h*l*c},u=kt(t.drawing,r).filter(t=>t.ratio>0).slice().sort((t,e)=>t.ratio-e.ratio);i||u.splice(Math.round(_t(t.drawing,"waveCount",6,1,12)));const p=[],f=[];let x=0;for(const e of u){const i=l*e.ratio,n={x:d.x+h*i*a,y:d.y+h*i*c};Number.isFinite(i)&&wt(n)&&(x=Math.max(x,i),p.push(ye(n,i,i,0,re,e.color)),f.push(...me(t,e,{x:n.x+a*i+4,y:n.y+c*i})))}if(0===x)return{paths:[]};const y=h*x+2*l;if(n){const t=1/h,e=Math.sqrt(1-t*t);for(const i of[-1,1])p.push(xe(d,{x:d.x+y*(a*e-i*c*t),y:d.y+y*(c*e+i*a*t)}))}else p.push(xe({x:d.x-c*y,y:d.y+a*y},{x:d.x+c*y,y:d.y-a*y}));return{paths:p,labels:f}})}const Te=[ve,we("fib-circles","Fib Circles","circle"),we("fib-speed-resistance-arcs","Fib Speed Resistance Arcs","arc"),we("fib-wedge","Fib Wedge","wedge"),Me,_e,Se,Pe("sonic","Sonic",!1,!1),Pe("supersonic","Supersonic",!1,!0),Pe("golden-sonic","Golden Sonic",!0,!1),Pe("golden-supersonic","Golden Supersonic",!0,!0)],Le="#16a34a",Fe="#dc2626",Ie={min:1.272,max:1.414},Ae={min:.74,max:.83},Ne={gartley:{ab:{min:.55,max:.68},bc:{min:.382,max:.886},cd:{min:1.13,max:1.618},ad:{min:.74,max:.83}},bat:{ab:{min:.382,max:.5},bc:{min:.382,max:.886},cd:{min:1.618,max:2.618},ad:{min:.84,max:.92}},butterfly:{ab:{min:.74,max:.83},bc:{min:.382,max:.886},cd:{min:1.618,max:2.618},ad:{min:1.272,max:1.618}},crab:{ab:{min:.382,max:.618},bc:{min:.382,max:.886},cd:{min:2.618,max:3.618},ad:{min:1.55,max:1.69}},shark:{ab:{min:.382,max:.618},bc:{min:1.13,max:1.618},cd:{min:1.618,max:2.24},ad:{min:.886,max:1.13}}},Ce=et([U,Y,X]),ze=et([U,J,Y,X]),Oe=(t,e,i)=>({points:St(t,e,i.rc)});function De(t,e){return Number.isFinite(t)&&Number.isFinite(e)&&Math.abs(e)>=1e-9?Math.abs(t)/Math.abs(e):null}function $e(t,e,i){const n=t[e]?.price,r=t[i]?.price;return Number.isFinite(n)&&Number.isFinite(r)?Math.abs(r-n):NaN}function He(t){return null!==t.value&&void 0!==t.band&&t.value>=t.band.min&&t.value<=t.band.max}function We(t,e,i,n){const r=e.x-t.x,o=e.y-t.y,s=n.x-i.x,l=n.y-i.y,a=s*o-l*r;if(!Number.isFinite(a)||Math.abs(a)<1e-9)return null;const c=((i.y-t.y)*r-(i.x-t.x)*o)/a;return!Number.isFinite(c)||c<0||c>1?null:{x:i.x+c*s,y:i.y+c*l}}function Be(t,e){const i=e.pts.slice(0,t.labels.length),n=e.drawing.points.slice(0,t.labels.length);if(i.length<2)return{paths:[]};const r=[];if(!0===t.fill)for(const t of[[0,1,2],[2,3,4]]){const n=t.map(t=>i[t]);n.every(t=>void 0!==t&&wt(t))&&r.push({points:Tt(n,e.rc),closed:!0,fill:!0,stroke:!1})}for(let t=1;t<i.length;t++)r.push(Oe(i[t-1],i[t],e));if(void 0!==t.neckline&&i.length>=t.labels.length){const[n,o]=t.neckline,s=i[n],l=i[o],a=We(s,l,i[0],i[1])??s,c=i.length-1,h=We(s,l,i[c-1],i[c])??l;r.push(...function(t,e,i){const n=St(t,e,i.rc);if(n.length<2)return[];const[r,o]=n,s=o.x-r.x,l=o.y-r.y,a=Math.hypot(s,l);if(!(a>0&&Number.isFinite(a)))return[{points:n}];const c=[];for(let t=0;t<a;t+=12){const e=t/a,i=Math.min(t+7,a)/a;c.push({points:[{x:r.x+s*e,y:r.y+l*e},{x:r.x+s*i,y:r.y+l*i}]})}return c}(a,h,e))}if(!1===e.drawing.style.showLabels)return{paths:r};const o=function(t,e){return e.flatMap((i,n)=>{const r=t.labels[n];if(!r||!wt(i))return[];const o=e[n-1]?.y??i.y,s=e[n+1]?.y??i.y,l=i.y<=(o+s)/2;return[{at:{x:i.x+4,y:i.y+(l?-6:14)},text:r}]})}(t,i),s=void 0!==t.harmonic?function(t,e){const i=$e(t,0,1),n=$e(t,1,2);if("cypher"===e){const e=$e(t,0,3);return[{name:"AB/XA",value:De(n,i),band:{min:.382,max:.618},anchor:2},{name:"XC/XA",value:De(e,i),band:Ie,anchor:3},{name:"CD/XC",value:De($e(t,3,4),e),band:Ae,anchor:4}]}const r=$e(t,2,3);return[{name:"AB/XA",value:De(n,i),band:e.ab,anchor:2},{name:"BC/AB",value:De(r,n),band:e.bc,anchor:3},{name:"CD/BC",value:De($e(t,3,4),r),band:e.cd,anchor:4},{name:"AD/XA",value:De($e(t,1,4),i),band:e.ad,anchor:4}]}(n,t.harmonic):void 0!==t.ratios?function(t,e){if("xabcd"===e){const e=$e(t,0,1),i=$e(t,1,2),n=$e(t,2,3);return[{name:"AB/XA",value:De(i,e),anchor:2},{name:"BC/AB",value:De(n,i),anchor:3},{name:"CD/BC",value:De($e(t,3,4),n),anchor:4}]}const i=$e(t,0,1),n=$e(t,1,2);return[{name:"BC/AB",value:De(n,i),anchor:2},{name:"CD/BC",value:De($e(t,2,3),n),anchor:3}]}(n,t.ratios):[];if(o.push(...function(t,e){const i=[];for(let n=0;n<t.length;n++){const r=t[n];if(null===r.value||!Number.isFinite(r.value))continue;const o=e[r.anchor],s=e[Math.max(0,r.anchor-1)];if(!(o&&s&&wt(o)&&wt(s)))continue;const l=void 0!==r.band,a=l&&He(r),c=l?a?" OK":" OUT":"",h=bt(s,o);i.push({at:{x:h.x+6,y:h.y+(n===t.length-1&&r.anchor===t[n-1]?.anchor?14:-3)},text:`${r.name} ${r.value.toFixed(3)}${c}`,color:l?a?Le:Fe:void 0})}return i}(s,i)),void 0!==t.harmonic&&i.length>=t.labels.length){const e=function(t,e){if(t.length<e)return!1;let i=t[1].price-t[0].price;if(!Number.isFinite(i)||0===i)return!1;for(let n=2;n<e;n++){const e=t[n].price-t[n-1].price;if(!Number.isFinite(e)||0===e||Math.sign(e)===Math.sign(i))return!1;i=e}return!0}(n,t.labels.length),r=e&&s.length>0&&s.every(He),l=i[t.labels.length-1];l&&wt(l)&&o.push({at:{x:l.x+4,y:l.y+30},text:`${t.name}: ${e?r?"valid":"outside range":"invalid sequence"}`,color:r?Le:Fe})}return{paths:r,labels:o}}function Ve(t){return Ht({id:t.id,name:t.name,points:t.labels.length,defaultStyle:!0===t.fill?{fill:!0,fillOpacity:.12,showLabels:!0}:{showLabels:!0},settings:!0===t.fill?ze:Ce},e=>Be(t,e))}const Re=["X","A","B","C","D"],je=[Ve({id:"xabcd-pattern",name:"XABCD Pattern",labels:Re,fill:!0,ratios:"xabcd"}),Ve({id:"abcd-pattern",name:"ABCD Pattern",labels:["A","B","C","D"],ratios:"abcd"}),Ve({id:"elliott-impulse",name:"Elliott Impulse",labels:["1","2","3","4","5"]}),Ve({id:"elliott-correction",name:"Elliott Correction",labels:["A","B","C"]}),Ve({id:"head-shoulders",name:"Head and Shoulders",labels:["","LS","","H","","RS",""],neckline:[2,4]}),Ve({id:"gartley",name:"Gartley",labels:Re,fill:!0,harmonic:Ne.gartley}),Ve({id:"bat",name:"Bat",labels:Re,fill:!0,harmonic:Ne.bat}),Ve({id:"butterfly",name:"Butterfly",labels:Re,fill:!0,harmonic:Ne.butterfly}),Ve({id:"crab",name:"Crab",labels:Re,fill:!0,harmonic:Ne.crab}),Ve({id:"shark",name:"Shark",labels:Re,fill:!0,harmonic:Ne.shark}),Ve({id:"cypher",name:"Cypher",labels:Re,fill:!0,harmonic:"cypher"})];function Ee(t,e,i,n){return"number"==typeof t&&Number.isFinite(t)?Math.max(i,Math.min(n,t)):e}function Ue(t,e,i=!1){let n=0,r=t.length;for(;n<r;){const o=n+r>>>1;(i?t[o].time<=e:t[o].time<e)?n=o+1:r=o}return n}function Je(t,e,i,n,r){return 0===t?"empty":e?r||i>0||n>0?"partial":"ready":i>0?"missing-volume":n>0?"invalid-data":"zero-volume"}function qe(t,e){switch(e){case"close":return t.close;case"hl2":return t.low/2+t.high/2;case"ohlc4":return t.open/4+t.high/4+t.low/4+t.close/4;default:return t.high/3+t.low/3+t.close/3}}function Ye(t,e,i={}){const n={status:"empty",historyPartial:!1,points:[],missingVolumeBars:0,invalidPriceBars:0};if(!Number.isFinite(e))return n;n.historyPartial=t.length>0&&e<t[0].time;const r=Ue(t,e);let o=0,s=0,l=0,a=0,c=0,h=!1;for(let e=r;e<t.length;e++){const r=t[e],d=r.volume;if(void 0===d||!Number.isFinite(d)||d<0){n.missingVolumeBars++,h=!0;continue}const u=qe(r,i.source??"hlc3");if(Number.isFinite(u)){if(d>0){if(0===o&&(l=u),d>s){const t=s/d;o*=t,c*=t,s=d}const t=d/s,e=o+t,i=u-l-a;a+=i*(t/e),c+=t*i*(u-l-a),o=e}if(o>0){const t=l+a,e=Math.sqrt(Math.max(0,c/o));if(!Number.isFinite(t)||!Number.isFinite(e))return{...n,status:"invalid-data",points:[]};n.points.push({time:r.time,value:t,deviation:e,breakBefore:h}),h=!1}}else n.invalidPriceBars++,h=!0}return n.status=Je(t.length-r,o>0,n.missingVolumeBars,n.invalidPriceBars,n.historyPartial),n}function Ke(t,e,i,n={}){const r={status:"empty",historyPartial:!1,rows:[],totalVolume:0,poc:null,valueAreaLow:null,valueAreaHigh:null,missingVolumeBars:0,invalidPriceBars:0};if(!Number.isFinite(e)||!Number.isFinite(i))return r;r.historyPartial=t.length>0&&Math.min(e,i)<t[0].time;const o=Ue(t,Math.min(e,i)),s=Ue(t,Math.max(e,i),!0);let l=1/0,a=-1/0,c=0;for(let e=o;e<s;e++){const i=t[e],n=i.volume;if(void 0===n||!Number.isFinite(n)||n<0){r.missingVolumeBars++;continue}const o=i.low,s=i.high;if(!Number.isFinite(o)||!Number.isFinite(s)||s<o){r.invalidPriceBars++;continue}l=Math.min(l,o),a=Math.max(a,s);const h=n-c,d=r.totalVolume+h;c=d-r.totalVolume-h,r.totalVolume=d}if(r.status=Je(s-o,r.totalVolume>0,r.missingVolumeBars,r.invalidPriceBars,r.historyPartial),!Number.isFinite(r.totalVolume)||r.totalVolume>0&&!Number.isFinite(a-l))return{...r,status:"invalid-data",totalVolume:0};if(r.totalVolume<=0)return r;const h=Math.round(Ee(n.rows,48,4,200)),d=[l];for(let t=1;t<h;t++){const e=l+t/h*(a-l);e>d[d.length-1]&&e<a&&d.push(e)}d.push(a);const u=d.length-1,p=(a-l)/u,f=t=>d[t],x=new Float64Array(u),y=new Float64Array(u+1),g=t=>{let e=0,i=u;for(;e<i;){const n=e+i+1>>>1;d[n]<=t?e=n:i=n-1}return Math.min(u-1,e)};for(let e=o;e<s;e++){const i=t[e],n=i.volume,o=i.low,s=i.high;if(void 0===n||!Number.isFinite(n)||n<=0||!Number.isFinite(o)||!Number.isFinite(s)||s<o)continue;const l=n/r.totalVolume,a=g(o),c=g(s);if(a===c){x[a]+=l;continue}const h=s-o;if(x[a]+=l*((f(a+1)-o)/h),x[c]+=l*((s-f(c))/h),c>a+1){const t=l*(p/h);y[a+1]+=t,y[c]-=t}}let m=0,b=0;for(let t=0;t<u;t++){m+=y[t];const e=1===u?0:m*((f(t+1)-f(t))/p);x[t]=Math.max(0,Math.min(1,x[t]+e))*r.totalVolume,x[t]>x[b]&&(b=t),r.rows.push({low:f(t),high:f(t+1),volume:x[t],valueArea:!1})}let v=b,w=b,M=x[b];const _=r.totalVolume*(Ee(n.valueArea,70,1,100)/100);for(;M<_&&(v>0||w<u-1);){M+=(v>0?x[v-1]:-1)>=(w<u-1?x[w+1]:-1)?x[--v]:x[++w]}for(let t=v;t<=w;t++)r.rows[t].valueArea=!0;return r.poc=f(b)/2+f(b+1)/2,r.valueAreaLow=f(v),r.valueAreaHigh=f(w+1),r}function Xe(t){const e=t.props?.source;return"close"===e||"hl2"===e||"ohlc4"===e?e:"hlc3"}function Ge(t,e,i){return{x:i.rc.timeScale.indexToX(i.rc.dataLayer.timeToIndexFloat(t)),y:i.rc.priceScale.priceToY(e)}}function Ze(t){const e=t.historyPartial?" (partial history)":"";switch(t.status){case"partial":return e+(t.missingVolumeBars>0||t.invalidPriceBars>0?" (partial volume/data)":"");case"missing-volume":return": volume unavailable"+e;case"zero-volume":return": zero volume"+e;case"invalid-data":return": data unavailable"+e;case"empty":return": no bars in range"+e;default:return""}}function Qe(t,e){for(const i of e.drawing.points){const n=Ge(i.time,i.price,e);for(const[i,r]of[[{x:n.x-4,y:n.y},{x:n.x+4,y:n.y}],[{x:n.x,y:n.y-4},{x:n.x,y:n.y+4}]]){const n=St(i,r,e.rc);n.length>0&&t.lines.push(n)}}}function ti(t){const e={lines:[],bands:[],rects:[],label:"",labelAt:{x:0,y:0},status:"empty"},i=t.drawing.points[0];if(!i)return e;const n=Ye(t.rc.bars?.()??[],i.time,{source:Xe(t.drawing)});e.status=n.status;const r=!1!==t.drawing.props?.showBands,o=Ee(t.drawing.props?.bandMultiplier,1,0,5),s=n.points[n.points.length-1];e.labelAt=s?Ge(s.time,s.value,t):Ge(i.time,i.price,t),e.label=`AVWAP${s?` ${t.rc.priceScale.format(s.value)}`:""}${Ze(n)}`;for(const i of r&&o>0?[0,-o,o]:[0]){let r;for(const o of n.points){const n=Ge(o.time,o.value+o.deviation*i,t);o.breakBefore&&(r=void 0);const s=St(r||{x:n.x-2,y:n.y},n,t.rc);s.length>0&&(0===i?e.lines:e.bands).push(s),r=n}}return s||Qe(e,t),e}function ei(t){const e={lines:[],bands:[],rects:[],label:"",labelAt:{x:0,y:0},status:"empty"},[i,n]=t.drawing.points;if(!i||!n)return e;const r=Ke(t.rc.bars?.()??[],i.time,n.time,{rows:Ee(t.drawing.props?.rows,48,4,200),valueArea:Ee(t.drawing.props?.valueArea,70,1,100)});e.status=r.status;const o=Ge(i.time,i.price,t),s=Ge(n.time,n.price,t),l=Math.min(o.x,s.x),a=Math.max(o.x,s.x),c=Math.max(0,...r.rows.map(t=>t.volume)),h=Math.max(12,a-l)*Ee(t.drawing.props?.width,35,5,100)/100,d="right"===t.drawing.props?.side,u=!1!==t.drawing.props?.showValueArea;e.label=`Volume profile (estimated)${Ze(r)}`,e.labelAt={x:l,y:Math.min(o.y,s.y)};for(const i of r.rows){const n=t.rc.priceScale.priceToY(i.low),r=t.rc.priceScale.priceToY(i.high);if(![n,r].every(Number.isFinite)||c<=0||i.volume<=0)continue;const o=h*i.volume/c,s=d?a-o:l,p=d?a:l+o,f=Math.max(0,Math.min(n,r)),x=Math.min(t.rc.plotHeight,Math.max(n,r)+(n===r?1:0)),y={left:Math.max(0,s),right:Math.min(t.rc.plotWidth,p),top:f,bottom:x,area:u&&i.valueArea};y.left<y.right&&y.top<y.bottom&&e.rects.push(y)}const p=(e,i)=>{if(null===e)return;const n=t.rc.priceScale.priceToY(e),r=St({x:l,y:n},{x:a===l?a+12:a,y:n},t.rc);r.length>0&&i.push(r)};return!1!==t.drawing.props?.showPoc&&p(r.poc,e.lines),u&&(p(r.valueAreaLow,e.bands),p(r.valueAreaHigh,e.bands)),null!==r.valueAreaHigh&&(e.labelAt.y=t.rc.priceScale.priceToY(r.rows[r.rows.length-1].high)),0===r.rows.length&&Qe(e,t),e}function ii(t,e){const{ctx:i,rc:n,drawing:r,style:o}=t,s=n.dpr;if(!(n.plotWidth>0&&n.plotHeight>0))return;const l=e({drawing:r,rc:n,pts:[]});i.save(),i.beginPath(),i.rect(0,0,n.plotWidth*s,n.plotHeight*s),i.clip(),i.strokeStyle=o.color,i.fillStyle=o.color,i.lineWidth=Math.max(1,o.lineWidth*s);const a="dashed"===o.lineStyle?[6*s,4*s]:"dotted"===o.lineStyle?[2*s,3*s]:[];i.setLineDash(a);for(const t of l.rects)i.save(),i.globalAlpha*=Ee(o.fillOpacity,.4,0,1)*(t.area?1:.45),i.fillRect(t.left*s,t.top*s,(t.right-t.left)*s,(t.bottom-t.top)*s),i.restore();const c=t=>{if(0!==t.length){i.beginPath();for(const e of t)i.moveTo(e[0].x*s,e[0].y*s),i.lineTo(e[1].x*s,e[1].y*s);i.stroke()}};if(c(l.lines),i.save(),i.globalAlpha*=.6,c(l.bands),i.restore(),(!1!==o.showLabels||"ready"!==l.status)&&l.label&&Number.isFinite(l.labelAt.x)&&Number.isFinite(l.labelAt.y)){const t=r.text,e=Ee(t?.fontSize,11,6,96);i.fillStyle=t?.color??o.color,i.font=`${t?.italic?"italic ":""}${t?.bold?"bold ":""}${e*s}px ${t?.fontFamily??"sans-serif"}`,i.textAlign="left",i.textBaseline="bottom";const a=i.measureText(l.label).width/s,c=Math.max(4,Math.min(n.plotWidth-a-4,l.labelAt.x+4)),h=Math.max(e+3,Math.min(n.plotHeight-3,l.labelAt.y-5));i.fillText(l.label,c*s,h*s)}i.restore()}function ni(t,e,i,n){if(t<0||e<0||t>i.rc.plotWidth||e>i.rc.plotHeight)return null;const r=n(i);let o=1/0;for(const i of[...r.lines,...r.bands])o=Math.min(o,k(t,e,i[0],i[1]));if(Ee(i.drawing.style.fillOpacity,.4,0,1)>0)for(const i of r.rects)o=Math.min(o,A(t,e,{x:i.left,y:i.top},{x:i.right,y:i.bottom},!0));return Number.isFinite(o)?o:null}const ri={id:"anchored-vwap",name:"Anchored VWAP",points:1,defaultStyle:{showLabels:!0,lineWidth:2},settings:et([U,Y,X,{path:"props.source",label:"Price source",kind:"select",group:"behavior",defaultValue:"hlc3",options:[{value:"hlc3",label:"HLC3"},{value:"close",label:"Close"},{value:"hl2",label:"HL2"},{value:"ohlc4",label:"OHLC4"}]},{path:"props.showBands",label:"Deviation bands",kind:"boolean",group:"behavior",defaultValue:!0},{path:"props.bandMultiplier",label:"Band multiplier",kind:"number",min:0,max:5,step:.25,group:"behavior",defaultValue:1}]),draw:t=>ii(t,ti),distance:(t,e,i)=>ni(t,e,i,ti)},oi={id:"fixed-range-volume-profile",name:"Fixed Range Volume Profile",points:2,defaultStyle:{showLabels:!0,fillOpacity:.4},settings:et([U,Y,X,{path:"style.fillOpacity",label:"Histogram opacity",kind:"opacity",min:0,max:1,step:.05,group:"fill",defaultValue:.4},{path:"props.rows",label:"Price rows",kind:"number",min:4,max:200,step:1,group:"behavior",defaultValue:48},{path:"props.valueArea",label:"Value area (%)",kind:"number",min:1,max:100,step:1,group:"behavior",defaultValue:70},{path:"props.width",label:"Range width (%)",kind:"number",min:5,max:100,step:1,group:"behavior",defaultValue:35},{path:"props.side",label:"Histogram side",kind:"select",group:"behavior",defaultValue:"left",options:[{value:"left",label:"Left"},{value:"right",label:"Right"}]},{path:"props.showPoc",label:"Point of control",kind:"boolean",group:"behavior",defaultValue:!0},{path:"props.showValueArea",label:"Value area levels",kind:"boolean",group:"behavior",defaultValue:!0}]),draw:t=>ii(t,ei),distance:(t,e,i)=>ni(t,e,i,ei)},si=new Map;function li(t){si.set(t.id,t)}function ai(t){const e=si.get(t);if(void 0===e)throw new Error(`openalgo-charts: unknown drawing tool "${t}"`);return e}function ci(t){return si.has(t)}function hi(){return Array.from(si.values())}function di(t){const e=t.split("+").map(t=>t.trim().toLowerCase());return{key:e[e.length-1]??"",alt:e.includes("alt"),ctrl:e.includes("ctrl")||e.includes("control"),shift:e.includes("shift")}}function ui(t){const e=(t.key??"").toLowerCase();if(""===e)return null;const i=!0===t.altKey,n=!0===t.ctrlKey||!0===t.metaKey,r=!0===t.shiftKey;if(!i&&!n)return null;for(const t of si.values()){if(void 0===t.shortcut)continue;const o=di(t.shortcut);if(o.key===e&&o.alt===i&&o.ctrl===n&&o.shift===r)return t.id}return null}function pi(){const t={};for(const e of si.values())void 0!==e.shortcut&&(t[e.id]=e.shortcut);return t}const fi=et([U]);function xi(t){return si.get(t)?.settings??fi}const yi={path:"style.fillOpacity",label:"Opacity",kind:"opacity",min:0,max:1,step:.01,group:"fill"},gi={...Y,label:"Show price"},mi={path:"style.showStats",label:"Show stats",kind:"boolean",group:"behavior"},bi={path:"style.pressure",label:"Pen pressure",kind:"boolean",group:"line"},vi=[{path:"style.accountSize",label:"Account size",kind:"number",min:0,step:1e3,group:"behavior"},{path:"style.risk",label:"Risk %",kind:"number",min:0,max:100,step:.1,group:"behavior"},{path:"props.showHeader",label:"Show direction and ratio",kind:"boolean",group:"behavior"},{path:"props.showLossSize",label:"Show loss and size",kind:"boolean",group:"behavior"},{path:"props.showTargetLabel",label:"Show target label",kind:"boolean",group:"behavior"},{path:"props.showStopLabel",label:"Show stop label",kind:"boolean",group:"behavior"},{path:"props.showPrices",label:"Show level prices",kind:"boolean",group:"behavior"},{path:"props.profitColor",label:"Profit zone",kind:"color",group:"fill"},{path:"props.lossColor",label:"Loss zone",kind:"color",group:"fill"}];function wi(t){const{ctx:e,rc:i,style:n}=t,r=i.dpr;e.strokeStyle=n.color,e.lineWidth=Math.max(1,Math.round(n.lineWidth*r)),e.lineJoin="round",e.lineCap="round",e.setLineDash("dashed"===n.lineStyle?[6*r,4*r]:"dotted"===n.lineStyle?[1*r,3*r]:[])}function Mi(t){return t.style.fillColor??t.style.color}function _i(t,e){if(!0!==t.style.fill)return;const{ctx:i}=t;i.save(),i.globalAlpha=t.style.fillOpacity??.12,i.fillStyle=Mi(t),e(),i.restore()}const ki={value:""};function Si(t){return t.text??ki}function Pi(t,e,i){const n=i??(!0===t.bold?"700":"");return`${!0===t.italic?"italic ":""}${""===n?"":`${n} `}${e}px ${void 0===t.fontFamily||""===t.fontFamily?"ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif":t.fontFamily}`}function Ti(t,e){const i=t.text?.value;return void 0===i||""===i?e:i}function Li(t,e,i,n,r,o={}){const{ctx:s,rc:l,style:a}=t,c=Si(t.drawing),h=(c.fontSize??11)*l.dpr;s.save(),s.setLineDash([]),s.font=Pi(c,h),s.textBaseline="middle",s.textAlign="left";const d=mt(s,e),u=!0===o.center?i-d/2:i;s.globalAlpha=.85,s.fillStyle=l.theme.background,s.fillRect(u-2*l.dpr,n-.75*h,d+4*l.dpr,1.5*h),void 0!==o.tint&&(s.globalAlpha=.22,s.fillStyle=o.tint,s.fillRect(u-2*l.dpr,n-.75*h,d+4*l.dpr,1.5*h)),s.globalAlpha=1,s.fillStyle=c.color??r??a.color,s.fillText(e,u,n),s.restore()}function Fi(t,e,i,n,r,o={}){const{ctx:s,rc:l}=t,a=l.dpr,c=Si(t.drawing),h=(c.fontSize??11)*a;s.save(),s.setLineDash([]),s.globalAlpha=1,s.font=Pi(c,h),s.textBaseline="middle",s.textAlign="left";const d=5*a,u=3*a,p=1.35*h;let f=0;for(const t of e)f=Math.max(f,mt(s,t));const x=f+2*d,y=p*e.length+2*u,g=o.align??"left",m="center"===g?i-x/2:"right"===g?i-x:i,b=o.place??"above",v="above"===b?n-y-4*a:"below"===b?n+4*a:n-y/2;s.beginPath(),D(s,m,v,x,y,3*a),s.fillStyle=r,s.fill(),s.fillStyle=c.color??O(r);for(let t=0;t<e.length;t++)s.fillText(e[t],m+d,v+u+p*(t+.5));return s.restore(),y}function Ii(t,e=0){const[i,n]=Math.abs(t).toFixed(e).split(".");return(t<0?"-":"")+i.replace(/\B(?=(\d{3})+(?!\d))/g,",")+(void 0===n?"":"."+n)}function Ai(t){const e=new Date(1e3*t),i=t=>String(t).padStart(2,"0");return`${e.getUTCFullYear()}-${i(e.getUTCMonth()+1)}-${i(e.getUTCDate())}`}function Ni(t){const e=Math.max(0,Math.round(Math.abs(t))),i=Math.floor(e/86400),n=Math.floor(e%86400/3600),r=Math.floor(e%3600/60);return i>0?`${i}d ${n}h`:n>0?`${n}h ${r}m`:`${r}m`}function Ci(t,e,i){const n=Math.max(8,5*t.style.lineWidth)*t.rc.dpr,r=Math.atan2(i.y-e.y,i.x-e.x);t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(i.x-n*Math.cos(r-.4),i.y-n*Math.sin(r-.4)),t.ctx.lineTo(i.x-n*Math.cos(r+.4),i.y-n*Math.sin(r+.4)),t.ctx.closePath(),t.ctx.fillStyle=t.style.color,t.ctx.fill()}const zi="#26a69a",Oi="#ef5350";function Di(t){if(!0!==t.style.showStats)return;const[e,i]=t.pts,[n,r]=t.drawing.points,o=r.price-n.price,s=0!==n.price?o/n.price*100:0,l=o>=0,a=l?"+":"",c=t.rc.dataLayer.timeToIndexFloat(n.time),h=t.rc.dataLayer.timeToIndexFloat(r.time),d=Math.abs(Math.round(h-c)),u=180*Math.atan2(e.y-i.y,i.x-e.x)/Math.PI,p=`${a}${t.formatPrice(o)} (${a}${s.toFixed(2)}%) ${Ii(d)} bars ${u.toFixed(1)} deg`;Li(t,p,(e.x+i.x)/2,(e.y+i.y)/2-10*t.rc.dpr,t.style.color,{tint:l?zi:Oi,center:!0})}function $i(t,e,i){const{ctx:n,style:r}=t,o=t.pts;if(n.save(),n.globalAlpha=i,n.lineCap="round",n.lineJoin="round",!0!==r.pressure)return n.strokeStyle=r.color,n.lineWidth=e,n.beginPath(),xt(n,o),n.stroke(),void n.restore();const s=o.length,l=[],a=[];for(let i=0;i<s;i++){const n=o[0===i?0:i-1],r=o[i===s-1?s-1:i+1],c=Math.hypot(r.x-n.x,r.y-n.y)||1,h=-(r.y-n.y)/c,d=(r.x-n.x)/c,u=t.drawing.points[i]?.pressure,p=yt(e,u??.5)/2;l.push({x:o[i].x+h*p,y:o[i].y+d*p}),a.push({x:o[i].x-h*p,y:o[i].y-d*p})}a.reverse(),n.fillStyle=r.color,n.beginPath(),xt(n,l.concat(a)),n.closePath(),n.fill(),n.restore()}function Hi(t,e){return(t??e).filter(t=>!1!==t.enabled&&Number.isFinite(t.ratio))}function Wi(t){return t.color??n(t.ratio)}function Bi(t){return t.label??s(t.ratio)}function Vi(t,e,i,n){return{id:t,name:e,points:2,angleLock:!0,alertValue:g(i,n),defaultStyle:{extendLeft:i,extendRight:n},settings:et([U,q,mi]),draw:t=>{const[e,r]=C(t.pts[0],t.pts[1],t.rc.plotWidth*t.rc.dpr,t.style.extendLeft??i,t.style.extendRight??n);wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(r.x,r.y),t.ctx.stroke(),t.ctx.setLineDash([]),Di(t)},distance:(t,e,r)=>{const o=r.drawing.style.extendLeft??i,s=r.drawing.style.extendRight??n;if(o&&s)return S(t,e,r.pts[0],r.pts[1]);const[l,a]=C(r.pts[0],r.pts[1],r.rc.plotWidth,o,s);return k(t,e,l,a)}}}const Ri={...Vi("trend-line","Trend Line",!1,!1),shortcut:"Alt+T"},ji=Vi("ray","Ray",!1,!0),Ei=Vi("extended-line","Extended Line",!0,!0),Ui={id:"arrow",name:"Arrow",points:2,angleLock:!0,settings:et([U,mi]),draw:t=>{const[e,i]=t.pts;wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(i.x,i.y),t.ctx.stroke(),Ci(t,e,i),t.ctx.setLineDash([]),Di(t)},distance:(t,e,i)=>k(t,e,i.pts[0],i.pts[1])},Ji={id:"horizontal-line",name:"Horizontal Line",points:1,shortcut:"Alt+H",alertValue:m(),defaultStyle:{showLabels:!0},settings:et([U,gi,X]),draw:t=>{const e=Math.round(t.pts[0].y)+.5;wi(t),t.ctx.beginPath(),t.ctx.moveTo(0,e),t.ctx.lineTo(t.rc.plotWidth*t.rc.dpr,e),t.ctx.stroke(),t.ctx.setLineDash([]),!1!==t.style.showLabels&&Li(t,t.formatPrice(t.drawing.points[0].price),4*t.rc.dpr,e-8*t.rc.dpr)},distance:(t,e,i)=>P(e,i.pts[0].y)},qi={id:"horizontal-ray",name:"Horizontal Ray",points:1,shortcut:"Alt+J",alertValue:m(!0),defaultStyle:{showLabels:!0},settings:et([U,gi,X]),draw:t=>{const e=Math.round(t.pts[0].y)+.5;wi(t),t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,e),t.ctx.lineTo(t.rc.plotWidth*t.rc.dpr,e),t.ctx.stroke(),t.ctx.setLineDash([]),!1!==t.style.showLabels&&Li(t,t.formatPrice(t.drawing.points[0].price),t.pts[0].x+4*t.rc.dpr,e-8*t.rc.dpr)},distance:(t,e,i)=>t<i.pts[0].x?null:P(e,i.pts[0].y)},Yi={id:"vertical-line",name:"Vertical Line",points:1,shortcut:"Alt+V",settings:fi,draw:t=>{const e=Math.round(t.pts[0].x)+.5;wi(t),t.ctx.beginPath(),t.ctx.moveTo(e,0),t.ctx.lineTo(e,t.rc.plotHeight*t.rc.dpr),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>T(t,i.pts[0].x)},Ki={id:"cross-line",name:"Cross Line",points:1,shortcut:"Alt+C",settings:fi,draw:t=>{const e=Math.round(t.pts[0].x)+.5,i=Math.round(t.pts[0].y)+.5;wi(t),t.ctx.beginPath(),t.ctx.moveTo(e,0),t.ctx.lineTo(e,t.rc.plotHeight*t.rc.dpr),t.ctx.moveTo(0,i),t.ctx.lineTo(t.rc.plotWidth*t.rc.dpr,i),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>Math.min(T(t,i.pts[0].x),P(e,i.pts[0].y))},Xi=et([U,J,Q]),Gi={id:"rectangle",name:"Rectangle",points:2,defaultStyle:{fill:!0},settings:Xi,draw:t=>{const e=F(t.pts[0],t.pts[1]);_i(t,()=>t.ctx.fillRect(e.x0,e.y0,e.x1-e.x0,e.y1-e.y0)),wi(t),t.ctx.strokeRect(e.x0,e.y0,e.x1-e.x0,e.y1-e.y0),t.ctx.setLineDash([]),xn(t,e)},distance:(t,e,i)=>A(t,e,i.pts[0],i.pts[1],!0===i.drawing.style.fill)},Zi={id:"ellipse",name:"Ellipse",points:2,defaultStyle:{fill:!0},settings:Xi,draw:t=>{const e=F(t.pts[0],t.pts[1]),i=(e.x0+e.x1)/2,n=(e.y0+e.y1)/2,r=Math.max(1,(e.x1-e.x0)/2),o=Math.max(1,(e.y1-e.y0)/2),s=()=>{t.ctx.beginPath(),t.ctx.ellipse(i,n,r,o,0,0,2*Math.PI)};_i(t,()=>{s(),t.ctx.fill()}),wi(t),s(),t.ctx.stroke(),t.ctx.setLineDash([]),xn(t,e,"center","middle")},distance:(t,e,i)=>N(t,e,i.pts[0],i.pts[1],!0===i.drawing.style.fill)},Qi={id:"parallel-channel",name:"Parallel Channel",points:3,alertValue:v("parallel"),alertLevels:b,defaultStyle:{fill:!0},settings:Xi,draw:t=>{const[e,i,n]=t.pts,r=n.y-(e.y+.5*(i.y-e.y)),o={x:e.x,y:e.y+r},s={x:i.x,y:i.y+r};_i(t,()=>{t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(i.x,i.y),t.ctx.lineTo(s.x,s.y),t.ctx.lineTo(o.x,o.y),t.ctx.closePath(),t.ctx.fill()}),wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(i.x,i.y),t.ctx.moveTo(o.x,o.y),t.ctx.lineTo(s.x,s.y),t.ctx.stroke(),t.ctx.setLineDash([]),xn(t,I([e,i,o,s]))},distance:(t,e,i)=>{const[n,r,o]=i.pts,s=o.y-(n.y+.5*(r.y-n.y)),l=k(t,e,n,r),a=k(t,e,{x:n.x,y:n.y+s},{x:r.x,y:r.y+s});return Math.min(l,a)}},tn=et([U,K,J,q,X]);function en(t,e,i){return{id:t,name:e,points:i,alertValue:_(i,h),alertLevels:M(h),defaultStyle:{showLabels:!0,levels:c(h),fill:!0,fillOpacity:.06},settings:tn,draw:t=>{const e=Hi(t.style.levels,h),n=t.drawing.points,r=t.rc.dpr,o=2===i?n[0].price:n[2].price,s=n[1].price-n[0].price,l=Math.min(t.pts[0].x,t.pts[i-1].x),a=Math.max(t.pts[0].x,t.pts[i-1].x),c=!0===t.style.extendLeft?0:l,d=!0===t.style.extendRight?t.rc.plotWidth*r:a;wi(t),t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,t.pts[0].y),t.ctx.lineTo(t.pts[1].x,t.pts[1].y),3===i&&t.ctx.lineTo(t.pts[2].x,t.pts[2].y),t.ctx.stroke();let u=null;for(const i of e){const e=o+s*i.ratio,n=Math.round(t.rc.priceScale.priceToY(e)*r)+.5,l=Wi(i);!0===t.style.fill&&null!==u&&(t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.06,t.ctx.fillStyle=t.style.fillColor??l,t.ctx.fillRect(c,Math.min(u,n),d-c,Math.abs(n-u)),t.ctx.restore()),u=n,t.ctx.strokeStyle=l,t.ctx.beginPath(),t.ctx.moveTo(c,n),t.ctx.lineTo(d,n),t.ctx.stroke(),!1!==t.style.showLabels&&Li(t,`${Bi(i)} ${t.formatPrice(e)}`,c+4*r,n-8*r,l)}t.ctx.setLineDash([])},distance:(t,e,n)=>{const r=Hi(n.drawing.style.levels,h),o=n.drawing.points,s=2===i?o[0].price:o[2].price,l=o[1].price-o[0].price,a=!0===n.drawing.style.extendLeft?0:Math.min(n.pts[0].x,n.pts[i-1].x),c=!0===n.drawing.style.extendRight?n.rc.plotWidth:Math.max(n.pts[0].x,n.pts[i-1].x);if(t<a-4||t>c+4)return null;let d=1/0;for(const t of r){const i=Math.abs(e-n.rc.priceScale.priceToY(s+l*t.ratio));i<d&&(d=i)}return d}}}const nn=en("fib-retracement","Fib Retracement",2),rn=en("fib-extension","Fib Extension",3),on=et([j,E,yi,Y,X]),sn={id:"measure",name:"Measure",points:2,defaultStyle:{fill:!0,showLabels:!0,fillOpacity:.14},settings:on,draw:t=>{const e=F(t.pts[0],t.pts[1]),i=t.rc.dpr,n=t.drawing.points,r=n[1].price-n[0].price,o=0!==n[0].price?r/n[0].price*100:0,s=r>=0,l=s?zi:Oi;t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.14,t.ctx.fillStyle=l,t.ctx.fillRect(e.x0,e.y0,e.x1-e.x0,e.y1-e.y0),t.ctx.restore(),wi(t),t.ctx.strokeStyle=l,t.ctx.strokeRect(e.x0,e.y0,e.x1-e.x0,e.y1-e.y0),t.ctx.setLineDash([]);const a=(e.x0+e.x1)/2,c=t.pts[0].y,h=t.pts[1].y;if(t.ctx.strokeStyle=l,t.ctx.beginPath(),t.ctx.moveTo(e.x0,c),t.ctx.lineTo(e.x1,c),t.ctx.stroke(),Ci(t,{x:e.x0,y:c},{x:e.x1,y:c}),t.ctx.beginPath(),t.ctx.moveTo(a,c),t.ctx.lineTo(a,h),t.ctx.stroke(),Ci(t,{x:a,y:c},{x:a,y:h}),!1===t.style.showLabels)return;const d=t.rc.dataLayer.timeToIndexFloat(n[0].time),u=t.rc.dataLayer.timeToIndexFloat(n[1].time),p=Math.abs(Math.round(u-d)),f=s?"+":"",x=[`${f}${t.formatPrice(r)} (${f}${o.toFixed(2)}%)`,`${Ii(p)} bars, ${Ni(n[1].time-n[0].time)}`],y=t.rc.bars?.();if(void 0!==y&&y.length>0){const t=Math.min(n[0].time,n[1].time),e=Math.max(n[0].time,n[1].time);let i=0,r=!1,o=0,s=y.length;for(;o<s;){const e=o+s>>>1;y[e].time<t?o=e+1:s=e}for(let t=o;t<y.length;t++){const n=y[t];if(n.time>e)break;void 0!==n.volume&&(i+=n.volume,r=!0)}r&&x.push(`Vol ${function(t){const e=Math.abs(t);return e>=1e9?`${(t/1e9).toFixed(2)}B`:e>=1e6?`${(t/1e6).toFixed(2)}M`:e>=1e3?`${(t/1e3).toFixed(2)}K`:Ii(t)}(i)}`)}Fi(t,x,a,Math.max(c,h)+6*i,l,{align:"center",place:"below"})},distance:(t,e,i)=>A(t,e,i.pts[0],i.pts[1],!0)};function ln(t){return!Number.isFinite(t)||t<=0?"0":t>=100?Ii(t):t>=1?Ii(t,2).replace(/\.?0+$/,""):t.toPrecision(3).replace(/\.?0+$/,"")}function an(t,e,i){return{id:t,name:e,points:2,defaultStyle:{showLabels:!0,fillOpacity:.13,accountSize:1e5,risk:1},settings:et([U,yi,Y,vi,X]),expand:(t,e)=>function(t,e,i){const n=t[0],r=t[1]??n,o=i?1:-1;if(void 0!==e.toPixel&&void 0!==e.fromPixel){const t=e.toPixel(n),i=e.toPixel(r);if(null!==t&&null!==i){const r=Math.abs(i.y-t.y)>=6,s=r?i.y:t.y-64*o*2,l=t.x+(r?Math.max(Math.abs(i.x-t.x),48):150),a=t.y-(s-t.y)/2,c=e.fromPixel({x:l,y:s}),h=e.fromPixel({x:l,y:a});if(null!==c&&null!==h)return[{...n},c,h]}}const s=r.price!==n.price,l=s?r.price-n.price:o*(.02*Math.abs(n.price)||2),a=Math.max(5,Math.round(.08*e.visibleBars)),c=Math.max(1,e.barSeconds)*a,h=s&&r.time>n.time?r.time:n.time+c;return[{...n},{time:h,price:n.price+l},{time:h,price:n.price-l/2}]}(t,e,i),constrain:(t,e)=>function(t,e){const i=t.map(t=>({...t}));if(i.length<3)return i;const[n,r,o]=i,s=Math.sign(r.price-n.price),l=Math.sign(o.price-n.price);return 0===s||0===l||s!==l||(1===e?o.price=2*n.price-o.price:r.price=2*n.price-r.price),i}(t,e),draw:t=>{const e=t.drawing.points;if(e.length<2||t.pts.length<2)return;const[n,r]=e,o=e.length>=3?e[2]:{time:r.time,price:n.price-(r.price-n.price)/2},s=t.rc.dpr,l=t.pts.map(t=>t.x),a=Math.min(...l),c=Math.max(...l),h=t.rc.priceScale.priceToY(n.price)*s,d=t.rc.priceScale.priceToY(r.price)*s,u=t.rc.priceScale.priceToY(o.price)*s,p=t.drawing.props??{},f="string"==typeof p.profitColor?p.profitColor:zi,x="string"==typeof p.lossColor?p.lossColor:Oi,y=(e,i,n)=>{t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.13,t.ctx.fillStyle=n,t.ctx.fillRect(a,Math.min(e,i),c-a,Math.abs(i-e)),t.ctx.restore()};y(h,d,f),y(h,u,x),wi(t);for(const[e,i]of[[d,f],[u,x],[h,t.style.color]]){const n=Math.round(e)+.5;t.ctx.strokeStyle=i,t.ctx.beginPath(),t.ctx.moveTo(a,n),t.ctx.lineTo(c,n),t.ctx.stroke()}if(t.ctx.setLineDash([]),!1===t.style.showLabels)return;const g=Math.abs(n.price-o.price),m=Math.abs(r.price-n.price),b=g>0?m/g:0,v=t=>(0!==n.price?t/n.price*100:0).toFixed(2),w=r.price===n.price?i?"Long":"Short":r.price>n.price?"Long":"Short",M=t.style.accountSize??0,_=M>0?M*(t.style.risk??0)/100:0,k=g>0?_/g:0,S=(a+c)/2,P=e=>!0===p.showPrices?` @ ${t.formatPrice(e)}`:"";let T=Math.min(h,d,u);!1!==p.showLossSize&&_>0&&(T-=Fi(t,[`Loss ${Ii(_,2)} Size ${ln(k)}`],S,T,t.rc.theme.background,{align:"center",place:"above"})+4*s),!1!==p.showHeader&&Fi(t,[`${w} R:R ${b.toFixed(2)}`],S,T,t.rc.theme.background,{align:"center",place:"above"}),!1!==p.showTargetLabel&&m>0&&Fi(t,[`Target +${v(m)}%${P(r.price)}`],S,(h+d)/2,f,{align:"center",place:"middle"}),!1!==p.showStopLabel&&g>0&&Fi(t,[`Stop -${v(g)}%${P(o.price)}`],S,(h+u)/2,x,{align:"center",place:"middle"})},distance:(t,e,i)=>{const n=i.drawing.points;if(i.pts.length<2||n.length<2)return null;const r=i.pts.map(t=>t.x),o=Math.min(...r),s=Math.max(...r);if(t<o-4||t>s+4)return null;const[l,a]=n,c=n.length>=3?n[2].price:l.price-(a.price-l.price)/2,h=[l.price,a.price,c].map(t=>i.rc.priceScale.priceToY(t)),d=Math.min(...h),u=Math.max(...h);return e>=d&&e<=u?0:Math.min(Math.abs(e-d),Math.abs(e-u))}}}const cn=an("long-position","Long Position",!0),hn=an("short-position","Short Position",!1),dn=1.35,un=14;function pn(t,e,i,n){const r=i.split("\n");if(!0!==e.wrap)return r;const o=[];for(const e of r){const i=e.split(/\s+/).filter(t=>""!==t);if(0===i.length){o.push("");continue}let r=i[0];for(let e=1;e<i.length;e++){const s=`${r} ${i[e]}`;t.measureText(s).width>n?(o.push(r),r=i[e]):r=s}o.push(r)}return o}function fn(t,e,i,n){const r=(e.fontSize??un)*n;t.font=Pi(e,r);const o=pn(t,e,i,(e.wrapWidth??220)*n);let s=0;for(const e of o)s=Math.max(s,t.measureText(e).width);const l=r*dn;return{lines:o,width:s+10*n,height:o.length*l+10*n,lineHeight:l}}function xn(t,e,i="left",n="top"){const{ctx:r,rc:o,style:s}=t,l=Si(t.drawing);if(""===l.value)return;const a=o.dpr,c=(l.fontSize??un)*a,h=6*a;r.save(),r.setLineDash([]),r.font=Pi(l,c),r.fillStyle=l.color??s.color,r.textBaseline="top";const d=pn(r,l,l.value,Math.max(20*a,e.x1-e.x0-2*h)),u=c*dn,p=d.length*u,f=l.align??i;r.textAlign=f;const x="center"===f?(e.x0+e.x1)/2:"right"===f?e.x1-h:e.x0+h;let y;if("outside"===l.position)y=e.y0-p-h;else{const t=l.valign??n;y="middle"===t?(e.y0+e.y1-p)/2:"bottom"===t?e.y1-p-h:e.y0+h}for(const t of d)r.fillText(t,x,y),y+=u;r.restore()}const yn={id:"text",name:"Text",points:1,defaultText:{value:"Text",fontSize:un},settings:et([Z],{textIsContent:!0}),draw:t=>{const{ctx:e,rc:i,style:n}=t,r=i.dpr,o=Si(t.drawing),s=fn(e,o,Ti(t.drawing,"Text"),r),l=t.pts[0].x,a=o.valign??"top",c="middle"===a?t.pts[0].y-s.height/2:"bottom"===a?t.pts[0].y-s.height:t.pts[0].y;e.save(),e.setLineDash([]),!0===o.background&&(e.globalAlpha=o.backgroundOpacity??1,e.fillStyle=o.backgroundColor??i.theme.background,e.beginPath(),e.roundRect(l,c,s.width,s.height,4*r),e.fill(),e.globalAlpha=1),!0===o.border&&(e.strokeStyle=o.borderColor??n.color,e.lineWidth=Math.max(1,n.lineWidth*r),e.beginPath(),e.roundRect(l,c,s.width,s.height,4*r),e.stroke()),e.font=Pi(o,(o.fontSize??un)*r),e.fillStyle=o.color??n.color,e.textBaseline="top";const h=o.align??"left";e.textAlign=h;const d="center"===h?l+s.width/2:"right"===h?l+s.width-5*r:l+5*r;let u=c+5*r;for(const t of s.lines)e.fillText(t,d,u),u+=s.lineHeight;e.restore()},distance:(t,e,i)=>{const n=Si(i.drawing),r=Ti(i.drawing,"Text"),o=n.fontSize??un,s=i.pts[0],l=mn(),a=null===l?{width:r.length*o*.6+10,height:o*dn+10}:fn(l,n,r,1),c=n.valign??"top",h="middle"===c?s.y-a.height/2:"bottom"===c?s.y-a.height:s.y;return t>=s.x-3&&t<=s.x+a.width+3&&e>=h-3&&e<=h+a.height+3?0:null}};let gn;function mn(){if(void 0!==gn)return gn;try{gn="undefined"==typeof document?null:document.createElement("canvas").getContext("2d")}catch{gn=null}return gn}const bn={id:"path",name:"Path",points:0,settings:fi,draw:t=>{if(!(t.pts.length<2)){wi(t),t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,t.pts[0].y);for(let e=1;e<t.pts.length;e++)t.ctx.lineTo(t.pts[e].x,t.pts[e].y);t.ctx.stroke(),t.ctx.setLineDash([]),Ci(t,t.pts[t.pts.length-2],t.pts[t.pts.length-1])}},distance:(t,e,i)=>i.pts.length<2?null:L(t,e,i.pts)},vn={id:"brush",name:"Brush",points:0,freehand:!0,defaultStyle:{lineWidth:2},settings:et([U,bi]),draw:t=>{t.pts.length<2||(wi(t),$i(t,Math.max(1,Math.round(t.style.lineWidth*t.rc.dpr)),1),t.ctx.setLineDash([]))},distance:(t,e,i)=>i.pts.length<2?null:L(t,e,i.pts)},wn={id:"rotated-rectangle",name:"Rotated Rectangle",points:3,defaultStyle:{fill:!0,fillOpacity:.12},settings:Xi,draw:t=>{if(t.pts.length<3)return;const e=Mn(t.pts[0],t.pts[1],t.pts[2]);t.ctx.beginPath(),t.ctx.moveTo(e[0].x,e[0].y);for(let i=1;i<4;i++)t.ctx.lineTo(e[i].x,e[i].y);t.ctx.closePath(),_i(t,()=>t.ctx.fill()),wi(t),t.ctx.stroke(),t.ctx.setLineDash([]),xn(t,I(e),"center","middle")},distance:(t,e,i)=>{if(i.pts.length<3)return null;const n=Mn(i.pts[0],i.pts[1],i.pts[2]);if(!0===i.drawing.style.fill&&function(t,e,i){let n=!1;for(let r=0,o=i.length-1;r<i.length;o=r++){const s=i[r],l=i[o];s.y>e!=l.y>e&&t<(l.x-s.x)*(e-s.y)/(l.y-s.y)+s.x&&(n=!n)}return n}(t,e,n))return 0;let r=1/0;for(let i=0;i<4;i++)r=Math.min(r,k(t,e,n[i],n[(i+1)%4]));return r}};function Mn(t,e,i){const n=e.x-t.x,r=e.y-t.y,o=Math.hypot(n,r)||1,s=-r/o,l=n/o,a=(i.x-e.x)*s+(i.y-e.y)*l;return[t,e,{x:e.x+s*a,y:e.y+l*a},{x:t.x+s*a,y:t.y+l*a}]}const _n={id:"double-curve",name:"Double Curve",points:3,settings:fi,draw:t=>{if(t.pts.length<3)return;const[e,i,n]=t.pts;wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.bezierCurveTo(i.x,i.y,e.x+n.x-i.x,e.y+n.y-i.y,n.x,n.y),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>{if(i.pts.length<3)return null;const[n,r,o]=i.pts;return L(t,e,function(t,e,i,n,r){const o=[];for(let s=0;s<=r;s++){const l=s/r,a=1-l,c=a*a*a,h=3*a*a*l,d=3*a*l*l,u=l*l*l;o.push({x:c*t.x+h*e.x+d*i.x+u*n.x,y:c*t.y+h*e.y+d*i.y+u*n.y})}return o}(n,r,{x:n.x+o.x-r.x,y:n.y+o.y-r.y},o,24))}};const kn={id:"cyclic-lines",name:"Cyclic Lines",points:2,defaultStyle:{lineStyle:"dashed"},settings:fi,draw:t=>{const[e,i]=t.pts,n=i.x-e.x;if(!Number.isFinite(n)||Math.abs(n)<.5)return;const r=t.rc.plotHeight*t.rc.dpr,o=t.rc.plotWidth*t.rc.dpr;wi(t),t.ctx.beginPath();for(let i=0;i<=12;i++){const s=Math.round(e.x+n*i)+.5;s<0||s>o||(t.ctx.moveTo(s,0),t.ctx.lineTo(s,r))}t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>{const n=i.pts[1].x-i.pts[0].x;if(!Number.isFinite(n)||Math.abs(n)<.5)return null;const r=Math.round((t-i.pts[0].x)/n);return r<0||r>12?null:Math.abs(t-(i.pts[0].x+n*r))}},Sn={id:"time-cycles",name:"Time Cycles",points:2,settings:fi,draw:t=>{const[e,i]=t.pts,n=i.x-e.x;if(!Number.isFinite(n)||Math.abs(n)<1)return;const r=Math.abs(n)/2;wi(t),t.ctx.beginPath();for(let i=0;i<12;i++){const o=e.x+n*i+n/2;o+r<0||o-r>t.rc.plotWidth*t.rc.dpr||(t.ctx.moveTo(o+r,e.y),t.ctx.arc(o,e.y,r,0,Math.PI,!0))}t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>{const[n,r]=i.pts,o=r.x-n.x;if(!Number.isFinite(o)||Math.abs(o)<1)return null;const s=Math.abs(o)/2;let l=1/0;for(let i=0;i<12;i++){const r=n.x+o*i+o/2;e>n.y+2||(l=Math.min(l,Math.abs(Math.hypot(t-r,e-n.y)-s)))}return Number.isFinite(l)?l:null}},Pn={id:"sine-line",name:"Sine Line",points:2,settings:fi,draw:t=>{const e=Tn(t.pts[0],t.pts[1]);if(null!==e){wi(t),t.ctx.beginPath(),t.ctx.moveTo(e[0].x,e[0].y);for(let i=1;i<e.length;i++)t.ctx.lineTo(e[i].x,e[i].y);t.ctx.stroke(),t.ctx.setLineDash([])}},distance:(t,e,i)=>{const n=Tn(i.pts[0],i.pts[1]);return null===n?null:L(t,e,n)}};function Tn(t,e){const i=e.x-t.x,n=e.y-t.y;if(!Number.isFinite(i)||Math.abs(i)<1)return null;const r=[];for(let e=0;e<=64;e++){const o=e/64;r.push({x:t.x+i*o,y:t.y+n*Math.sin(o*Math.PI*2)})}return r}const Ln=et([R,G,X],{textIsContent:!0}),Fn={id:"price-label",name:"Price Label",points:1,settings:et([R,{...G,label:"Text (blank shows the price)"},X]),draw:t=>{const e=t.pts[0],i=t.rc.dpr,n=Ti(t.drawing,t.formatPrice(t.drawing.points[0].price)),r=An(t,[n],e.x+18*i,e.y-30*i,12);Nn(t,r,e,t.style.color),Cn(t,r,[n],t.style.color,12)},distance:(t,e,i)=>{const n=i.pts[0],r=n.x+18,o=n.y-30-11;return t>=r&&t<=r+64&&e>=o&&e<=o+22||Math.hypot(t-n.x,e-n.y)<=10?0:null}},In={id:"callout",name:"Callout",points:2,defaultText:{value:"Note",fontSize:12},settings:Ln,draw:t=>{if(t.pts.length<2)return;const[e,i]=t.pts,n=Ti(t.drawing,"Note").split("\n"),r=An(t,n,i.x,i.y,12);Nn(t,r,e,t.style.color),Cn(t,r,n,t.style.color,12)},distance:(t,e,i)=>{if(i.pts.length<2)return null;const[n,r]=i.pts;return Math.abs(t-r.x)<=60&&Math.abs(e-r.y)<=16?0:k(t,e,n,r)}};function An(t,e,i,n,r){const o=t.rc.dpr,s=Si(t.drawing),l=(s.fontSize??r)*o;t.ctx.save(),t.ctx.font=Pi(s,l);let a=0;for(const i of e)a=Math.max(a,t.ctx.measureText(i).width);t.ctx.restore();const c=a+14*o,h=1.4*l*e.length+8*o;return{x:i-c/2,y:n-h/2,w:c,h:h}}function Nn(t,e,i,n){const r=t.rc.dpr,o=e.x+e.w/2,s=e.y+e.h/2,l=Math.atan2(i.y-s,i.x-o),a=5*r,c=o+Math.cos(l)*(e.w/2-a),h=s+Math.sin(l)*(e.h/2-a);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.fillStyle=n,t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(c-Math.sin(l)*a,h+Math.cos(l)*a),t.ctx.lineTo(c+Math.sin(l)*a,h-Math.cos(l)*a),t.ctx.closePath(),t.ctx.fill(),t.ctx.beginPath(),D(t.ctx,e.x,e.y,e.w,e.h,5*r),t.ctx.fill(),t.ctx.beginPath(),t.ctx.arc(i.x,i.y,2.5*r,0,2*Math.PI),t.ctx.fill(),t.ctx.restore()}function Cn(t,e,i,n,r){const o=t.rc.dpr,s=Si(t.drawing),l=(s.fontSize??r)*o;t.ctx.save(),t.ctx.setLineDash([]),t.ctx.font=Pi(s,l),t.ctx.textAlign="center",t.ctx.textBaseline="middle",t.ctx.fillStyle=s.color??O(n);const a=1.4*l,c=e.y+e.h/2-a*(i.length-1)/2;for(let n=0;n<i.length;n++)t.ctx.fillText(i[n],e.x+e.w/2,c+a*n);t.ctx.restore()}const zn=et([U,J]),On={id:"flag-mark",name:"Flag Mark",points:1,defaultStyle:{fill:!0,fillOpacity:.95},settings:zn,draw:t=>{const e=t.pts[0],i=t.rc.dpr,n=22*i,r=15*i,o=11*i;wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(e.x,e.y-n),t.ctx.stroke(),t.ctx.setLineDash([]),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y-n),t.ctx.lineTo(e.x+r,e.y-n+.28*o),t.ctx.lineTo(e.x+.72*r,e.y-n+.5*o),t.ctx.lineTo(e.x+r,e.y-n+.72*o),t.ctx.lineTo(e.x,e.y-n+o),t.ctx.closePath(),!1!==t.style.fill&&(t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.95,t.ctx.fillStyle=Mi(t),t.ctx.fill(),t.ctx.restore()),t.ctx.stroke()},distance:(t,e,i)=>{const n=i.pts[0];return t>=n.x-4&&t<=n.x+16&&e>=n.y-24&&e<=n.y+2||Math.hypot(t-n.x,e-n.y)<=8?0:null}};function Dn(t,e,i,n,r){const o=t.rc.dpr,s=Si(t.drawing),l=(s.fontSize??r.size)*o;t.ctx.save(),t.ctx.font=Pi(s,l);let a=0;for(const i of e)a=Math.max(a,t.ctx.measureText(i).width);return t.ctx.restore(),{x:i,y:n,w:a+16*o,h:1.45*l*e.length+10*o}}function $n(t){return t.drawing.text?.backgroundColor??t.style.color}function Hn(t,e,i,n,r=6){const o=t.rc.dpr,s=Si(t.drawing),l=(s.fontSize??n.size)*o,a=$n(t);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.globalAlpha=s.backgroundOpacity??n.opacity,t.ctx.fillStyle=a,t.ctx.beginPath(),D(t.ctx,e.x,e.y,e.w,e.h,r*o),t.ctx.fill(),t.ctx.globalAlpha=1,!0===s.border&&(t.ctx.strokeStyle=s.borderColor??t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*o),t.ctx.stroke()),t.ctx.font=Pi(s,l),t.ctx.textAlign="left",t.ctx.textBaseline="middle",t.ctx.fillStyle=s.color??O(a);const c=1.45*l,h=e.y+e.h/2-c*(i.length-1)/2;for(let n=0;n<i.length;n++)t.ctx.fillText(i[n],e.x+8*o,h+c*n);t.ctx.restore()}function Wn(t,e,i,n,r,o){return t>=i&&t<=i+r&&e>=n&&e<=n+o?0:null}function Bn(t,e,i){return{w:120,h:1.45*(t.text?.fontSize??i.size)*Math.max(1,e)+10}}const Vn=(t,e)=>Ti(t,e).split("\n").length,Rn=et([R,j,tt],{textIsContent:!0}),jn=et([R,tt],{textIsContent:!0}),En={size:12,opacity:.95},Un={id:"note",name:"Note",points:1,defaultText:{value:"Note",fontSize:En.size},settings:Rn,draw:t=>{const e=t.rc.dpr,i=t.pts[0],n=Ti(t.drawing,"Note").split("\n"),r=Dn(t,n,i.x+16*e,i.y-34*e,En);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.strokeStyle=t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*e),t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(r.x,r.y+r.h),t.ctx.stroke(),t.ctx.fillStyle=t.style.color,t.ctx.beginPath(),t.ctx.arc(i.x,i.y,3.5*e,0,2*Math.PI),t.ctx.fill(),t.ctx.restore(),Hn(t,r,n,En)},distance:(t,e,i)=>{const n=i.pts[0],r=Bn(i.drawing,Vn(i.drawing,"Note"),En),o=Wn(t,e,n.x+16,n.y-34,r.w,r.h);return null!==o?o:Math.hypot(t-n.x,e-n.y)<=8?0:null}},Jn={size:12,opacity:.95},qn={id:"balloon",name:"Balloon",points:1,defaultText:{value:"Balloon",fontSize:Jn.size},settings:jn,draw:t=>{const e=t.rc.dpr,i=t.pts[0],n=Ti(t.drawing,"Balloon").split("\n"),r=Dn(t,n,0,0,Jn),o={x:i.x-r.w/2,y:i.y-r.h-12*e,w:r.w,h:r.h};t.ctx.save(),t.ctx.setLineDash([]),t.ctx.globalAlpha=t.drawing.text?.backgroundOpacity??Jn.opacity,t.ctx.fillStyle=$n(t),t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(i.x-6*e,o.y+o.h),t.ctx.lineTo(i.x+6*e,o.y+o.h),t.ctx.closePath(),t.ctx.fill(),t.ctx.restore(),Hn(t,o,n,Jn,8)},distance:(t,e,i)=>{const n=i.pts[0],r=Bn(i.drawing,Vn(i.drawing,"Balloon"),Jn),o=Wn(t,e,n.x-r.w/2,n.y-r.h-12,r.w,r.h);return null!==o?o:Math.hypot(t-n.x,e-n.y)<=8?0:null}},Yn={size:11,opacity:.92},Kn={id:"comment",name:"Comment",points:1,defaultText:{value:"Comment",fontSize:Yn.size},settings:jn,draw:t=>{const e=t.rc.dpr,i=t.pts[0],n=Ti(t.drawing,"Comment").split("\n"),r=Dn(t,n,0,0,Yn),o={x:i.x+8*e,y:i.y-r.h-10*e,w:r.w,h:r.h};t.ctx.save(),t.ctx.setLineDash([]),t.ctx.globalAlpha=t.drawing.text?.backgroundOpacity??Yn.opacity,t.ctx.fillStyle=$n(t),t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(o.x,o.y+o.h-5*e),t.ctx.lineTo(o.x+10*e,o.y+o.h),t.ctx.closePath(),t.ctx.fill(),t.ctx.restore(),Hn(t,o,n,Yn,3)},distance:(t,e,i)=>{const n=i.pts[0],r=Bn(i.drawing,Vn(i.drawing,"Comment"),Yn),o=Wn(t,e,n.x+8,n.y-r.h-10,r.w,r.h);return null!==o?o:Math.hypot(t-n.x,e-n.y)<=8?0:null}},Xn={size:11,opacity:.95},Gn={id:"signpost",name:"Signpost",points:1,defaultText:{value:"Event",fontSize:Xn.size},settings:Rn,draw:t=>{const e=t.rc.dpr,i=t.pts[0],n=34*e,r=Ti(t.drawing,"Event").split("\n"),o=Dn(t,r,0,0,Xn);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.strokeStyle=t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*e),t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(i.x,i.y-n),t.ctx.stroke(),t.ctx.fillStyle=t.style.color,t.ctx.beginPath(),t.ctx.arc(i.x,i.y,3*e,0,2*Math.PI),t.ctx.fill(),t.ctx.restore(),Hn(t,{x:i.x-o.w/2,y:i.y-n-o.h,w:o.w,h:o.h},r,Xn,4)},distance:(t,e,i)=>{const n=i.pts[0],r=Bn(i.drawing,Vn(i.drawing,"Event"),Xn),o=Wn(t,e,n.x-r.w/2,n.y-34-r.h,r.w,r.h);return null!==o?o:Math.abs(t-n.x)<=5&&e>=n.y-34&&e<=n.y+4?0:null}},Zn={size:11,opacity:.95},Qn={id:"price-note",name:"Price Note",points:1,defaultText:{value:"Note",fontSize:Zn.size},settings:Rn,draw:t=>{const e=t.rc.dpr,i=t.pts[0],n=[t.formatPrice(t.drawing.points[0].price),...Ti(t.drawing,"Note").split("\n")],r=Dn(t,n,i.x+14*e,i.y-12*e,Zn);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.strokeStyle=t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*e),t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(r.x,i.y),t.ctx.stroke(),t.ctx.restore(),Hn(t,r,n,Zn,4)},distance:(t,e,i)=>{const n=i.pts[0],r=Bn(i.drawing,Vn(i.drawing,"Note")+1,Zn),o=Wn(t,e,n.x+14,n.y-12,r.w,r.h);return null!==o?o:Math.hypot(t-n.x,e-n.y)<=8?0:null}};function tr(t,e,i,n){const r=(e.fontSize??11)*n,o=Array.from({length:Math.max(...i.map(t=>t.length))},()=>0);t?.save();try{for(let s=0;s<i.length;s++){null!==t&&(t.font=Pi(e,r,0===s?!0===e.bold?"800":"600":void 0));for(let e=0;e<o.length;e++){const l=i[s][e]??"",a=null===t?l.length*r*.6:mt(t,l);o[e]=Math.max(o[e],a+14*n)}}}finally{t?.restore()}return o}const er={id:"table",name:"Table",points:1,defaultText:{value:"Level|Price\nEntry|-\nStop|-",fontSize:11},settings:et([R,{...j,label:"Border width"},G,X,{path:"text.backgroundColor",label:"Background color",kind:"color",group:"text"},{path:"text.backgroundOpacity",label:"Background opacity",kind:"opacity",min:0,max:1,step:.01,group:"text"},{path:"text.border",label:"Border",kind:"boolean",group:"text"},{path:"text.borderColor",label:"Border color",kind:"color",group:"text"}],{textIsContent:!0}),draw:t=>{const e=t.rc.dpr,i=t.pts[0],n=Si(t.drawing),r=ir(t.drawing),o=(n.fontSize??11)*e,s=7*e,l=1.7*o;t.ctx.save();const a=tr(t.ctx,n,r,e),c=a.reduce((t,e)=>t+e,0),h=l*r.length;if(t.ctx.setLineDash([]),t.ctx.globalAlpha=n.backgroundOpacity??.92,t.ctx.fillStyle=n.backgroundColor??t.rc.theme.background,t.ctx.beginPath(),D(t.ctx,i.x,i.y,c,h,4*e),t.ctx.fill(),t.ctx.globalAlpha=1,!1!==n.border){t.ctx.strokeStyle=n.borderColor??t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*e),t.ctx.stroke(),t.ctx.globalAlpha=.45,t.ctx.beginPath();for(let e=1;e<r.length;e++)t.ctx.moveTo(i.x,i.y+l*e),t.ctx.lineTo(i.x+c,i.y+l*e);let o=i.x;for(let e=0;e<a.length-1;e++)o+=a[e],t.ctx.moveTo(o,i.y),t.ctx.lineTo(o,i.y+h);t.ctx.stroke(),t.ctx.globalAlpha=1}t.ctx.textBaseline="middle",t.ctx.textAlign="left";const d=Pi(n,o,!0===n.bold?"800":"600"),u=Pi(n,o);for(let e=0;e<r.length;e++){t.ctx.fillStyle=n.color??t.style.color,t.ctx.font=0===e?d:u;let o=i.x;for(let n=0;n<a.length;n++)t.ctx.fillText(r[e][n]??"",o+s,i.y+l*e+l/2),o+=a[n]}t.ctx.restore()},distance:(t,e,i)=>{const n=i.pts[0],r=ir(i.drawing),o=i.drawing.text?.fontSize??11,s=tr(mn(),Si(i.drawing),r,1).reduce((t,e)=>t+e,0);return Wn(t,e,n.x,n.y,s,1.7*o*r.length)}};function ir(t){return Ti(t,"Level|Price").split("\n").map(t=>t.split("|"))}const nr={id:"price-range",name:"Price Range",points:2,defaultStyle:{fill:!0,showLabels:!0,fillOpacity:.1},settings:on,draw:t=>{const e=t.rc.dpr,i=t.drawing.points,n=i[1].price-i[0].price,r=0!==i[0].price?n/i[0].price*100:0,o=n>=0,s=o?zi:Oi,l=Math.min(t.pts[0].x,t.pts[1].x),a=Math.max(t.pts[0].x,t.pts[1].x)+(t.pts[0].x===t.pts[1].x?60*e:0),c=t.pts[0].y,h=t.pts[1].y;t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.1,t.ctx.fillStyle=s,t.ctx.fillRect(l,Math.min(c,h),a-l,Math.abs(h-c)),t.ctx.restore(),wi(t),t.ctx.strokeStyle=s,t.ctx.beginPath();for(const e of[c,h])t.ctx.moveTo(l,Math.round(e)+.5),t.ctx.lineTo(a,Math.round(e)+.5);const d=(l+a)/2;t.ctx.moveTo(d,c),t.ctx.lineTo(d,h);const u=h>c?1:-1;if(t.ctx.moveTo(d-4*e,h-5*e*u),t.ctx.lineTo(d,h),t.ctx.lineTo(d+4*e,h-5*e*u),t.ctx.moveTo(d-4*e,c+5*e*u),t.ctx.lineTo(d,c),t.ctx.lineTo(d+4*e,c+5*e*u),t.ctx.stroke(),t.ctx.setLineDash([]),!1===t.style.showLabels)return;const p=o?"+":"";Li(t,`${p}${t.formatPrice(n)} (${p}${r.toFixed(2)}%)`,d+6*e,(c+h)/2,s)},distance:(t,e,i)=>A(t,e,i.pts[0],i.pts[1],!0)},rr={id:"date-range",name:"Date Range",points:2,defaultStyle:{fill:!0,showLabels:!0,fillOpacity:.1},settings:et([U,yi,Y,X]),draw:t=>{const e=t.rc.dpr,i=t.drawing.points,n=t.style.color,r=t.pts[0].x,o=t.pts[1].x,s=Math.min(t.pts[0].y,t.pts[1].y),l=Math.max(t.pts[0].y,t.pts[1].y)+(t.pts[0].y===t.pts[1].y?40*e:0);t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.1,t.ctx.fillStyle=n,t.ctx.fillRect(Math.min(r,o),s,Math.abs(o-r),l-s),t.ctx.restore(),wi(t),t.ctx.beginPath();for(const e of[r,o])t.ctx.moveTo(Math.round(e)+.5,s),t.ctx.lineTo(Math.round(e)+.5,l);const a=(s+l)/2;t.ctx.moveTo(r,a),t.ctx.lineTo(o,a);const c=o>r?1:-1;if(t.ctx.moveTo(o-5*e*c,a-4*e),t.ctx.lineTo(o,a),t.ctx.lineTo(o-5*e*c,a+4*e),t.ctx.moveTo(r+5*e*c,a-4*e),t.ctx.lineTo(r,a),t.ctx.lineTo(r+5*e*c,a+4*e),t.ctx.stroke(),t.ctx.setLineDash([]),!1===t.style.showLabels)return;const h=t.rc.dataLayer.timeToIndexFloat(i[0].time),d=t.rc.dataLayer.timeToIndexFloat(i[1].time);Li(t,`${Math.abs(Math.round(d-h))} bars`,(r+o)/2,s-10*e,n)},distance:(t,e,i)=>A(t,e,i.pts[0],i.pts[1],!0)},or={id:"forecast",name:"Forecast",points:2,defaultStyle:{fill:!0,showLabels:!0,fillOpacity:.12,lineStyle:"dashed"},settings:on,draw:t=>{const e=t.rc.dpr,i=t.drawing.points,[n,r]=t.pts,o=i[1].price-i[0].price,s=0!==i[0].price?o/i[0].price*100:0,l=o>=0,a=l?zi:Oi,c=.35*Math.abs(r.y-n.y)+6*e;if(t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.12,t.ctx.fillStyle=a,t.ctx.beginPath(),t.ctx.moveTo(n.x,n.y),t.ctx.lineTo(r.x,r.y-c),t.ctx.lineTo(r.x,r.y+c),t.ctx.closePath(),t.ctx.fill(),t.ctx.restore(),wi(t),t.ctx.strokeStyle=a,t.ctx.beginPath(),t.ctx.moveTo(n.x,n.y),t.ctx.lineTo(r.x,r.y),t.ctx.stroke(),t.ctx.setLineDash([]),t.ctx.beginPath(),t.ctx.arc(n.x,n.y,3*e,0,2*Math.PI),t.ctx.fillStyle=a,t.ctx.fill(),!1===t.style.showLabels)return;const h=l?"+":"";Fi(t,[t.formatPrice(i[0].price),Ai(i[0].time)],n.x,n.y,a,{align:"center"}),Fi(t,[`${h}${t.formatPrice(o)} (${h}${s.toFixed(2)}%) in ${Ni(i[1].time-i[0].time)}`,`${t.formatPrice(i[1].price)}, ${Ai(i[1].time)}`],r.x,r.y,a,{align:"center",place:"below"});const d=t.rc.bars?.();if(void 0!==d&&d.length>0&&d[d.length-1].time>=i[1].time){let n=!1;for(const t of d)if(!(t.time<i[0].time)){if(t.time>i[1].time)break;if(l?t.high>=i[1].price:t.low<=i[1].price){n=!0;break}}Fi(t,[n?"SUCCESS":"MISSED"],r.x,r.y+36*e,n?"#4a934a":"#8a4a4a",{align:"center",place:"below"})}},distance:(t,e,i)=>k(t,e,i.pts[0],i.pts[1])},sr={id:"circle",name:"Circle",points:2,defaultStyle:{fill:!0},settings:Xi,draw:t=>{const[e,i]=t.pts,n=Math.hypot(i.x-e.x,i.y-e.y);t.ctx.beginPath(),t.ctx.arc(e.x,e.y,n,0,2*Math.PI),_i(t,()=>t.ctx.fill()),wi(t),t.ctx.stroke(),t.ctx.setLineDash([]),xn(t,{x0:e.x-n,y0:e.y-n,x1:e.x+n,y1:e.y+n},"center","middle")},distance:(t,e,i)=>{const[n,r]=i.pts,o=Math.hypot(r.x-n.x,r.y-n.y),s=Math.hypot(t-n.x,e-n.y);return!0===i.drawing.style.fill&&s<=o?0:Math.abs(s-o)}},lr={id:"triangle",name:"Triangle",points:3,defaultStyle:{fill:!0},settings:Xi,draw:t=>{t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,t.pts[0].y),t.ctx.lineTo(t.pts[1].x,t.pts[1].y),t.ctx.lineTo(t.pts[2].x,t.pts[2].y),t.ctx.closePath(),_i(t,()=>t.ctx.fill()),wi(t),t.ctx.stroke(),t.ctx.setLineDash([]),xn(t,I(t.pts.slice(0,3)),"center","middle")},distance:(t,e,i)=>L(t,e,[...i.pts,i.pts[0]])},ar={id:"polyline",name:"Polyline",points:0,defaultStyle:{fill:!1},settings:et([U,J]),draw:t=>{if(!(t.pts.length<2)){t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,t.pts[0].y);for(let e=1;e<t.pts.length;e++)t.ctx.lineTo(t.pts[e].x,t.pts[e].y);!0===t.style.fill&&(t.ctx.closePath(),_i(t,()=>t.ctx.fill())),wi(t),t.ctx.stroke(),t.ctx.setLineDash([])}},distance:(t,e,i)=>L(t,e,i.pts)};const cr={id:"arc",name:"Arc",points:3,settings:fi,draw:t=>{const[e,i,n]=t.pts;wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.quadraticCurveTo(2*i.x-(e.x+n.x)/2,2*i.y-(e.y+n.y)/2,n.x,n.y),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>L(t,e,function(t,e,i){const n=2*e.x-(t.x+i.x)/2,r=2*e.y-(t.y+i.y)/2,o=[];for(let e=0;e<=16;e++){const s=e/16,l=1-s;o.push({x:l*l*t.x+2*l*s*n+s*s*i.x,y:l*l*t.y+2*l*s*r+s*s*i.y})}return o}(i.pts[0],i.pts[1],i.pts[2]))},hr={id:"curve",name:"Curve",points:3,settings:fi,draw:t=>{const[e,i,n]=t.pts;wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.quadraticCurveTo(i.x,i.y,n.x,n.y),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>{const[n,r,o]=i.pts,s=[];for(let t=0;t<=16;t++){const e=t/16,i=1-e;s.push({x:i*i*n.x+2*i*e*r.x+e*e*o.x,y:i*i*n.y+2*i*e*r.y+e*e*o.y})}return L(t,e,s)}};function dr(t,e,i,n="vertical"){return{id:t,name:e,points:1,defaultStyle:{fill:!0,fillOpacity:.9},settings:zn,draw:t=>{const e=t.rc.dpr,{x:r,y:o}=t.pts[0],s=16*e,l=6*e,a=i?1:-1,c=o+2*a*e;if(t.ctx.beginPath(),"vertical"!==n){const i="right"===n?-1:1,a=r+2*i*e;t.ctx.moveTo(a,o),t.ctx.lineTo(a+i*l,o-l),t.ctx.lineTo(a+i*l,o-l/2.4),t.ctx.lineTo(a+i*s,o-l/2.4),t.ctx.lineTo(a+i*s,o+l/2.4),t.ctx.lineTo(a+i*l,o+l/2.4),t.ctx.lineTo(a+i*l,o+l),t.ctx.closePath()}else t.ctx.moveTo(r,c),t.ctx.lineTo(r-l,c+a*l),t.ctx.lineTo(r-l/2.4,c+a*l),t.ctx.lineTo(r-l/2.4,c+a*s),t.ctx.lineTo(r+l/2.4,c+a*s),t.ctx.lineTo(r+l/2.4,c+a*l),t.ctx.lineTo(r+l,c+a*l),t.ctx.closePath();!1!==t.style.fill&&(t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.9,t.ctx.fillStyle=Mi(t),t.ctx.fill(),t.ctx.restore()),wi(t),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,n)=>{const r=n.pts[0],o=i?e-r.y:r.y-e;return Math.abs(t-r.x)<=8&&o>=-4&&o<=20?0:null}}}const ur=dr("arrow-up","Arrow Up",!0),pr=dr("arrow-down","Arrow Down",!1),fr=dr("arrow-left","Arrow Left",!0,"left"),xr=dr("arrow-right","Arrow Right",!0,"right"),yr={id:"highlighter",name:"Highlighter",points:0,freehand:!0,defaultStyle:{lineWidth:12,fillOpacity:.28},settings:et([R,j,yi,bi]),draw:t=>{t.pts.length<2||(t.ctx.setLineDash([]),$i(t,Math.max(2,(t.style.lineWidth||12)*t.rc.dpr),t.style.fillOpacity??.28))},distance:(t,e,i)=>{const n=L(t,e,i.pts);return n<=Math.max(6,(i.drawing.style.lineWidth??12)/2)?0:n}},gr=et([j,E,K,X]),mr={id:"fib-channel",name:"Fib Channel",points:3,alertValue:_("channel",h),alertLevels:M(h),defaultStyle:{showLabels:!0,levels:c(h)},settings:et([j,E,K,q,X]),draw:t=>{const e=Hi(t.style.levels,h),[i,n,r]=t.pts,o=t.rc.dpr,s=r.x-n.x,l=r.y-n.y,a=!0===t.style.extendLeft,c=!0===t.style.extendRight;wi(t);for(const r of e){const[e,h]=C({x:i.x+s*r.ratio,y:i.y+l*r.ratio},{x:n.x+s*r.ratio,y:n.y+l*r.ratio},t.rc.plotWidth*o,a,c),d=Wi(r);t.ctx.strokeStyle=d,t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(h.x,h.y),t.ctx.stroke(),!1!==t.style.showLabels&&Li(t,Bi(r),n.x+s*r.ratio+4*o,n.y+l*r.ratio,d)}t.ctx.setLineDash([])},distance:(t,e,i)=>{const n=Hi(i.drawing.style.levels,h),[r,o,s]=i.pts,l=!0===i.drawing.style.extendLeft,a=!0===i.drawing.style.extendRight;let c=1/0;for(const h of n){const[n,d]=C({x:r.x+(s.x-o.x)*h.ratio,y:r.y+(s.y-o.y)*h.ratio},{x:o.x+(s.x-o.x)*h.ratio,y:o.y+(s.y-o.y)*h.ratio},i.rc.plotWidth,l,a),u=k(t,e,n,d);u<c&&(c=u)}return c}},br={id:"fib-time-zone",name:"Fib Time Zone",points:2,defaultStyle:{showLabels:!0,levels:c(f)},settings:et([U,K,X]),draw:t=>{const[e,i]=t.pts,n=i.x-e.x;if(Math.abs(n)<.5)return;const r=t.rc.dpr,o=t.rc.plotWidth*r,s=Hi(t.style.levels,f);wi(t);for(const i of s){const s=e.x+n*i.ratio;if(s<-10||s>o+10)continue;const l=i.color??t.style.color;t.ctx.strokeStyle=l,t.ctx.beginPath(),t.ctx.moveTo(Math.round(s)+.5,0),t.ctx.lineTo(Math.round(s)+.5,t.rc.plotHeight*r),t.ctx.stroke(),!1!==t.style.showLabels&&Li(t,i.label??String(i.ratio),s+3*r,12*r,l)}t.ctx.setLineDash([])},distance:(t,e,i)=>{const[n,r]=i.pts,o=r.x-n.x;if(Math.abs(o)<.5)return null;let s=1/0;for(const e of Hi(i.drawing.style.levels,f))s=Math.min(s,Math.abs(t-(n.x+o*e.ratio)));return Number.isFinite(s)?s:null}},vr={id:"fib-fan",name:"Fib Fan",points:2,defaultStyle:{showLabels:!0,levels:c(d)},settings:gr,draw:t=>{const e=Hi(t.style.levels,d),[i,n]=t.pts,r=t.rc.dpr,o=t.rc.plotWidth*r;wi(t);for(const s of e){const e=C(i,{x:n.x,y:i.y+(n.y-i.y)*s.ratio},o,!1,!0)[1],l=Wi(s);t.ctx.strokeStyle=l,t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(e.x,e.y),t.ctx.stroke(),!1!==t.style.showLabels&&Li(t,Bi(s),n.x+4*r,i.y+(n.y-i.y)*s.ratio,l)}t.ctx.setLineDash([])},distance:(t,e,i)=>{const n=Hi(i.drawing.style.levels,d),[r,o]=i.pts;let s=1/0;for(const l of n)s=Math.min(s,k(t,e,r,C(r,{x:o.x,y:r.y+(o.y-r.y)*l.ratio},i.rc.plotWidth,!1,!0)[1]));return Number.isFinite(s)?s:null}},wr={id:"gann-fan",name:"Gann Fan",points:2,defaultStyle:{showLabels:!0,levels:c(p)},settings:gr,draw:t=>{const[e,i]=t.pts,n=t.rc.dpr,r=t.rc.plotWidth*n,s=i.x-e.x,a=i.y-e.y;if(Math.abs(s)<.5)return;const c=Hi(t.style.levels,p);wi(t),c.forEach((c,h)=>{const d=C(e,{x:e.x+s,y:e.y+a*c.ratio},r,!1,!0)[1],u=c.color??o(h);t.ctx.strokeStyle=u,t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(d.x,d.y),t.ctx.stroke(),!1!==t.style.showLabels&&Li(t,c.label??l(c.ratio),i.x+4*n,e.y+a*c.ratio,u)}),t.ctx.setLineDash([])},distance:(t,e,i)=>{const[n,r]=i.pts,o=r.x-n.x,s=r.y-n.y;if(Math.abs(o)<.5)return null;let l=1/0;for(const r of Hi(i.drawing.style.levels,p))l=Math.min(l,k(t,e,n,C(n,{x:n.x+o,y:n.y+s*r.ratio},i.rc.plotWidth,!1,!0)[1]));return Number.isFinite(l)?l:null}},Mr={id:"gann-box",name:"Gann Box",points:2,defaultStyle:{fill:!0,fillOpacity:.05,showLabels:!0,levels:c(u)},settings:et([U,J,K,X]),draw:t=>{const[e,i]=t.pts,n=F(e,i),r=t.rc.dpr,o=t.drawing.points,s=Hi(t.style.levels,u);_i(t,()=>t.ctx.fillRect(n.x0,n.y0,n.x1-n.x0,n.y1-n.y0)),wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(i.x,i.y),t.ctx.stroke();for(const l of s){const s=Math.round(e.x+(i.x-e.x)*l.ratio)+.5,a=o[0].price+(o[1].price-o[0].price)*l.ratio,c=Math.round(t.rc.priceScale.priceToY(a)*r)+.5,h=Wi(l);t.ctx.strokeStyle=h,t.ctx.beginPath(),t.ctx.moveTo(s,n.y0),t.ctx.lineTo(s,n.y1),t.ctx.moveTo(n.x0,c),t.ctx.lineTo(n.x1,c),t.ctx.stroke(),!1!==t.style.showLabels&&Li(t,Bi(l),n.x1+4*r,c,h)}t.ctx.setLineDash([])},distance:(t,e,i)=>A(t,e,i.pts[0],i.pts[1],!0===i.drawing.style.fill)},_r=[Ri,ji,Ei,Ui,Ji,qi,Yi,Ki,Gi,Zi,Qi,nn,rn,cn,hn,or,sn,nr,rr,sr,lr,ar,cr,hr,wn,_n,ur,pr,yr,vn,mr,br,vr,wr,Mr,kn,Sn,Pn,yn,bn,Fn,In,On,Un,qn,Kn,Gn,Qn,er,fr,xr,...ne,...Te,...je,ri,oi];let kr=!1;function Sr(){if(!kr){kr=!0;for(const t of _r)li(t)}}const Pr=2,Tr=t=>Number.isFinite(t.zIndex)?t.zIndex:0;function Lr(t){return t.slice().sort((t,e)=>Tr(t)-Tr(e))}function Fr(t,e){return ci(t)&&!0===ai(t).freehand&&e>2?[0,e-1]:Array.from({length:e},(t,e)=>e)}class Ir{constructor(t="top"){Object.defineProperty(this,"_order",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_drawings",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_selected",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_hovered",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_snap",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_preview",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_below",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_above",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_touch",{enumerable:!0,configurable:!0,writable:!0,value:!1}),this._order=t}attached(t){this._host=t}detached(){this._host=null}zOrder(){return this._order}autoscaleInfo(){return null}setDrawings(t){this._drawings=Lr(t),this._host?.requestUpdate()}setSelected(t){t.length===this._selected.length&&t.every((t,e)=>t===this._selected[e])||(this._selected=t.slice(),this._host?.requestUpdate())}setHovered(t){t!==this._hovered&&(this._hovered=t,this._requestOverlay())}hovered(){return this._hovered}setSnapPoint(t){(null===t?null!==this._snap:null===this._snap||this._snap.time!==t.time||this._snap.price!==t.price)&&(this._snap=null===t?null:{time:t.time,price:t.price},this._requestOverlay())}snapPoint(){return this._snap}setPointerType(t){this._touch="touch"===t}setPreview(t){this._preview=t,this._host?.requestUpdate()}setBelow(t){this._below!==t&&(null!==this._below&&(this._below._above=null),this._below=t,null!==t&&(null!==t._above&&t._above!==this&&(t._above._below=null),t._above=this,t._host?.requestUpdate()),this._host?.requestUpdate())}_requestOverlay(){null===this._above&&this._host?.requestUpdate()}_isTouch(t){const e=t.pointerType;return"touch"===e||"mouse"!==e&&"pen"!==e&&this._touch}_grabRadius(t){return this._isTouch(t)?12:6}_handleRadius(t){return this._isTouch(t)?10:5}_project(t,e,i){return{x:t.timeScale.indexToX(t.dataLayer.timeToIndexFloat(e)),y:t.priceScale.priceToY(i)}}_points(t,e){return e.points.map(e=>this._project(t,e.time,e.price))}_handled(){if(0===this._selected.length)return[];const t=new Set(this._selected);return this._drawings.filter(e=>t.has(e.id)&&!0!==e.locked&&!1!==e.visible)}_hoverHandled(){const t=this._hovered;if(null===t||this._selected.includes(t))return null;const e=this._drawings.find(e=>e.id===t);return void 0===e||!0===e.locked||!1===e.visible?null:e}draw(t,e){t.save(),t.beginPath(),t.rect(0,0,Math.round(e.plotWidth*e.dpr),Math.round(e.plotHeight*e.dpr)),t.clip();try{!function(t,e){const i=gt.get(t);gt.set(t,new Map);try{return e()}finally{i?gt.set(t,i):gt.delete(t)}}(t,()=>this._drawContent(t,this._drawingContext(e)))}finally{t.restore()}}_drawingContext(t){return t.readoutPriceScale?{...t,priceScale:t.readoutPriceScale}:t}_drawContent(t,e){const i=new Set(this._selected),n=null===this._preview?this._drawings:[...this._drawings,this._preview];for(const r of n){if(!1===r.visible||!ci(r.tool))continue;const n=ai(r.tool),o=this._points(e,r);if(0===o.length||!o.every(t=>Number.isFinite(t.x)&&Number.isFinite(t.y)))continue;if(r.points.length<Math.max(1,n.points)){r===this._preview&&this._drawPlacementGuide(t,e,o,r);continue}const s={color:r.style.color??e.theme.lineColor,lineWidth:r.style.lineWidth??1.5,...r.style},l=e.dpr;t.save(),r===this._preview&&(t.globalAlpha=.7),n.draw({ctx:t,rc:e,drawing:r,selected:i.has(r.id),pts:o.map(t=>({x:t.x*l,y:t.y*l})),style:{...s,color:s.color,lineWidth:s.lineWidth},formatPrice:t=>e.priceScale.format(t)}),t.restore()}if(null!==this._above)return;const r=[this._hoverHandled(),this._below?._hoverHandled()??null];for(const i of r)null!==i&&this._drawHandles(t,e,this._points(e,i),i.tool,!0);for(const i of this._handled())this._drawHandles(t,e,this._points(e,i),i.tool,!1);if(null!==this._below)for(const i of this._below._handled())this._drawHandles(t,e,this._points(e,i),i.tool,!1);null!==this._snap&&this._drawSnapRing(t,e,this._project(e,this._snap.time,this._snap.price))}_drawPlacementGuide(t,e,i,n){const r=e.dpr;t.save(),t.globalAlpha*=.7,t.strokeStyle=n.style.color??e.theme.lineColor,t.lineWidth=Math.max(1,(n.style.lineWidth??1.5)*r),t.setLineDash([3*r,3*r]),t.beginPath(),t.moveTo(i[0].x*r,i[0].y*r);for(let e=1;e<i.length;e++)t.lineTo(i[e].x*r,i[e].y*r);t.stroke(),t.setLineDash([]);for(const n of i)t.beginPath(),t.arc(n.x*r,n.y*r,3*r,0,2*Math.PI),t.fillStyle=e.theme.background,t.fill(),t.stroke();t.restore()}_drawHandles(t,e,i,n,r){const o=e.dpr;t.save(),t.setLineDash([]),t.lineWidth=Math.max(1,Math.round((r?1:1.5)*o)),r&&(t.globalAlpha=.6);for(const r of Fr(n,i.length)){const n=i[r];t.beginPath(),t.arc(n.x*o,n.y*o,5*o,0,2*Math.PI),t.fillStyle=e.theme.background,t.fill(),t.strokeStyle=e.theme.lineColor,t.stroke()}t.restore()}_drawSnapRing(t,e,i){if(!Number.isFinite(i.x)||!Number.isFinite(i.y))return;const n=e.dpr;t.save(),t.setLineDash([]),t.globalAlpha=.9,t.lineWidth=Math.max(1,Math.round(1.5*n)),t.strokeStyle=e.theme.axisText,t.beginPath(),t.arc(i.x*n,i.y*n,5.5*n,0,2*Math.PI),t.stroke(),t.restore()}hitTest(t,e,i){if(i=this._drawingContext(i),null!==this._above)return null;const n=this._hitHandle(t,e,i)??this._below?._hitHandle(t,e,i)??null;return null!==n?n:this._hitBody(t,e,i)??this._below?._hitBody(t,e,i)??null}_hitHandle(t,e,i){const n=this._handleRadius(i)+2;for(const r of this._handled()){const o=this._points(i,r);for(const i of Fr(r.tool,o.length))if(Math.hypot(t-o[i].x,e-o[i].y)<=n)return{externalId:`draw:${r.id}#${i}`,zOrder:"top",distance:0,cursor:"grabbing",draggable:!0}}return null}_hitBody(t,e,i){const n=this._grabRadius(i);let r=null;for(let o=this._drawings.length-1;o>=0;o--){const s=this._drawings[o];if(!1===s.visible||!0===s.locked||!ci(s.tool))continue;const l=ai(s.tool);if(s.points.length<Math.max(1,l.points))continue;const a=l.distance(t,e,{pts:this._points(i,s),drawing:s,rc:i});if(null!==a&&Number.isFinite(a)&&!(a>n)&&((null===r||a<r.distance)&&(r={id:s.id,distance:a}),0===a))break}return null===r?null:{externalId:`draw:${r.id}`,zOrder:"top",distance:r.distance,cursor:"move",draggable:!0}}}const Ar=t=>"object"==typeof t&&null!==t&&!Array.isArray(t),Nr=t=>"number"==typeof t&&Number.isFinite(t),Cr=(t,e)=>"string"==typeof t&&e.includes(t),zr=t=>"__proto__"===t||"constructor"===t||"prototype"===t,Or=["solid","dashed","dotted"],Dr=["left","center","right"],$r=["top","middle","bottom"],Hr=["inside","outside"],Wr=["color","fillColor"],Br=["lineWidth","fillOpacity","accountSize","risk"],Vr=["fill","extendLeft","extendRight","showLabels","showStats","pressure"],Rr=["color","fontFamily","backgroundColor","borderColor"],jr=["fontSize","wrapWidth","backgroundOpacity"],Er=["bold","italic","wrap","background","border"];let Ur=1;function Jr(t){const e=Array.isArray(t)?t:Ar(t)&&Array.isArray(t.drawings)?t.drawings:null,i=[];if(null===e)return{version:2,drawings:i};const n=new Set;for(const t of e){const e=qr(t);if(null!==e){for(;""===e.id||n.has(e.id);)e.id="m"+Ur++;n.add(e.id),i.push(e)}}return{version:2,drawings:i}}function qr(t){if(!Ar(t))return null;const e=t.tool;if("string"!=typeof e||""===e)return null;const i=function(t){if(!Array.isArray(t)||0===t.length)return null;const e=[];for(const i of t){if(!Ar(i)||!Nr(i.time)||!Nr(i.price))return null;const t={time:i.time,price:i.price};Nr(i.pressure)&&i.pressure>=0&&i.pressure<=1&&(t.pressure=i.pressure),e.push(t)}return e}(t.points);if(null===i)return null;const n=void 0===t.paneIndex?0:t.paneIndex;if(!Nr(n)||n<0||!Number.isInteger(n))return null;const r=Ar(t.style)?t.style:{},o={id:"string"==typeof t.id?t.id:Nr(t.id)?String(t.id):"",tool:e,points:i,style:Yr(r,e),paneIndex:n,zIndex:Nr(t.zIndex)?t.zIndex:0},s=(l=t.text,(Ar(l)&&"string"==typeof l.value?Kr(l.value,l):null)??function(t){return"string"!=typeof t.text?null:Kr(t.text,{color:t.fontColor,fontSize:t.fontSize,fontFamily:t.fontFamily,bold:"bold"===t.fontWeight||void 0,italic:"italic"===t.fontStyle||void 0,align:t.textAlign,valign:t.textVAlign,position:t.textPosition,wrap:t.wrap,wrapWidth:t.wrapWidth,background:t.background,backgroundColor:t.backgroundColor,backgroundOpacity:t.backgroundOpacity,border:t.border,borderColor:t.borderColor})}(r));var l;return null!==s&&(o.text=s),Ar(t.props)&&(o.props=Xr(t.props,0)),"boolean"==typeof t.locked&&(o.locked=t.locked),"boolean"==typeof t.visible&&(o.visible=t.visible),Nr(t.createdAt)&&(o.createdAt=t.createdAt),o}function Yr(t,e){const i={};for(const e of Wr){const n=t[e];"string"==typeof n&&(i[e]=n)}for(const e of Br){const n=t[e];Nr(n)&&(i[e]=n)}for(const e of Vr){const n=t[e];"boolean"==typeof n&&(i[e]=n)}Cr(t.lineStyle,Or)&&(i.lineStyle=t.lineStyle);const r=function(t,e){if(!Array.isArray(t))return null;const i=[];return t.forEach((t,r)=>{if(Nr(t))i.push(function(t,e,i){return"fib-time-zone"===t?{ratio:e}:"gann-fan"===t?{ratio:e,color:o(i)}:{ratio:e,color:n(e)}}(e,t,r));else if(Ar(t)&&Nr(t.ratio)){const e={ratio:t.ratio};"string"==typeof t.color&&(e.color=t.color),"boolean"==typeof t.enabled&&(e.enabled=t.enabled),"string"==typeof t.label&&(e.label=t.label),i.push(e)}}),0===i.length&&t.length>0?null:i}(t.levels,e);return null!==r&&(i.levels=r),i}function Kr(t,e){const i={value:t};for(const t of Rr){const n=e[t];"string"==typeof n&&(i[t]=n)}for(const t of jr){const n=e[t];Nr(n)&&(i[t]=n)}for(const t of Er){const n=e[t];"boolean"==typeof n&&(i[t]=n)}return Cr(e.align,Dr)&&(i.align=e.align),Cr(e.valign,$r)&&(i.valign=e.valign),Cr(e.position,Hr)&&(i.position=e.position),i}function Xr(t,e){const i={};for(const[n,r]of Object.entries(t)){if(zr(n))continue;const t=Gr(r,e);void 0!==t&&(i[n]=t)}return i}function Gr(t,e){return null===t||"boolean"==typeof t||"string"==typeof t?t:"number"==typeof t?Number.isFinite(t)?t:void 0:e>=8?void 0:Array.isArray(t)?t.map(t=>Gr(t,e+1)??null):Ar(t)?Xr(t,e+1):void 0}const Zr="openalgo-charts/drawings",Qr=2,to=4096;let eo=null;function io(){eo=null}function no(){const t=globalThis.navigator,e=t?.clipboard;return void 0===e||"function"!=typeof e.writeText||"function"!=typeof e.readText?null:e}function ro(t){const e={...t,points:t.points.map(oo),style:so(t.style)};return void 0!==t.text&&(e.text={...t.text}),void 0!==t.props&&(e.props=JSON.parse(JSON.stringify(t.props))),e}function oo(t){return void 0===t.pressure?{time:t.time,price:t.price}:{time:t.time,price:t.price,pressure:t.pressure}}function so(t){const e={...t};return void 0!==t.levels&&(e.levels=t.levels.map(t=>({...t}))),e}function lo(t){return JSON.stringify({[Zr]:{version:2,drawings:t.map(t=>({tool:t.tool,points:t.points.map(oo),style:t.style??{},...void 0===t.text?{}:{text:t.text},...void 0===t.props?{}:{props:t.props},paneIndex:t.paneIndex,zIndex:Number.isFinite(t.zIndex)?t.zIndex:0,...void 0===t.locked?{}:{locked:t.locked},...void 0===t.visible?{}:{visible:t.visible}}))}})}const ao=t=>"object"==typeof t&&null!==t&&!Array.isArray(t),co=t=>"number"==typeof t&&Number.isFinite(t),ho=t=>"string"==typeof t&&t.length<=to,uo=t=>"__proto__"===t||"constructor"===t||"prototype"===t,po=(t,e)=>"string"==typeof t&&e.includes(t),fo=["text","fontSize","fontFamily","fontWeight","fontStyle","background","backgroundColor","backgroundOpacity","border","borderColor","wrap","wrapWidth","textAlign","textVAlign","textPosition","fontColor"];function xo(t){if(!Array.isArray(t)||t.length>64)return null;const e=[];for(const i of t)if(co(i))e.push({ratio:i});else{if(!ao(i)||!co(i.ratio))return null;{const t={ratio:i.ratio};ho(i.color)&&(t.color=i.color),"boolean"==typeof i.enabled&&(t.enabled=i.enabled),ho(i.label)&&(t.label=i.label),e.push(t)}}return e}function yo(t){if(!ao(t)||!ho(t.value))return null;const e={value:t.value};for(const i of["color","fontFamily","backgroundColor","borderColor"]){const n=t[i];ho(n)&&(e[i]=n)}for(const i of["fontSize","wrapWidth","backgroundOpacity"]){const n=t[i];co(n)&&(e[i]=n)}for(const i of["bold","italic","wrap","background","border"]){const n=t[i];"boolean"==typeof n&&(e[i]=n)}return po(t.align,["left","center","right"])&&(e.align=t.align),po(t.valign,["top","middle","bottom"])&&(e.valign=t.valign),po(t.position,["inside","outside"])&&(e.position=t.position),e}function go(t,e=0){if(!ao(t))return null;const i={};let n=0;for(const[r,o]of Object.entries(t)){if(uo(r))continue;if(++n>64)return null;const t=mo(o,e);void 0!==t&&(i[r]=t)}return i}function mo(t,e){if("boolean"==typeof t||co(t))return t;if("string"==typeof t)return t.length<=to?t:void 0;if(Array.isArray(t)){if(t.length>64||e+1>=3)return;const i=[];for(const n of t){const t=mo(n,e+1);if(void 0===t)return;i.push(t)}return i}if(ao(t)){if(e+1>=3)return;return go(t,e+1)??void 0}}function bo(t){if(!ao(t))return null;if("string"!=typeof t.tool||""===t.tool||t.tool.length>to)return null;if(!ci(t.tool))return null;const e=function(t){if(!Array.isArray(t)||0===t.length||t.length>2e4)return null;const e=[];for(const i of t){if(!ao(i)||!co(i.time)||!co(i.price))return null;const t={time:i.time,price:i.price};co(i.pressure)&&i.pressure>=0&&i.pressure<=1&&(t.pressure=i.pressure),e.push(t)}return e}(t.points);if(null===e)return null;const i=function(t){if(void 0===t)return{};if(!ao(t))return null;const e={};let i=0;for(const[n,r]of Object.entries(t))if(!uo(n)){if(++i>64)return null;if(!fo.includes(n))switch(n){case"color":case"fillColor":ho(r)&&(e[n]=r);break;case"lineWidth":case"fillOpacity":case"accountSize":case"risk":co(r)&&(e[n]=r);break;case"lineStyle":po(r,["solid","dashed","dotted"])&&(e[n]=r);break;case"fill":case"extendLeft":case"extendRight":case"showLabels":case"showStats":case"pressure":"boolean"==typeof r&&(e[n]=r);break;case"levels":{const t=xo(r);null!==t&&(e[n]=t);break}default:if("string"==typeof r){if(r.length>to)return null;e[n]=r}else if("boolean"==typeof r||co(r))e[n]=r;else if(Array.isArray(r)){if(r.length>64)return null;r.every(co)&&(e[n]=r.slice())}}}return e}(t.style);if(null===i)return null;const n=void 0===t.paneIndex?0:t.paneIndex;if(!co(n)||n<0||!Number.isInteger(n))return null;if(void 0!==t.locked&&"boolean"!=typeof t.locked)return null;if(void 0!==t.visible&&"boolean"!=typeof t.visible)return null;const r=yo(t.text)??(ao(t.style)?function(t){return"string"!=typeof t.text?null:yo({value:t.text,color:t.fontColor,fontSize:t.fontSize,fontFamily:t.fontFamily,bold:"bold"===t.fontWeight||void 0,italic:"italic"===t.fontStyle||void 0,align:t.textAlign,valign:t.textVAlign,wrap:t.wrap,wrapWidth:t.wrapWidth,background:t.background,backgroundColor:t.backgroundColor,backgroundOpacity:t.backgroundOpacity,border:t.border,borderColor:t.borderColor,position:t.textPosition})}(t.style):null),o=void 0===t.props?null:go(t.props),s={tool:t.tool,points:e,style:i,paneIndex:n,zIndex:co(t.zIndex)?t.zIndex:0};return null!==r&&(s.text=r),null!==o&&(s.props=o),void 0!==t.locked&&(s.locked=t.locked),void 0!==t.visible&&(s.visible=t.visible),co(t.createdAt)&&(s.createdAt=t.createdAt),s}function vo(t){if("string"!=typeof t||0===t.length)return null;let e;try{e=JSON.parse(t)}catch{return null}if(!ao(e))return null;const i=e[Zr];if(!ao(i))return null;if(!co(i.version)||i.version<1||i.version>2)return null;const n=i.drawings;if(!Array.isArray(n)||0===n.length||n.length>512)return null;for(const t of n)if(null===bo(t))return null;const r=Jr(n).drawings;if(r.length!==n.length)return null;const o=[];for(const t of r){const e=bo(t);if(null===e)return null;o.push(e)}return o}class wo{constructor(t={}){Object.defineProperty(this,"_port",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_fallback",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_lastError",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._port=void 0===t.port?no():t.port,this._fallback=t.fallbackToMemory??!0}setPort(t){this._port=t}setFallbackToMemory(t){this._fallback=t}lastError(){return this._lastError}async write(t){if(0===t.length)return!1;let e;try{e=lo(t)}catch(t){return this._lastError=String(t),!1}this._lastError=null;let i=!1;if(null!==this._port)try{await this._port.writeText(e),i=!0}catch(t){this._lastError=String(t)}else this._lastError="openalgo-charts: no system clipboard available";return this._fallback?(eo=e,!0):i}async read(){if(this._lastError=null,null!==this._port)try{const t=vo(await this._port.readText());if(null!==t)return t}catch(t){this._lastError=String(t)}return this._fallback?vo(eo):null}}const Mo=Math.PI/4;let _o=1;const ko=(t,e)=>t.length===e.length&&t.every((t,i)=>t===e[i]);function So(t){return!0===t?"strong":"weak"===t||"strong"===t?t:"off"}const Po=t=>!0===t.modifiers?.shift||!0===t.shiftKey,To="openalgo-charts/drawing-link";function Lo(t,e,i){if(JSON.stringify(e)===JSON.stringify(i))return t;if(null===e||null===i||"object"!=typeof e||"object"!=typeof i||Array.isArray(e)||Array.isArray(i))return i;const n=e,r=i,o={...t};for(const t of new Set([...Object.keys(n),...Object.keys(r)]))JSON.stringify(n[t])!==JSON.stringify(r[t])&&(t in r?o[t]=Lo(o[t],n[t],r[t]):delete o[t]);return o}class Fo{constructor(t,e={}){Object.defineProperty(this,"_chart",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_opts",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_clipboard",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_layers",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_drawings",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_linkedPreviews",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_destroyed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_tool",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_pending",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_pendingPane",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_selection",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_hovered",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_lifted",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_shift",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_pointerKind",{enumerable:!0,configurable:!0,writable:!0,value:"mouse"}),Object.defineProperty(this,"_undo",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_redo",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_pendingHistory",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_dragStart",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_off",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_lastCursor",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_lastBar",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._chart=t,this._opts={magnet:So(e.magnet),stayInDrawingMode:e.stayInDrawingMode??!1,historyLimit:e.historyLimit??50,pasteOffsetBars:e.pasteOffsetBars??2,pasteOffsetPixels:e.pasteOffsetPixels??16,defaultStyle:e.defaultStyle??{}},this._clipboard=new wo({...void 0===e.clipboard?{}:{port:e.clipboard},...void 0===e.clipboardFallbackToMemory?{}:{fallbackToMemory:e.clipboardFallbackToMemory}}),this._off.push(t.on("click",t=>this._onClick(t))),this._off.push(t.on("crosshair:move",t=>this._onCrosshair(t))),this._off.push(t.on("hover",t=>this._onHover(t))),this._off.push(t.on("drag",t=>this._onDrag(t))),this._off.push(t.on("drag:end",()=>this._onDragEnd())),this._off.push(t.on("drag:cancel",()=>{this.cancelDrag()})),this._off.push(t.on("data:context",()=>{this.cancelDrag()})),this._off.push(t.on("dblclick",()=>{this.finish()})),this._off.push(t.on("drawings:restore",t=>this.fromJSON(t)));const i=t.drawingState();null!=i&&(this._drawings=Jr(i).drawings),this._sync()}setTool(t){if(null!==t&&!ci(t))throw new Error(`openalgo-charts: unknown drawing tool "${t}"`);this.cancelDrag(),this._tool=t,this._pending=[],this._setPlacementMode(null!==t),this._syncPreview(),this._syncSnapRing(),this._chart.emit("draw:tool",{tool:t})}_setPlacementMode(t){const e=this._chart;e.setPlacementMode?.(t)}activeTool(){return this._tool}setOptions(t){const{clipboard:e,magnet:i,...n}=t;this._opts={...this._opts,...n,defaultStyle:t.defaultStyle??this._opts.defaultStyle,magnet:void 0===i?this._opts.magnet:So(i)},void 0!==e&&this._clipboard.setPort(e),this._syncSnapRing()}magnetMode(){return this._opts.magnet}hovered(){return this._hovered}clipboard(){return this._clipboard}drawings(){return this._drawings}get(t){return this._drawings.find(e=>e.id===t)}get isDestroyed(){return this._destroyed}applyLinkedDrawing(t,e){if(this._destroyed||!0===this._chart.isDestroyed)return;this._dragStart?.items.some(e=>e.id===t)&&this.cancelDrag();const i=this._drawings.findIndex(e=>e.id===t);if(this._linkedPreviews.delete(t),null===e){if(i<0)return;this._drawings.splice(i,1),this._pruneSelection()}else{const n=ro({...e,id:t});i<0?this._drawings.push(n):this._drawings[i]=n}this._sync(),this._chart.emit("drawing:change",{ids:[t],kind:null===e?"remove":i<0?"add":"update",linked:!0})}setLinkedPreview(t,e){this._destroyed||!0===this._chart.isDestroyed||(null===e?this._linkedPreviews.delete(t):void 0!==this.get(t)&&this._linkedPreviews.set(t,ro({...e,id:t})),this._sync())}reorderLinkedDrawings(t){if(this._destroyed||!0===this._chart.isDestroyed)return;const e=[...new Set(t)].map(t=>this.get(t)).filter(t=>void 0!==t),i=new Set(e.map(t=>t.id));let n=0;const r=this._drawings.map(t=>i.has(t.id)?e[n++]:t);r.every((t,e)=>t===this._drawings[e])||(this._drawings=r,this._sync(),this._chart.emit("drawing:change",{ids:e.map(t=>t.id),kind:"reorder",linked:!0}))}alertInfo(t){const e=this.get(t);if(!e||!ci(e.tool))return{available:!1,reason:"Drawing is unavailable",levels:[]};const i=ai(e.tool);if(!i.alertValue)return{available:!1,reason:"This tool has no numeric alert value",paneIndex:e.paneIndex,levels:[]};const n=i.alertLevels?.(e)??[{id:"line",title:"Line"}];return this._chart.timeToCoordinate&&this._chart.priceToCoordinate&&this._chart.coordinateToPrice?{available:n.length>0,reason:n.length?void 0:"No active drawing levels",paneIndex:e.paneIndex,levels:n.map(t=>({...t}))}:{available:!1,reason:"Drawing coordinate maps are unavailable",levels:[],paneIndex:e.paneIndex}}valueAt(t,e,i){const n=this.get(t);if(!n||!ci(n.tool)||!Number.isFinite(e))return;const r=ai(n.tool),o=this._chart.timeToCoordinate,s=this._chart.coordinateToPrice;if(!r.alertValue||!o||!s)return;const l=[];for(const t of n.points){const e=this._toPixel(t,n.paneIndex);if(!e)return;l.push(e)}const a=o.call(this._chart,e);if(!Number.isFinite(a))return;const c=r.alertValue({drawing:n,pts:l,time:e,x:a,fromY:t=>s.call(this._chart,t,n.paneIndex)},i);return c&&Number.isFinite(c.price)&&(void 0===c.upperPrice||Number.isFinite(c.upperPrice))?{...c,paneIndex:n.paneIndex}:void 0}add(t){this._pushUndo();const e=this._insert(t);return this._sync(),this._chart.emit("draw:add",{drawing:e}),this._emitChange([e.id],"add"),e}_insert(t){const e=ai(t.tool);let i=t.id??this._mintId();for(;void 0===t.id&&void 0!==this.get(i);)i=this._mintId();const{id:n,...r}=t,o={...r,id:i,style:{...this._opts.defaultStyle,...e.defaultStyle,...t.style},zIndex:Number.isFinite(t.zIndex)?t.zIndex:0,createdAt:t.createdAt??Date.now()};return void 0!==o.props?.[To]&&(o.props={...o.props},delete o.props[To]),void 0===t.text&&void 0===e.defaultText||(o.text={value:"",...e.defaultText,...t.text}),this._drawings.push(o),o}_mintId(){return"d"+_o++}update(t,e){const i=this.get(t);return void 0!==i&&(this._pushUndo(),this._applyPatch(i,e),this._sync(),this._chart.emit("draw:update",{drawing:i}),this._emitChange([t],"update"),!0)}updateMany(t){const e=t.map(t=>({d:this.get(t.id),patch:t.patch})).filter(t=>void 0!==t.d);if(0!==e.length){this._pushUndo();for(const{d:t,patch:i}of e)this._applyPatch(t,i);this._sync();for(const{d:t}of e)this._chart.emit("draw:update",{drawing:t});this._emitChange(e.map(t=>t.d.id),"update")}}_applyPatch(t,e){if(void 0!==e.points){const i=e.points.map(t=>({...t})),n=ci(t.tool)?ai(t.tool):void 0;t.points=void 0===n?.constrain?i:n.constrain(i,function(t,e){if(t.length!==e.length)return null;let i=null;for(let n=0;n<e.length;n++)if(t[n].time!==e[n].time||t[n].price!==e[n].price){if(null!==i)return null;i=n}return i}(t.points,i))}void 0!==e.style&&(t.style={...t.style,...e.style}),void 0!==e.text&&(t.text={...t.text,...e.text}),void 0!==e.props&&(t.props={...t.props,...e.props}),void 0!==e.locked&&(t.locked=e.locked),void 0!==e.visible&&(t.visible=e.visible),void 0!==e.zIndex&&Number.isFinite(e.zIndex)&&(t.zIndex=e.zIndex)}remove(t){return this._removeIds([t],!0).length>0}removeMany(t){this._removeIds(t,!0)}_removeIds(t,e){const i=new Set(t),n=this._drawings.filter(t=>i.has(t.id));if(0===n.length)return[];e&&this._pushUndo(),this._drawings=this._drawings.filter(t=>!i.has(t.id)),this._selection=this._selection.filter(t=>!i.has(t)),this._sync();for(const t of n)this._chart.emit("draw:remove",{drawing:t});return this._emitChange(n.map(t=>t.id),"remove"),n}clear(){0!==this._drawings.length&&(this._removeIds(this._drawings.map(t=>t.id),!0),this._setSelection([]))}select(t,e=!1){const i=null===t?[]:"string"==typeof t?[t]:t,n=[];for(const t of i)void 0===this.get(t)||n.includes(t)||n.push(t);if(!e)return void this._setSelection(n);const r=this._selection.slice();for(const t of n){const e=r.indexOf(t);e>=0?r.splice(e,1):r.push(t)}this._setSelection(r)}selected(){return 0===this._selection.length?null:this._selection[0]}selection(){return this._selection}_setSelection(t){const e=!ko(t,this._selection);this._selection=t;for(const e of this._layers.values())e.bottom.setSelected(t),e.top.setSelected(t);e&&(this._chart.emit("draw:select",{id:this.selected()}),this._chart.emit("drawing:select",{ids:t.slice()}))}_emitChange(t,e){null!==this._pendingHistory&&(this._pendingHistory.after=JSON.stringify(this._drawings),this._pendingHistory=null),this._chart.emit("drawing:change",{ids:t.slice(),kind:e})}setZIndex(t,e){const i=this.get(t);void 0!==i&&Number.isFinite(e)&&i.zIndex!==e&&(this._pushUndo(),i.zIndex=e,this._sync(),this._chart.emit("draw:update",{drawing:i}),this._emitChange([t],"reorder"))}bringToFront(t){const e=this.get(t);if(void 0===e)return;const i=this._band(e),n=0===i.length?e.zIndex:Math.max(...i.map(t=>t.zIndex));this._reorder(e,n,"end")}sendToBack(t){const e=this.get(t);if(void 0===e)return;const i=this._band(e),n=0===i.length?e.zIndex:Math.min(...i.map(t=>t.zIndex));this._reorder(e,n,"start")}sendBehindSeries(t){const e=this.get(t);void 0!==e&&e.zIndex>=0&&this.setZIndex(t,-1)}bringAboveSeries(t){const e=this.get(t);void 0!==e&&e.zIndex<0&&this.setZIndex(t,0)}_band(t){const e=t.zIndex<0;return this._drawings.filter(i=>i!==t&&i.paneIndex===t.paneIndex&&i.zIndex<0===e)}_reorder(t,e,i){const n=this._drawings.indexOf(t);n===("end"===i?this._drawings.length-1:0)&&t.zIndex===e||(this._pushUndo(),t.zIndex=e,this._drawings.splice(n,1),"end"===i?this._drawings.push(t):this._drawings.unshift(t),this._sync(),this._chart.emit("draw:update",{drawing:t}),this._emitChange([t.id],"reorder"))}nudge(t,e,i){if(0===e&&0===i)return;const n=this._targets(t).filter(t=>!0!==t.locked);if(0!==n.length){this._pushUndo();for(const t of n)t.points=t.points.map(n=>({time:this._offsetTime(n.time,e),price:this._offsetPrice(n.price,t.paneIndex,i)}));this._sync();for(const t of n)this._chart.emit("draw:update",{drawing:t});this._emitChange(n.map(t=>t.id),"update")}}duplicate(t){const e=this._targets(t);if(0===e.length)return[];this._pushUndo();const i=e.map(t=>{const{id:e,createdAt:i,...n}=ro(t);return this._insert({...n,points:this._offsetPoints(t.points,t.paneIndex)})});this._sync();for(const t of i)this._chart.emit("draw:add",{drawing:t});return this._emitChange(i.map(t=>t.id),"add"),this.select(i.map(t=>t.id)),i}async copy(t){const e=this._targets(t);if(0===e.length)return!1;const i=await this._clipboard.write(e);return i&&this._chart.emit("draw:copy",{drawings:e.map(ro)}),i}async cut(t){const e=this._targets(t);if(0===e.length)return!1;if(!await this._clipboard.write(e))return!1;const i=this._removeIds(e.map(t=>t.id),!0);return 0!==i.length&&(this._chart.emit("draw:cut",{drawings:i}),!0)}async paste(){const t=await this._clipboard.read();if(null===t||0===t.length)return[];for(const e of t)if(!ci(e.tool))return[];const e=t.map(t=>{const e=this._clampPane(t.paneIndex);return{...t,paneIndex:e,points:this._offsetPoints(t.points,e)}});this._pushUndo();const i=e.map(t=>this._insert(t));this._sync();for(const t of i)this._chart.emit("draw:add",{drawing:t});return this._emitChange(i.map(t=>t.id),"add"),this._chart.emit("draw:paste",{drawings:i}),this.select(i.map(t=>t.id)),i}_targets(t){const e=null==t?this._selection:"string"==typeof t?[t]:t,i=[];for(const t of e){const e=this.get(t);void 0===e||i.includes(e)||i.push(e)}return i}_clampPane(t){const e=this._chart.panes;if(void 0===e)return t;const i=e.call(this._chart).length;return 0===i?0:Math.min(t,i-1)}_offsetPoints(t,e){const i=this._barSeconds()*this._opts.pasteOffsetBars,n=this._opts.pasteOffsetPixels;return t.map(t=>({time:t.time+i,price:this._offsetPrice(t.price,e,n)}))}_offsetPrice(t,e,i){if(0===i)return t;const n=this._chart.priceToCoordinate,r=this._chart.coordinateToPrice;if(void 0===n||void 0===r)return t;const o=n.call(this._chart,t,e);if(null===o||!Number.isFinite(o))return t;const s=r.call(this._chart,o+i,e);return null!==s&&Number.isFinite(s)?s:t}_offsetTime(t,e){if(0===e)return t;const i=this._chart.timeToCoordinate,n=this._chart.coordinateToTime;if(void 0!==i&&void 0!==n){const r=i.call(this._chart,t);if(Number.isFinite(r)){const t=n.call(this._chart,r+e);if(Number.isFinite(t))return t}}return t+e/8*this._barSeconds()}undo(){this._onDragEnd();const t=this._undo.pop();return void 0!==t&&(this._redo.push(t),this._applyHistory(t.after,t.before,"undo"),!0)}redo(){this._onDragEnd();const t=this._redo.pop();return void 0!==t&&(this._undo.push(t),this._applyHistory(t.before,t.after,"redo"),!0)}_applyHistory(t,e,i){const n=JSON.parse(t),r=JSON.parse(e),o=new Map(n.map(t=>[t.id,t])),s=new Map(r.map(t=>[t.id,t])),l=n.filter(t=>s.has(t.id)).map(t=>t.id),a=r.filter(t=>o.has(t.id)).map(t=>t.id),c=[...new Set([...o.keys(),...s.keys()])].filter(t=>JSON.stringify(o.get(t))!==JSON.stringify(s.get(t))||l.indexOf(t)!==a.indexOf(t)),h=new Set(c),d=new Map(this._drawings.map(t=>[t.id,t]));this._drawings=this._drawings.filter(t=>!h.has(t.id)||s.has(t.id)).map(t=>h.has(t.id)?Lo(t,o.get(t.id),s.get(t.id)):t);for(let t=0;t<r.length;t++){const e=r[t];if(!h.has(e.id)||d.has(e.id)||o.has(e.id))continue;const i=r.slice(t+1).find(t=>void 0!==this.get(t.id)),n=void 0===i?this._drawings.length:this._drawings.findIndex(t=>t.id===i.id);this._drawings.splice(n,0,e)}const u=a.filter(t=>l.indexOf(t)!==a.indexOf(t)).map(t=>this.get(t)).filter(t=>void 0!==t),p=new Set(u.map(t=>t.id));let f=0;this._drawings=this._drawings.map(t=>p.has(t.id)?u[f++]:t),this._pruneSelection(),this._sync();for(const t of c){const e=this.get(t),i=d.get(t);void 0!==e?this._chart.emit(void 0===i?"draw:add":"draw:update",{drawing:e,history:!0}):void 0!==i&&this._chart.emit("draw:remove",{drawing:i,history:!0})}this._emitChange(c,i)}_pruneSelection(){const t=this._selection.filter(t=>void 0!==this.get(t));ko(t,this._selection)||this._setSelection(t)}canUndo(){return this._undo.length>0}canRedo(){return this._redo.length>0}toJSON(){return{version:2,drawings:this._drawings.map(ro)}}fromJSON(t){this.cancelDrag(),this._linkedPreviews.clear(),this._drawings=Jr(t).drawings,this._undo=[],this._redo=[],this._pendingHistory=null,this._setSelection([]),this._sync(),this._chart.emit("draw:restore",{})}destroy(){if(!this._destroyed){this.cancelDrag(),this._destroyed=!0,this._linkedPreviews.clear(),this._chart.emit("draw:destroy",{controller:this}),this._setPlacementMode(!1);for(const t of this._off)t();this._off.length=0;for(const t of this._layers.values())t.top.setBelow(null),this._chart.removePrimitive(t.top),this._chart.removePrimitive(t.bottom);this._layers.clear()}}_onCrosshair(t){const e=t.time??null,i=e??(t.point?this._chart.coordinateToTime?.(t.point.x)??null:null),n=t.price??null,r=t.paneIndex??null;this._lastCursor=null!==i&&Number.isFinite(i)&&null!==n&&null!==r?{time:i,price:n,paneIndex:r}:null;const o=t.bar??null;if(this._lastBar=null===o||null===e?null:{time:e,...o},this._shift=Po(t),this._notePointer(t),null===i&&null===n&&this._setHovered(null),null!==this._tool&&!0===t.pressed&&this._isFreehand()&&null!==i&&null!==n&&null!==r&&Number.isFinite(i)&&Number.isFinite(n))for(const e of this._coalesced(t,{time:i,price:n},r))this._inkPoint(e,r);else this._syncSnapRing(),null!==this._tool&&this._pending.length>0&&this._syncPreview()}_onHover(t){const e=t.id??null;this._setHovered(null!==e&&e.startsWith("draw:")?e.slice(5).split("#")[0]:null)}_setHovered(t){if(t!==this._hovered){this._hovered=t;for(const e of this._layers.values())e.bottom.setHovered(t),e.top.setHovered(t);this._chart.emit("drawing:hover",{id:t})}}_notePointer(t){const e=(t=>"touch"===t.pointerType||"pen"===t.pointerType?t.pointerType:"mouse")(t);if(e!==this._pointerKind){this._pointerKind=e;for(const t of this._layers.values())t.bottom.setPointerType(e),t.top.setPointerType(e)}}_coalesced(t,e,i){const n=t.samples,r={...e,...this._pressureOf(t.pressure)},o=this._chart.coordinateToTime,s=this._chart.coordinateToPrice;if(!Array.isArray(n)||n.length<2||void 0===o||void 0===s||null===t.point||void 0===t.point)return[r];const l=n[n.length-1],a=t.point.y-l.y;if(!Number.isFinite(a))return[r];const c=[];for(let t=0;t<n.length-1;t++){const e=n[t],r=o.call(this._chart,e.x),l=s.call(this._chart,e.y+a,i);null!==l&&Number.isFinite(r)&&Number.isFinite(l)&&c.push({time:r,price:l,...this._pressureOf(e.pressure)})}return c.push({...r,...this._pressureOf(l.pressure??t.pressure)}),c}_pressureOf(t){return"number"==typeof t&&Number.isFinite(t)&&.5!==t?{pressure:Math.min(1,Math.max(0,t))}:{}}_expand(t,e){if(void 0===t.expand)return e;const i=this._chart.getVisibleLogicalRange(),n=null===i?60:Math.max(1,i.to-i.from),r=this._pendingPane,o=void 0!==this._chart.timeToCoordinate&&void 0!==this._chart.priceToCoordinate&&void 0!==this._chart.coordinateToTime&&void 0!==this._chart.coordinateToPrice,s=t.expand(e,{barSeconds:this._barSeconds(),visibleBars:n,...o?{toPixel:t=>this._toPixel(t,r),fromPixel:t=>this._fromPixel(t,r)}:{}});return void 0===t.constrain?s:t.constrain(s,null)}_barSeconds(){const t=this._chart.dataLayer,e=t.baseIndex,i=e>0?t.indexToTime(e-1):void 0,n=e>=0?t.indexToTime(e):void 0;return void 0!==i&&void 0!==n&&n>i?n-i:60}_isFreehand(){return null!==this._tool&&!0===ai(this._tool).freehand}_inkPoint(t,e){if(0===this._pending.length)this._pendingPane=e;else{if(e!==this._pendingPane)return;{const e=this._pending[this._pending.length-1];if(e.time===t.time&&e.price===t.price)return}}this._pending.push(t),this._syncPreview()}_commit(t){const e=ai(this._tool),i=this.add({tool:e.id,points:t,style:{},paneIndex:this._pendingPane});this._pending=[],this._opts.stayInDrawingMode||(this._tool=null,this._setPlacementMode(!1)),this._syncPreview(),this._syncSnapRing(),this.select(i.id),this._chart.emit("draw:tool",{tool:this._tool})}_finishFreehand(){const t=this._pending;this._pending=[],t.length<2?this._syncPreview():this._commit(this._thinStroke(t,this._pendingPane))}_thinStroke(t,e){const i=[];for(const n of t){const r=this._toPixel(n,e);if(null===r)return t;i.push(r)}const n=ft(i,1.5),r=[];let o=0;for(const e of n){for(;o<i.length&&(i[o].x!==e.x||i[o].y!==e.y);)o++;if(o>=i.length)return t;r.push(t[o]),o++}return r}finish(){if(null===this._tool||0===this._pending.length)return!1;const t=ai(this._tool);if(0!==t.points||!0===t.freehand)return!1;const e=this._pending;return e.length<2?(this._pending=[],this._syncPreview(),!1):(this._commit(e),!0)}cancel(){if(this.cancelDrag())return!0;if(null===this._tool)return!1;const t=this._pending.length>0;return this._pending=[],t&&this._opts.stayInDrawingMode?(this._syncPreview(),!0):(this._tool=null,this._setPlacementMode(!1),this._syncPreview(),this._syncSnapRing(),this._chart.emit("draw:tool",{tool:null}),!0)}popAnchor(){if(null===this._tool||0===this._pending.length)return!1;const t=ai(this._tool);return 0===t.points&&!0!==t.freehand&&(this._pending.pop(),this._syncPreview(),!0)}_onClick(t){if(this._notePointer(t),null!==this._tool){if(this._isFreehand())return void(!0===t.viaDrag&&this._finishFreehand());if(!0===t.viaDrag&&0===this._pending.length)return;if(null===t.price||!Number.isFinite(t.price)||!Number.isFinite(t.time))return;return this._shift=Po(t),void this._placePoint(this._aimPoint({time:t.time,price:t.price},t.paneIndex),t.paneIndex)}const e=!0===t.shiftKey||!0===t.ctrlKey||!0===t.metaKey;null!==t.id&&t.id.startsWith("draw:")?this.select(t.id.slice(5).split("#")[0],e):null!==t.id||e||this.select(null)}_placePoint(t,e){0===this._pending.length&&(this._pendingPane=e),this._pending.push(t);const i=ai(this._tool);i.points>0&&this._pending.length>=i.points?this._commit(this._expand(i,this._pending)):this._syncPreview()}_aimPoint(t,e){const i=this._lockedPoint(t,e);return null!==i?i:this._snapPoint(t,e)??t}_lockedPoint(t,e){return this._shift&&null!==this._tool&&1===this._pending.length?!0!==ai(this._tool).angleLock||e!==this._pendingPane?null:this._lockAngle(this._pending[0],t,e):null}_snapPoint(t,e){const i=this._opts.magnet;if("off"===i||0!==e)return null;const n=this._lastBar;if(null===n)return null;const r=[n.open,n.high,n.low,n.close];if("strong"===i){let e=r[0],i=1/0;for(const n of r){const r=Math.abs(n-t.price);r<i&&(i=r,e=n)}return{time:n.time,price:e}}const o=this._chart.priceToCoordinate;if(void 0===o)return null;const s=o.call(this._chart,t.price,e);if(null===s||!Number.isFinite(s))return null;let l=null,a=8;for(const t of r){const i=o.call(this._chart,t,e);if(null===i||!Number.isFinite(i))continue;const n=Math.abs(i-s);n<=a&&(a=n,l=t)}return null===l?null:{time:n.time,price:l}}_onDrag(t){if(!t.id.startsWith("draw:"))return;const[e,i]=t.id.slice(5).split("#"),n=this.get(e);if(void 0===n||!0===n.locked)return;const r=void 0===i?null:Number(i);if(this._notePointer(t),this._shift=Po(t),null===this._dragStart||this._dragStart.id!==e||this._dragStart.handle!==r){null!==r||this._selection.includes(e)||this.select(e);const i=null===r?this._targets(this._selection).filter(t=>!0!==t.locked):[n],o=this._undo.slice(),s=this._redo.slice();this._pushUndo(),this._dragStart={id:e,handle:r,undo:o,redo:s,from:{time:t.fromTime??t.time,price:t.fromPrice??t.price},items:i.map(t=>({id:t.id,paneIndex:t.paneIndex,points:t.points.map(t=>({...t}))}))};let l=!1;for(const t of i)t.zIndex<0&&(this._lifted.add(t.id),l=!0);return this._moveDrag(t,n,r),l?this._sync():this._syncDrag(),void this._emitDragPreview()}this._moveDrag(t,n,r),this._syncDrag(),this._emitDragPreview()}_emitDragPreview(){const t=this._dragStart?.items.map(t=>this.get(t.id)).filter(t=>void 0!==t)??[];this._chart.emit("draw:preview",{drawings:t.map(ro)})}cancelDrag(){const t=this._dragStart;if(null===t)return!1;this._dragStart=null;for(const e of t.items){const t=this.get(e.id);void 0!==t&&(t.points=e.points.map(t=>({...t})))}return this._undo=t.undo,this._redo=t.redo,this._pendingHistory=null,this._lifted.clear(),!0!==this._chart.isDestroyed&&this._sync(),this._chart.emit("draw:preview-clear",{ids:t.items.map(t=>t.id)}),!0}_moveDrag(t,e,i){const n=this._dragStart;if(null===i){const e=t.time-n.from.time,i=t.price-n.from.price,r=this._pixelDelta(n.from.price,t.price,t.paneIndex);for(const o of n.items){const n=this.get(o.id);if(void 0===n)continue;const s=o.paneIndex===t.paneIndex;n.points=o.points.map(t=>({...t,time:t.time+e,price:s?t.price+i:this._offsetPrice(t.price,o.paneIndex,r)}))}}else if(i>=0&&i<e.points.length){const r=n.items[0];let o={time:t.time,price:t.price};this._shift&&2===r.points.length&&ci(e.tool)&&!0===ai(e.tool).angleLock&&(o=this._lockAngle(r.points[1-i],o,e.paneIndex)??o);const s=r.points.map((t,e)=>e===i?{...t,...o}:{...t}),l=ci(e.tool)?ai(e.tool):void 0;e.points=void 0===l?.constrain?s:l.constrain(s,i)}}_pixelDelta(t,e,i){const n=this._chart.priceToCoordinate;if(void 0===n)return 0;const r=n.call(this._chart,t,i),o=n.call(this._chart,e,i);return null!==r&&null!==o&&Number.isFinite(r)&&Number.isFinite(o)?o-r:0}_toPixel(t,e){const i=this._chart.timeToCoordinate,n=this._chart.priceToCoordinate;if(void 0===i||void 0===n)return null;const r=i.call(this._chart,t.time),o=n.call(this._chart,t.price,e);return null!==o&&Number.isFinite(r)&&Number.isFinite(o)?{x:r,y:o}:null}_fromPixel(t,e){const i=this._chart.coordinateToTime,n=this._chart.coordinateToPrice;if(void 0===i||void 0===n)return null;const r=i.call(this._chart,t.x),o=n.call(this._chart,t.y,e);return null!==o&&Number.isFinite(r)&&Number.isFinite(o)?{time:r,price:o}:null}_lockAngle(t,e,i){const n=this._toPixel(t,i),r=this._toPixel(e,i);if(null===n||null===r)return null;const o=r.x-n.x,s=r.y-n.y;if(0===o&&0===s)return null;const l=Math.round(Math.atan2(s,o)/Mo)*Mo,a=Math.cos(l),c=Math.sin(l),h=o*a+s*c;return this._fromPixel({x:n.x+h*a,y:n.y+h*c},i)}_onDragEnd(){if(null===this._dragStart)return;const t=this._dragStart.items.map(t=>this.get(t.id)).filter(t=>void 0!==t);this._dragStart=null,this._chart.emit("draw:preview-clear",{ids:t.map(t=>t.id)}),this._lifted.size>0&&(this._lifted.clear(),this._sync());for(const e of t)this._chart.emit("draw:update",{drawing:e});t.length>0&&this._emitChange(t.map(t=>t.id),"update")}_layerFor(t){let e=this._layers.get(t);return void 0===e&&(e={bottom:new Ir("bottom"),top:new Ir("top")},this._chart.addPrimitive(e.bottom,t),this._chart.addPrimitive(e.top,t),e.top.setBelow(e.bottom),e.bottom.setSelected(this._selection),e.top.setSelected(this._selection),this._layers.set(t,e)),e}_onTop(t){return t.zIndex>=0||this._lifted.has(t.id)}_sync(){const t=new Map;for(const e of this._drawings){const i=this._linkedPreviews.get(e.id)??e;let n=t.get(i.paneIndex);void 0===n&&(n={below:[],above:[]},t.set(i.paneIndex,n)),(this._onTop(i)?n.above:n.below).push(i)}for(const[e,i]of t){const t=this._layerFor(e);t.bottom.setDrawings(i.below),t.top.setDrawings(i.above)}for(const[e,i]of this._layers)t.has(e)||(i.bottom.setDrawings([]),i.top.setDrawings([])),i.bottom.setSelected(this._selection),i.top.setSelected(this._selection);null!==this._hovered&&void 0===this.get(this._hovered)&&this._setHovered(null),this._chart.setDrawingState(this.toJSON())}_syncDrag(){const t=this._dragStart;if(null===t)return;const e=new Set(t.items.map(t=>t.paneIndex));for(const t of e){const e=this._layers.get(t);void 0!==e&&e.top.setDrawings(this._drawings.filter(e=>e.paneIndex===t&&this._onTop(e)))}this._chart.setDrawingState(this.toJSON())}_syncPreview(){for(const t of this._layers.values())t.top.setPreview(null);if(null===this._tool||0===this._pending.length)return;const t=this._lastCursor,e=null===t||t.paneIndex!==this._pendingPane?this._pending:[...this._pending,this._aimPoint({time:t.time,price:t.price},t.paneIndex)];this._layerFor(this._pendingPane).top.setPreview({id:"__preview",tool:this._tool,points:e,style:this._opts.defaultStyle,paneIndex:this._pendingPane,zIndex:0})}_syncSnapRing(){const t=this._lastCursor;let e=null,i=t?.paneIndex??this._pendingPane;null===t||null===this._tool||this._isFreehand()||null!==this._lockedPoint({time:t.time,price:t.price},t.paneIndex)||(e=this._snapPoint({time:t.time,price:t.price},t.paneIndex),i=t.paneIndex);for(const[t,n]of this._layers)n.top.setSnapPoint(t===i?e:null);null===e||this._layers.has(i)||this._layerFor(i).top.setSnapPoint(e)}_pushUndo(){this._onDragEnd();const t=JSON.stringify(this._drawings);this._pendingHistory={before:t,after:t},this._undo.push(this._pendingHistory),this._undo.length>this._opts.historyLimit&&this._undo.shift(),this._redo=[]}}function Io(t){return"string"==typeof t?.symbol&&t.symbol.trim()&&"string"==typeof t.exchange&&t.exchange.trim()?JSON.stringify([t.symbol,t.exchange]):null}function Ao(t){try{return("function"==typeof t?t():t)??void 0}catch{return}}let No=1;const Co=(()=>{const t=globalThis.crypto;if("function"==typeof t?.randomUUID)return t.randomUUID();if("function"!=typeof t?.getRandomValues)throw new Error("Drawing links require Web Crypto");return Array.from(t.getRandomValues(new Uint8Array(16)),t=>t.toString(16).padStart(2,"0")).join("")})();let zo=1;function Oo(t,e){const i=t.props?.[To];return 1===i?.version&&i.context===e&&"string"==typeof i.id&&i.id.length>0?i.id:void 0}class Do{constructor(t={}){Object.defineProperty(this,"_members",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_enabled",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_broadcasting",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_destroyed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),this._enabled=t.enabled??!1}options(){return{enabled:this._enabled}}add(t,e,i){if(this._destroyed||!0===t.isDestroyed||e.isDestroyed)return;const n=this._members.get(t);if(n?.controller===e)return void(void 0!==i&&(n.resolveContext="function"==typeof i?i:null,this.setContext(t,Ao(i))));void 0!==n&&this.remove(t);const r={chart:t,controller:e,context:Io(Ao(i??(()=>t.getDataContext?.()))),resolveContext:"function"==typeof i?i:null,drawings:new Map,off:[]};this._members.set(t,r);for(const e of["draw:add","draw:update","draw:remove"])r.off.push(t.on(e,t=>{const i=t;void 0!==i?.drawing&&this._change(r,i.drawing,"draw:remove"===e,"draw:add"===e&&!0!==i.history)}));r.off.push(t.on("draw:preview",t=>{const e=t?.drawings;void 0!==e&&this._preview(r,e)}),t.on("draw:preview-clear",()=>this._clearMemberPreviews(r)),t.on("drawing:change",t=>{!0!==t?.linked&&this._syncOrder(r)}),t.on("data:context",e=>this.setContext(t,null===r.resolveContext?e:Ao(r.resolveContext))),t.on("symbol",()=>this.setContext(t,Ao(r.resolveContext??void 0))),t.on("draw:restore",()=>{this._detachDrawings(r,!1),this._reconnect(r)}),t.on("draw:destroy",i=>{i?.controller===e&&this.remove(t)}),t.on("destroy",()=>this.remove(t))),this._reconnect(r)}setOptions(t){if(void 0!==t.enabled&&!this._destroyed){if(!t.enabled)for(const t of this._members.values())this._clearMemberPreviews(t);if(this._enabled=t.enabled,this._enabled)for(const t of this._members.values())this._reconnect(t)}}setContext(t,e){const i=this._members.get(t);if(void 0===i)return;const n=Io(e);for(const t of i.drawings.values())this._clearPreview(t);n!==i.context&&(i.controller.cancelDrag(),this._detachDrawings(i,!0),i.context=n,this._reconnect(i))}share(t,e){const i=this._members.get(t);if(void 0===i||!this._eligible(i)||this._broadcasting)return 0;let n=0;for(const t of new Set(e??i.controller.drawings().map(t=>t.id))){const e=i.controller.get(t);if(void 0===e||0!==e.paneIndex)continue;const r=i.drawings.get(t)??this._create(i,e);this._commit(r,i,e,!0),n++}return this._syncOrder(i),n}has(t){return this._members.has(t)}remove(t){const e=this._members.get(t);if(void 0!==e){this._detachDrawings(e,!1);for(const t of e.off)t();this._members.delete(t)}}destroy(){this._destroyed=!0;for(const t of[...this._members.keys()])this.remove(t)}_eligible(t){return!this._destroyed&&this._enabled&&null!==t.context&&this._members.get(t.chart)===t&&!0!==t.chart.isDestroyed&&!t.controller.isDestroyed}_create(t,e){const i=t.context,n=Oo(e,i)??`${Co}:${zo++}`;for(const r of this._members.values())for(const o of r.drawings.values())if(o.context===i&&o.lineage===n)return o.bindings.set(t,{id:e.id,local:!0}),t.drawings.set(e.id,o),o;const r={id:e.id,context:i,lineage:n,drawing:ro(e),bindings:new Map([[t,{id:e.id,local:!0}]]),previewFrom:null};return t.drawings.set(e.id,r),r}_reconnect(t){if(this._broadcasting||!this._eligible(t))return;const e=t.context,i=new Set;this._broadcasting=!0;try{for(const n of[...t.controller.drawings()]){if(0!==n.paneIndex||t.drawings.has(n.id))continue;const r=Oo(n,e);if(void 0!==r)for(const o of this._members.values()){if(o===t||!this._eligible(o)||o.context!==e)continue;let s=[...o.drawings.values()].find(t=>t.context===e&&t.lineage===r);if(void 0===s){const t=o.controller.drawings().find(t=>0===t.paneIndex&&Oo(t,e)===r);void 0!==t&&(s=this._create(o,t))}if(void 0!==s){s.bindings.set(t,{id:n.id,local:!1}),t.drawings.set(n.id,s),t.controller.applyLinkedDrawing(n.id,s.drawing),i.add(o);break}}}for(const e of i)this._orderFrom(e,t)}finally{this._broadcasting=!1}}_change(t,e,i,n){if(this._broadcasting||!this._eligible(t)||0!==e.paneIndex)return;let r=t.drawings.get(e.id);const o=void 0===r&&n;void 0===r&&n&&(r=this._create(t,e)),void 0!==r&&this._commit(r,t,i?null:e,o)}_commit(t,e,i,n){this._broadcasting=!0;try{this._clearPreview(t);const r=null===i?null:{...i,props:{...i.props,[To]:{version:1,id:t.lineage,context:t.context}}};null!==r&&Oo(i,t.context)!==t.lineage&&e.controller.applyLinkedDrawing(i.id,r),t.drawing=null===r?null:ro({...r,id:t.id});for(const r of[...this._members.values()]){if(r===e||!this._eligible(r)||r.context!==t.context)continue;let o=t.bindings.get(r);if(void 0===o){if(null===i||!n)continue;const e=r.controller.drawings().find(e=>0===e.paneIndex&&!r.drawings.has(e.id)&&Oo(e,t.context)===t.lineage);let s=e?.id??t.id;if(void 0===e)for(;void 0!==r.controller.get(s)||r.drawings.has(s);)s="linked"+No++;o={id:s,local:!1},t.bindings.set(r,o),r.drawings.set(s,t)}r.controller.applyLinkedDrawing(o.id,t.drawing)}}finally{this._broadcasting=!1}}_preview(t,e){if(!this._broadcasting&&this._eligible(t))for(const i of e){const e=t.drawings.get(i.id);if(void 0!==e){e.previewFrom=t;for(const[n,r]of e.bindings)n!==t&&this._eligible(n)&&n.context===e.context&&n.controller.setLinkedPreview(r.id,i)}}}_syncOrder(t){if(!this._broadcasting&&this._eligible(t)){this._broadcasting=!0;try{for(const e of this._members.values())e!==t&&this._eligible(e)&&e.context===t.context&&this._orderFrom(t,e)}finally{this._broadcasting=!1}}}_orderFrom(t,e){const i=t.controller.drawings().map(i=>t.drawings.get(i.id)?.bindings.get(e)?.id).filter(t=>void 0!==t);e.controller.reorderLinkedDrawings(i)}_clearPreview(t){if(null!==t.previewFrom){for(const[e,i]of t.bindings)e.controller.setLinkedPreview(i.id,null);t.previewFrom=null}}_clearMemberPreviews(t){for(const e of t.drawings.values())e.previewFrom===t&&this._clearPreview(e)}_detachDrawings(t,e){for(const i of t.drawings.values()){const n=i.bindings.get(t);this._clearPreview(i),i.bindings.delete(t),e&&!1===n?.local&&t.controller.applyLinkedDrawing(n.id,null)}t.drawings.clear()}}function $o(t={}){return new Do(t)}const Ho="0 0 24 24",Wo=2,Bo={viewBox:Ho,fill:"none",stroke:"currentColor",strokeWidth:2,strokeLinecap:"round",strokeLinejoin:"round"},Vo={cursor:"M12 3v6M12 15v6M3 12h6M15 12h6",magnet:"M6 4v8a6 6 0 0 0 12 0V4M6 9h4M14 9h4","trend-line":"M4 20 20 4",ray:"M4 20 20 4M2 18 6 22","extended-line":"M2 22 22 2",arrow:"M4 20 18 6M18 6h-6M18 6v6","horizontal-line":"M2 12h20","horizontal-ray":"M6 12H22M4 10V14","vertical-line":"M12 2v20","cross-line":"M2 12h20M12 2v20","trend-angle":"M4 20 18 8M4 20h12M8 20a8 8 0 0 0 2-5","info-line":"M4 20 20 4M8 8h8",path:"M2 18 8 8l4 6 4-10",polyline:"M2 18 8 8l4 6 4-10 4 4","parallel-channel":"M2 16 14 4M8 22 20 10","disjoint-channel":"M2 16 14 6M8 22 22 14","flat-bottom":"M2 14 14 4M2 20h20","fib-channel":"M2 14 14 4M4 18 16 8M6 22 18 12","regression-trend":"M2 18 20 6M4 20 22 8M2 14 20 2","flat-top-bottom":"M3 6h18M3 20 21 12M3 6v14","regression-channel":"M3 18 19 6M5 21 21 9M3 12 17 2M10 16h4",pitchfork:"M4 21 16 9M4 9 16 21M10 15 22 3M4 9 10 3M16 21l6-6","schiff-pitchfork":"M4 19 18 5M4 11 12 19M8 15 20 3M4 11l6-6M12 19l8-8","modified-schiff-pitchfork":"M4 20 20 4M4 12 12 20M8 16 20 4M4 12l8-8M12 20l8-8","inside-pitchfork":"M4 20 20 4M6 10 14 18M6 10l6-6M14 18l6-6","fib-extension-two-point":"M3 4h18M3 10h18M3 16h18M3 21h18M6 4v12","fib-speed-resistance-fan":"M3 21V3M3 21h18M3 21 21 3M3 21 12 3M3 21 21 12","icon-stamp":"M12 3l3 6 6 3-6 3-3 6-3-6-6-3 6-3z",rectangle:"M3 5h18v14H3z","rotated-rectangle":"M2 14 10 4l12 6-8 10z",ellipse:"M12 5c5 0 9 3 9 7s-4 7-9 7-9-3-9-7 4-7 9-7z",circle:"M12 3a9 9 0 1 0 0 18 9 9 0 0 0 0-18z",triangle:"M12 4 21 20H3z",arc:"M3 19a12 12 0 0 1 18 0",curve:"M3 18c4-12 14-12 18 0","double-curve":"M3 18c3-9 7-9 9 0 2 9 6 9 9 0",brush:"M3 19c4 0 4-8 8-8s4 8 9 4",highlighter:"M4 16 14 6l4 4-10 10H4z","fib-retracement":"M3 4h18M3 9h18M3 14h18M3 19h18","fib-extension":"M3 5h18M3 12h18M3 19h18M8 5v14","fib-fan":"M3 20 21 4M3 20 21 10M3 20 21 16M3 20h18","fib-time-zone":"M4 3v18M8 3v18M14 3v18M22 3v18","fib-circles":"M12 20A5 5 0 0 1 12 10M12 20A9 9 0 0 1 12 2M10 20h4","fib-spiral":"M12 13A3 3 0 1 1 15 10A7 7 0 1 1 8 3A9 9 0 1 1 21 12","fib-wedge":"M3 20 21 4M3 20 21 12M3 20a12 12 0 0 0 10-6","fib-speed-fan":"M3 20V4M3 20h18M3 20 21 4M3 20 13 4","gann-fan":"M3 20 21 2M3 20 21 9M3 20 21 15M3 20h18","gann-box":"M3 3h18v18H3zM3 3l18 18M3 21 21 3","trend-fib-time":"M3 20 8 8 12 15M12 3v18M16 3v18M22 3v18","fib-speed-resistance-arcs":"M3 21a6 6 0 0 0 6-6M3 21a12 12 0 0 0 12-12M3 21a18 18 0 0 0 18-18M3 21 21 3","gann-square":"M3 3h18v18H3zM9 3v18M15 3v18M3 9h18M3 15h18M3 21 21 3","dedekind-tessellation":"M3 21a9 9 0 0 1 18 0M3 21a4 4 0 0 1 8 0M13 21a4 4 0 0 1 8 0M12 3v18",sonic:"M3 12a9 9 0 0 1 18 0M6 12a6 6 0 0 1 12 0M9 12a3 3 0 0 1 6 0M3 12h18",supersonic:"M3 12 21 3M3 12l18 9M7 12a5 5 0 0 1 10 0M10 12a3 3 0 0 1 6 0","golden-sonic":"M3 12a9 9 0 0 1 18 0M7 12a5 5 0 0 1 10 0M10 12a2 2 0 0 1 4 0M3 16h18M12 16v5","golden-supersonic":"M3 12 21 3M3 12l18 9M6 12a6 6 0 0 1 12 0M10 12a2 2 0 0 1 4 0M18 11v2","xabcd-pattern":"M3 19 7 4 12 16 16 8 21 20M3 19 12 16 21 20M7 4 16 8","abcd-pattern":"M3 18 9 4 15 15 21 3M3 18 15 15M9 4 21 3","elliott-impulse":"M2 21 6 12 9 17 13 5 17 11 22 2","elliott-correction":"M3 4 9 18 15 8 21 21M3 4h4M17 21h4","head-shoulders":"M2 20 5 10 8 17 12 3 16 17 19 10 22 20M3 17h18",gartley:"M3 20 7 3 12 13 16 7 21 17M3 20 12 13 21 17",bat:"M3 21 7 3 12 11 16 6 21 19M7 3 16 6M3 21 21 19",butterfly:"M3 16 7 3 12 14 16 7 21 21M3 16 12 14 21 21",crab:"M3 12 7 3 12 10 16 5 21 22M3 12 12 10 21 22",shark:"M3 17 7 10 12 20 16 3 21 18M3 17 21 18M7 10 16 3",cypher:"M3 20 7 9 12 16 16 3 21 18M3 20 12 16M7 9 16 3",measure:"M4 16h16M4 12v8M20 12v8M8 4h8M12 4v6","anchored-vwap":"M4 4v16M4 15 8 12 12 14 16 8 20 6M2 4h4","fixed-range-volume-profile":"M4 3v18M4 5h8v3H4M4 10h16v3H4M4 15h12v3H4","price-range":"M12 4v16M8 8l4-4 4 4M8 16l4 4 4-4","date-range":"M4 12h16M8 8l-4 4 4 4M16 8l4 4-4 4","date-price-range":"M4 6h16v12H4zM4 12h16M12 6v12","cyclic-lines":"M4 4v16M10 4v16M16 4v16M22 4v16","time-cycles":"M4 12a4 4 0 0 1 8 0 4 4 0 0 0 8 0M4 4v16","sine-line":"M2 12c3-9 6-9 9 0 3 9 6 9 9 0","long-position":"M3 15h18v5H3zM3 5h18v5H3zM12 10v5","short-position":"M3 5h18v5H3zM3 15h18v5H3zM12 10v5",forecast:"M3 18 9 10l4 4 8-10M13 4h8v8","risk-reward-long":"M3 14h18v6H3zM3 4h18v6H3zM12 10v4M6 7h4","risk-reward-short":"M3 4h18v6H3zM3 14h18v6H3zM12 10v4M6 17h4",text:"M4 5h16M12 5v14M8 19h8",note:"M4 20 9 15M9 5h13v10H9zM4 20v-4",callout:"M3 4h18v11H3zM8 15l-2 5 6-5",balloon:"M3 4h18v12H9l-4 4v-4H3z",comment:"M3 5h18v10H3zM6 15v4l4-4",signpost:"M12 21V9M5 3h14v6H5z","price-label":"M3 12 8 7h13v10H8z","price-note":"M2 12h5M7 6h15v12H7zM10 12h9",table:"M3 5h18v14H3zM3 10h18M9 10v9M15 10v9","flag-mark":"M6 21V3M6 3h12l-3 4 3 4H6","arrow-up":"M12 3v18M6 9l6-6 6 6","arrow-down":"M12 3v18M6 15l6 6 6-6","arrow-left":"M3 12h18M9 6l-6 6 6 6","arrow-right":"M3 12h18M15 6l6 6-6 6"};function Ro(t){return Vo[t]}function jo(){return Object.keys(Vo)}const Eo="0 0 16 16",Uo=1.5,Jo={viewBox:Eo,fill:"none",stroke:"currentColor",strokeWidth:1.5,strokeLinecap:"round",strokeLinejoin:"round"},qo={cursor:"M8 2v4M8 10v4M2 8h4M10 8h4",magnet:"M3 2v6a5 5 0 0 0 10 0V2M3 5h3M10 5h3",lock:"M3 7h10v7H3zM5 7V5a3 3 0 0 1 6 0v2",unlock:"M3 7h10v7H3zM5 7V5a3 3 0 0 1 6 0",eye:"M1 8c2-4 4-6 7-6s5 2 7 6c-2 4-4 6-7 6s-5-2-7-6zM10 8a2 2 0 1 1-4 0 2 2 0 0 1 4 0","eye-off":"M1 8c2-4 4-6 7-6s5 2 7 6c-2 4-4 6-7 6s-5-2-7-6zM2 2l12 12",star:"M8 1l2 5h5l-4 3 1 5-4-3-4 3 1-5-4-3h5z","star-filled":"M8 1 12 14 1 6h14L4 14z",trash:"M2 4h12M6 4V2h4v2M3 4l1 10h8l1-10",settings:"M2 4h12M2 8h12M2 12h12M5 2v4M11 6v4M7 10v4",undo:"M3 6h7a4 4 0 0 1 0 8H6M6 3 3 6l3 3",redo:"M13 6H6a4 4 0 0 0 0 8h4M10 3l3 3-3 3",copy:"M6 6h8v8H6zM10 6V2H2v8h4",paste:"M4 3H3v11h10V3h-1M6 2h4v2H6z",duplicate:"M6 6h8v8H6zM10 6V2H2v8h4M10 8v4M8 10h4",front:"M2 9h6v5H2zM11 14V2M8 5l3-3 3 3",back:"M2 2h6v5H2zM11 2v12M8 11l3 3 3-3",text:"M3 3h10M8 3v10M6 13h4","chevron-down":"M3 6l5 5 5-5","chevron-right":"M6 3l5 5-5 5",close:"M3 3l10 10M13 3 3 13",plus:"M8 2v12M2 8h12",minus:"M2 8h12",search:"M7 2a5 5 0 1 0 0 10 5 5 0 0 0 0-10zM11 11l3 3",grid:"M2 2h12v12H2zM2 8h12M8 2v12",link:"M6 10l4-4M7 4l1-1a3 3 0 0 1 4 4l-1 1M9 12l-1 1a3 3 0 0 1-4-4l1-1",unlink:"M7 4l1-1a3 3 0 0 1 4 4l-1 1M9 12l-1 1a3 3 0 0 1-4-4l1-1M4 4l8 8",camera:"M2 5h3l1-2h4l1 2h3v8H2zM8 11a2 2 0 1 0 0-4 2 2 0 0 0 0 4z",download:"M8 2v9M4 7l4 4 4-4M2 14h12"},Yo=new Set(["star-filled"]);function Ko(t){return qo[t]}function Xo(){return Object.keys(qo)}const Go="http://www.w3.org/2000/svg",Zo="oac-icon-";function Qo(t){return String(t).replace(/[&<>"]/g,t=>"&"===t?"&":"<"===t?"<":">"===t?">":""")}function ts(t,e,i){const n=t[e];if(void 0===n)throw new Error(`openalgo-charts: no ${i} icon "${e}"`);return n}function es(t){return Number(t.viewBox.split(" ")[2])}function is(t,e,i,n){const r=Qo(n.size??es(e)),o=Qo(n.stroke??e.strokeWidth),s=void 0!==n.className&&""!==n.className?` class="${Qo(n.className)}"`:"";return`<svg xmlns="${Go}" viewBox="${Qo(e.viewBox)}" width="${r}" height="${r}" fill="${Qo(i)}" stroke="${Qo(e.stroke)}" stroke-width="${o}" stroke-linecap="${Qo(e.strokeLinecap)}" stroke-linejoin="${Qo(e.strokeLinejoin)}"${s} aria-hidden="true">${t}</svg>`}function ns(t,e={}){return is(`<path d="${Qo(ts(Vo,t,"tool"))}"/>`,Bo,Bo.fill,e)}function rs(t,e={}){const i=ts(qo,t,"chrome"),n=Yo.has(t)?"currentColor":Jo.fill;return is(`<path d="${Qo(i)}"/>`,Jo,n,e)}function os(t=Object.keys(Vo)){let e="";for(const i of new Set(t)){const t=ts(Vo,i,"tool");e+=`<symbol id="${Qo(Zo+i)}" viewBox="${Qo(Bo.viewBox)}"><path d="${Qo(t)}"/></symbol>`}return`<svg xmlns="${Go}" style="display:none" aria-hidden="true">${e}</svg>`}function ss(t,e={}){return ts(Vo,t,"tool"),is(`<use href="#${Qo(Zo+t)}"/>`,Bo,Bo.fill,e)}function ls(t,e={}){const i=ts(Vo,t,"tool"),n=e.size??20;if(!(Number.isFinite(n)&&n>0&&n<=128))throw new RangeError(`openalgo-charts: cursor size ${n} is outside 1..128`);const[r,o]=e.hotspot??[n/2,n/2],s=e.color??"#fff",l=e.halo??function(t){const e=/^#([0-9a-f]{3,8})$/i.exec(t.trim());if(!e)return"#000";const i=e[1],n=i.length<6?i.split("").map(t=>t+t).join(""):i;return(.2126*parseInt(n.slice(0,2),16)+.7152*parseInt(n.slice(2,4),16)+.0722*parseInt(n.slice(4,6),16))/255>.5?"#000":"#fff"}(s),a=e.fallback??"crosshair",c=es(Bo)/n,h=Math.round(100*(2+2*c))/100,d=`<svg xmlns="${Go}" viewBox="${Bo.viewBox}" width="${n}" height="${n}" fill="none" stroke-linecap="${Bo.strokeLinecap}" stroke-linejoin="${Bo.strokeLinejoin}"><path d="${i}" stroke="${Qo(l)}" stroke-width="${h}"/><path d="${i}" stroke="${Qo(s)}" stroke-width="2"/></svg>`;return`url("data:image/svg+xml,${encodeURIComponent(d)}") ${Math.round(r)} ${Math.round(o)}, ${a}`}const as=1,cs=10;function hs(t,e){if(e.editingText)return null;const i=t.key??"";if(""===i)return null;const n=!0===t.ctrlKey||!0===t.metaKey,r=!0===t.shiftKey;if(!0===t.altKey)return null;if(!0===e.placing&&!n&&!r){if("Escape"===i)return{type:"cancel"};if("Enter"===i)return{type:"finish"};if("Backspace"===i)return{type:"popAnchor"}}const o=e.hasSelection||e.hasTarget;if(n)switch(i.toLowerCase()){case"z":return r?{type:"redo"}:{type:"undo"};case"y":return r?null:{type:"redo"};case"c":return!r&&o?{type:"copy"}:null;case"x":return!r&&o?{type:"cut"}:null;case"v":return r?null:{type:"paste"};case"d":return!r&&o?{type:"duplicate"}:null;default:return null}if("Delete"===i||"Backspace"===i)return!r&&o?{type:"delete"}:null;if(!e.hasSelection)return null;const s=r?10:1;switch(i){case"ArrowLeft":return{type:"nudge",dx:-s,dy:0};case"ArrowRight":return{type:"nudge",dx:s,dy:0};case"ArrowUp":return{type:"nudge",dx:0,dy:-s};case"ArrowDown":return{type:"nudge",dx:0,dy:s};default:return null}}const ds="draw";Sr();export{Te as ADVANCED_GEOMETRY_TOOLS,ne as ADVANCED_LINE_TOOLS,H as ALIGN_OPTIONS,ri as ANCHORED_VWAP,cr as ARC,Ui as ARROW,pr as ARROW_DOWN,fr as ARROW_LEFT,xr as ARROW_RIGHT,ur as ARROW_UP,qn as BALLOON,vn as BRUSH,_r as BUILTIN_DRAWING_TOOLS,In as CALLOUT,qo as CHROME_ICONS,Jo as CHROME_ICON_ATTRS,Yo as CHROME_ICON_FILLED,Uo as CHROME_ICON_STROKE,Eo as CHROME_ICON_VIEWBOX,sr as CIRCLE,R as COLOR_FIELD,Kn as COMMENT,Ki as CROSS_LINE,hr as CURVE,r as CYCLE_PALETTE,kn as CYCLIC_LINES,rr as DATE_RANGE,h as DEFAULT_FIB,d as DEFAULT_FIB_FAN,f as DEFAULT_FIB_TIME_ZONE,u as DEFAULT_GANN_BOX,p as DEFAULT_GANN_FAN,_n as DOUBLE_CURVE,Zr as DRAWING_CLIPBOARD_KEY,Qr as DRAWING_CLIPBOARD_VERSION,Pr as DRAWING_STATE_VERSION,Vo as DRAWING_TOOL_ICONS,ds as DRAW_TIER,wo as DrawingClipboard,Fo as DrawingController,Ir as DrawingLayer,Do as DrawingLinkGroup,Zi as ELLIPSE,Ei as EXTENDED_LINE,q as EXTEND_FIELDS,mr as FIB_CHANNEL,rn as FIB_EXTENSION,vr as FIB_FAN,nn as FIB_RETRACEMENT,br as FIB_TIME_ZONE,J as FILL_FIELDS,oi as FIXED_RANGE_VOLUME_PROFILE,On as FLAG_MARK,X as FONT_FIELDS,V as FONT_OPTIONS,or as FORECAST,Mr as GANN_BOX,wr as GANN_FAN,yr as HIGHLIGHTER,Ji as HORIZONTAL_LINE,qi as HORIZONTAL_RAY,Bo as ICON_ATTRS,Wo as ICON_STROKE,Zo as ICON_SYMBOL_PREFIX,Ho as ICON_VIEWBOX,K as LEVEL_FIELDS,t as LEVEL_NEUTRAL,U as LINE_FIELDS,E as LINE_STYLE_FIELD,$ as LINE_STYLE_OPTIONS,j as LINE_WIDTH_FIELD,cn as LONG_POSITION,sn as MEASURE,Un as NOTE,as as NUDGE_STEP_PX,cs as NUDGE_STEP_SHIFT_PX,Qi as PARALLEL_CHANNEL,bn as PATH,je as PATTERN_DRAWING_TOOLS,tt as PLATE_TEXT_FIELDS,ar as POLYLINE,Fn as PRICE_LABEL,Qn as PRICE_NOTE,nr as PRICE_RANGE,ji as RAY,Gi as RECTANGLE,wn as ROTATED_RECTANGLE,Q as SHAPE_TEXT_FIELDS,hn as SHORT_POSITION,Y as SHOW_LABELS_FIELD,Gn as SIGNPOST,Pn as SINE_LINE,er as TABLE,yn as TEXT,Z as TEXT_FIELDS,B as TEXT_POSITION_OPTIONS,G as TEXT_VALUE_FIELD,Sn as TIME_CYCLES,Ri as TREND_LINE,lr as TRIANGLE,W as VALIGN_OPTIONS,Yi as VERTICAL_LINE,Ye as anchoredVwapAnalysis,ut as applyDrawingSettings,I as boundsOf,xt as catmullRom,Ko as chromeIcon,Xo as chromeIconIds,rs as chromeIconSvg,io as clearMemoryClipboard,ro as cloneDrawing,c as cloneLevels,dt as coerceSettingValue,et as composeSettings,$o as createDrawingLinkGroup,o as cycleColor,vo as decodeClipboardPayload,N as distToEllipse,S as distToLine,L as distToPolyline,A as distToRect,k as distToSegment,xi as drawingSettingsSchema,pi as drawingShortcuts,Ro as drawingToolIcon,jo as drawingToolIconIds,lo as encodeClipboardPayload,C as extendSegment,Ke as fixedRangeVolumeProfileAnalysis,s as formatRatio,l as gannLabel,ai as getDrawingTool,ci as hasDrawingTool,os as iconSprite,ns as iconSvg,ss as iconUse,hs as keyToDrawingAction,n as levelColor,ui as matchDrawingShortcut,Jr as migrateDrawings,yt as pressureWidth,ft as rdpSimplify,ot as readDrawingSetting,st as readDrawingSettings,F as rectOf,Sr as registerBuiltinDrawingTools,li as registerDrawingTool,hi as registeredDrawingTools,bo as sanitizeDrawing,Lr as sortByZIndex,no as systemClipboard,ls as toolCursor};
|
|
1
|
+
const t="#787b86",e=[[.236,"#f23645"],[.25,"#7e57c2"],[.382,"#ff9800"],[.5,"#4caf50"],[.618,"#089981"],[.75,"#26c6da"],[.786,"#00bcd4"],[1.272,"#2962ff"],[1.414,"#9c27b0"],[1.618,"#e91e63"],[2.618,"#ff5252"],[3.618,"#9575cd"],[4.236,"#64b5f6"]],i=1e-6;function n(n){if(!Number.isFinite(n))return t;for(const[t,r]of e)if(Math.abs(t-n)<=i)return r;return t}const r=["#2962ff","#f23645","#089981","#ff9800","#9c27b0","#00bcd4","#e91e63","#4caf50"];function o(t){const e=r.length,i=Number.isFinite(t)?Math.trunc(t):0;return r[(i%e+e)%e]}function s(t){return`${(100*t).toFixed(1)}%`}function l(t){if(!Number.isFinite(t)||t<=0)return String(t);if(t>=1)return`${t}x1`;const e=1/t;return Math.abs(e-Math.round(e))<=i?`1x${Math.round(e)}`:`${t}x1`}function a(t){for(const e of t)Object.freeze(e);return Object.freeze(t)}function c(t){return t.map(t=>({...t}))}const h=a([0,.236,.382,.5,.618,.786,1].map(t=>({ratio:t,color:n(t)}))),d=a([.236,.382,.5,.618,.786,1].map(t=>({ratio:t,color:n(t)}))),u=a([0,.25,.382,.5,.618,.75,1].map(t=>({ratio:t,color:n(t)}))),p=a([.125,.25,.5,1,2,4,8].map((t,e)=>({ratio:t,color:o(e),label:l(t)}))),f=a([0,1,2,3,5,8,13,21,34,55].map(t=>({ratio:t})));function x(t,e,i,n,r){if(t.x===e.x)return;const o=n?-1/0:t.x,s=r?1/0:e.x;return i<Math.min(o,s)-1e-7||i>Math.max(o,s)+1e-7?void 0:t.y+(e.y-t.y)*(i-t.x)/(e.x-t.x)}function y(t,e){if(void 0===e)return;const i=t.fromY(e);return null!==i&&Number.isFinite(i)?{price:i}:void 0}function g(t=!1,e=!1){return(i,n)=>{if(!(i.pts.length<2||void 0!==n&&"line"!==n))return y(i,x(i.pts[0],i.pts[1],i.x,i.drawing.style.extendLeft??t,i.drawing.style.extendRight??e))}}function m(t=!1){return(e,i)=>{if(!(!e.pts[0]||t&&e.x<e.pts[0].x||void 0!==i&&"line"!==i))return{price:e.drawing.points[0].price}}}function b(){return[{id:"band",title:"Channel band"},{id:"base",title:"First boundary"},{id:"boundary",title:"Second boundary"},{id:"middle",title:"Middle"}]}function v(t){return(e,i="band")=>{if(e.pts.length<("disjoint"===t?4:3))return;const[n,r,o]=e.pts,s=o.y-(n.y+r.y)/2,l="disjoint"===t?[o,e.pts[3]]:"flat"===t?[{x:n.x,y:o.y},{x:r.x,y:o.y}]:[{x:n.x,y:n.y+s},{x:r.x,y:r.y+s}],a="parallel"!==t&&!0===e.drawing.style.extendLeft,c="parallel"!==t&&!0===e.drawing.style.extendRight,h=(i,n)=>"parallel"!==t&&i.x>n.x?x(n,i,e.x,a,c):x(i,n,e.x,a,c),d=h(n,r),u=h(l[0],l[1]);if("base"===i)return y(e,d);if("boundary"===i)return y(e,u);if(void 0===d||void 0===u)return;if("middle"===i)return y(e,(d+u)/2);if("band"!==i)return;const p=y(e,d),f=y(e,u);return p&&f?{price:Math.min(p.price,f.price),upperPrice:Math.max(p.price,f.price)}:void 0}}function w(t,e){return(t.style.levels??e).filter(t=>!1!==t.enabled&&Number.isFinite(t.ratio))}function _(t){return e=>[...new Map(w(e,t).map(t=>{const e=`ratio:${t.ratio}`;return[e,{id:e,title:t.label??s(t.ratio)}]})).values()]}function M(t,e){return(i,n)=>{const r=w(i.drawing,e).find(t=>`ratio:${t.ratio}`===n);if(!r||i.pts.length<(2===t?2:3))return;const[o,s,l]=i.pts,a=!0===i.drawing.style.extendLeft,c=!0===i.drawing.style.extendRight;if("channel"===t){const t=t=>({x:t.x+(l.x-s.x)*r.ratio,y:t.y+(l.y-s.y)*r.ratio});return y(i,x(t(o),t(s),i.x,a,c))}const h=i.pts[t-1];if(!a&&i.x<Math.min(o.x,h.x)||!c&&i.x>Math.max(o.x,h.x))return;const d=i.drawing.points;return{price:(2===t?d[0].price:d[2].price)+(d[1].price-d[0].price)*r.ratio}}}function k(t,e,i,n){const r=n.x-i.x,o=n.y-i.y,s=r*r+o*o;if(0===s)return Math.hypot(t-i.x,e-i.y);let l=((t-i.x)*r+(e-i.y)*o)/s;return l=l<0?0:l>1?1:l,Math.hypot(t-(i.x+l*r),e-(i.y+l*o))}function S(t,e,i,n){const r=n.x-i.x,o=n.y-i.y,s=Math.hypot(r,o);return 0===s?Math.hypot(t-i.x,e-i.y):Math.abs(o*(t-i.x)-r*(e-i.y))/s}function P(t,e){return Math.abs(t-e)}function T(t,e){return Math.abs(t-e)}function L(t,e,i){let n=1/0;for(let r=1;r<i.length;r++){const o=k(t,e,i[r-1],i[r]);o<n&&(n=o)}return n}function F(t,e){return{x0:Math.min(t.x,e.x),y0:Math.min(t.y,e.y),x1:Math.max(t.x,e.x),y1:Math.max(t.y,e.y)}}function I(t){let e=1/0,i=1/0,n=-1/0,r=-1/0;for(const o of t)o.x<e&&(e=o.x),o.y<i&&(i=o.y),o.x>n&&(n=o.x),o.y>r&&(r=o.y);return{x0:e,y0:i,x1:n,y1:r}}function A(t,e,i,n,r){const o=F(i,n);if(t>=o.x0&&t<=o.x1&&e>=o.y0&&e<=o.y1&&r)return 0;const s=[{x:o.x0,y:o.y0},{x:o.x1,y:o.y0},{x:o.x1,y:o.y1},{x:o.x0,y:o.y1}];let l=1/0;for(let i=0;i<4;i++){const n=k(t,e,s[i],s[(i+1)%4]);n<l&&(l=n)}return l}function C(t,e,i,n,r){const o=F(i,n),s=(o.x0+o.x1)/2,l=(o.y0+o.y1)/2,a=Math.max(1e-6,(o.x1-o.x0)/2),c=Math.max(1e-6,(o.y1-o.y0)/2),h=(t-s)/a,d=(e-l)/c,u=Math.hypot(h,d);return r&&u<=1?0:Math.abs(u-1)*Math.min(a,c)}function N(t,e,i,n,r){const o=e.x-t.x;if(0===o)return[t,e];const s=(e.y-t.y)/o;return[n?{x:0,y:t.y+(0-t.x)*s}:t,r?{x:i,y:t.y+(i-t.x)*s}:e]}function z(t){const e=function(t){const e=t.trim();if(e.startsWith("#")){const t=e.slice(1);if(3===t.length||4===t.length){const[e,i,n,r]=[...t].map(t=>parseInt(t+t,16));return[e,i,n].some(Number.isNaN)?null:{r:e,g:i,b:n,a:4===t.length?r/255:1}}if(6===t.length||8===t.length){const e=parseInt(t.slice(0,6),16);if(Number.isNaN(e))return null;const i=8===t.length?parseInt(t.slice(6,8),16)/255:1;return{r:e>>16&255,g:e>>8&255,b:255&e,a:Number.isNaN(i)?1:i}}return null}const i=/^rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*(?:,\s*([\d.]+)\s*)?\)$/i.exec(e);return null===i?null:{r:Number(i[1]),g:Number(i[2]),b:Number(i[3]),a:void 0===i[4]?1:Number(i[4])}}(t);if(null===e)return.5;const i=t=>{const e=t/255;return e<=.03928?e/12.92:Math.pow((e+.055)/1.055,2.4)};return.2126*i(e.r)+.7152*i(e.g)+.0722*i(e.b)}function O(t){return z(t)>.45?"#10131a":"#ffffff"}function D(t,e,i,n,r,o){const s=t.roundRect;"function"==typeof s?s.call(t,e,i,n,r,Math.max(0,Math.min(o,n/2,r/2))):t.rect(e,i,n,r)}const $=[{value:"solid",label:"Solid"},{value:"dashed",label:"Dashed"},{value:"dotted",label:"Dotted"}],H=[{value:"left",label:"Left"},{value:"center",label:"Center"},{value:"right",label:"Right"}],W=[{value:"top",label:"Top"},{value:"middle",label:"Middle"},{value:"bottom",label:"Bottom"}],B=[{value:"inside",label:"Inside"},{value:"outside",label:"Outside"}],R=[{value:"ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif",label:"Sans"},{value:"ui-serif, Georgia, Times New Roman, serif",label:"Serif"},{value:"ui-monospace, Menlo, Consolas, monospace",label:"Mono"}],V={path:"style.color",label:"Color",kind:"color",group:"line"},j={path:"style.lineWidth",label:"Width",kind:"number",min:1,max:12,step:.5,group:"line"},E={path:"style.lineStyle",label:"Line style",kind:"lineStyle",options:$,group:"line"},U=[V,j,E],J=[{path:"style.fill",label:"Fill",kind:"boolean",group:"fill"},{path:"style.fillColor",label:"Fill color",kind:"color",group:"fill"},{path:"style.fillOpacity",label:"Fill opacity",kind:"opacity",min:0,max:1,step:.01,group:"fill"}],q=[{path:"style.extendLeft",label:"Extend left",kind:"boolean",group:"behavior"},{path:"style.extendRight",label:"Extend right",kind:"boolean",group:"behavior"}],Y={path:"style.showLabels",label:"Show labels",kind:"boolean",group:"behavior"},K=[{path:"style.levels",label:"Levels",kind:"levels",group:"levels"},{...Y,group:"levels"}],G=[{path:"text.color",label:"Text color",kind:"color",group:"text"},{path:"text.fontSize",label:"Font size",kind:"number",min:6,max:96,step:1,group:"text"},{path:"text.fontFamily",label:"Font",kind:"select",options:R,group:"text"},{path:"text.bold",label:"Bold",kind:"boolean",group:"text"},{path:"text.italic",label:"Italic",kind:"boolean",group:"text"}],X={path:"text.value",label:"Text",kind:"text",group:"text"},Z=[X,...G,{path:"text.align",label:"Align",kind:"select",options:H,group:"text"},{path:"text.valign",label:"Vertical align",kind:"select",options:W,group:"text"},{path:"text.wrap",label:"Wrap",kind:"boolean",group:"text"},{path:"text.wrapWidth",label:"Wrap width",kind:"number",min:40,max:2e3,step:10,group:"text"},{path:"text.background",label:"Background",kind:"boolean",group:"text"},{path:"text.backgroundColor",label:"Background color",kind:"color",group:"text"},{path:"text.backgroundOpacity",label:"Background opacity",kind:"opacity",min:0,max:1,step:.01,group:"text"},{path:"text.border",label:"Border",kind:"boolean",group:"text"},{path:"text.borderColor",label:"Border color",kind:"color",group:"text"}],Q=[X,...G,{path:"text.align",label:"Align",kind:"select",options:H,group:"text"},{path:"text.valign",label:"Vertical align",kind:"select",options:W,group:"text"},{path:"text.wrap",label:"Wrap",kind:"boolean",group:"text"},{path:"text.position",label:"Label position",kind:"select",options:B,group:"text"}],tt=[X,...G,{path:"text.backgroundColor",label:"Plate color",kind:"color",group:"text"},{path:"text.backgroundOpacity",label:"Plate opacity",kind:"opacity",min:0,max:1,step:.01,group:"text"},{path:"text.border",label:"Border",kind:"boolean",group:"text"},{path:"text.borderColor",label:"Border color",kind:"color",group:"text"}];function et(t,e={}){const i=new Set,n=[];for(const e of t)for(const t of Array.isArray(e)?e:[e])i.has(t.path)||(i.add(t.path),n.push(t));return!0===e.textIsContent?{fields:n,textIsContent:!0}:{fields:n}}const it=new Set(["style","text","props"]),nt=new Set(["locked","visible","zIndex"]);function rt(t){const e=t.split(".");return 1===e.length&&nt.has(e[0])?{root:e[0],key:null}:2===e.length&&it.has(e[0])&&""!==e[1]?{root:e[0],key:e[1]}:null}function ot(t,e){const i=rt(e);if(null===i)return;if(null===i.key)return t[i.root];const n=t[i.root],r=n?.[i.key];return Array.isArray(r)?r.map(t=>({...t})):r}function st(t,e){const i={};for(const n of e.fields)i[n.path]=ot(t,n.path)??n.defaultValue;return i}const lt=new Set(["solid","dashed","dotted"]);function at(t){return"boolean"==typeof t?t:"true"===t||1===t||"1"===t||"false"!==t&&0!==t&&"0"!==t&&void 0}function ct(t){const e="number"==typeof t?t:"string"==typeof t&&""!==t.trim()?Number(t):NaN;return Number.isFinite(e)?e:void 0}function ht(t){if("object"!=typeof t||null===t)return null;const e=t,i=ct(e.ratio);if(void 0===i)return null;const n={ratio:i};"string"==typeof e.color&&""!==e.color&&(n.color=e.color);const r=at(e.enabled);return void 0!==r&&(n.enabled=r),"string"==typeof e.label&&""!==e.label&&(n.label=e.label),n}function dt(t,e){if(null!=e)switch(t.kind){case"color":case"text":return"string"==typeof e?e:String(e);case"number":{const i=ct(e);if(void 0===i)return;const n=t.min??-1/0,r=t.max??1/0;return i<n?n:i>r?r:i}case"opacity":{const t=ct(e);return void 0===t?void 0:t<0?0:t>1?1:t}case"boolean":return at(e);case"lineStyle":return"string"==typeof e&<.has(e)?e:void 0;case"select":if("string"!=typeof e)return;return void 0===t.options||t.options.some(t=>t.value===e)?e:void 0;case"levels":{if(!Array.isArray(e))return;const t=[];for(const i of e){const e=ht(i);null!==e&&t.push(e)}return t}default:return}}function ut(t,e,i){const n=void 0===i?null:new Map(i.fields.map(t=>[t.path,t])),r={},o={};for(const i of Object.keys(e)){const s=rt(i);if(null===s)continue;let l=e[i];if(null!==n){const t=n.get(i);if(void 0===t)continue;l=dt(t,l)}if(null===s.key){if(void 0===l)continue;"zIndex"===s.root?"number"==typeof l&&Number.isFinite(l)&&(r.zIndex=l):"boolean"==typeof l&&(r[s.root]=l);continue}const a=o[s.root]??{...t[s.root]};o[s.root]=a,void 0===l?delete a[s.key]:a[s.key]=l}if(void 0!==o.style&&(r.style=o.style),void 0!==o.props&&(r.props=o.props),void 0!==o.text){const t=o.text;"string"!=typeof t.value&&(t.value=""),r.text=t}return r}function pt(t,e,i){return t<e?e:t>i?i:t}function ft(t,e){const i=t.length;if(!(e>0)||i<3)return t.map(t=>({x:t.x,y:t.y}));const n=new Uint8Array(i);n[0]=1,n[i-1]=1;const r=[0,i-1];for(;r.length>0;){const i=r.pop(),o=r.pop(),s=t[o],l=t[i];let a=-1,c=e;for(let e=o+1;e<i;e++){const i=k(t[e].x,t[e].y,s,l);i>c&&(c=i,a=e)}a<0||(n[a]=1,r.push(o,a,a,i))}const o=[];for(let e=0;e<i;e++)1===n[e]&&o.push({x:t[e].x,y:t[e].y});return o}function xt(t,e,i=.5){const n=e.length;if(n<2)return;if(t.moveTo(e[0].x,e[0].y),2===n)return void t.lineTo(e[1].x,e[1].y);const r=pt(i,0,1)/3;for(let i=0;i<n-1;i++){const o=e[0===i?0:i-1],s=e[i],l=e[i+1],a=e[i+2<n?i+2:n-1];t.bezierCurveTo(s.x+(l.x-o.x)*r,s.y+(l.y-o.y)*r,l.x-(a.x-s.x)*r,l.y-(a.y-s.y)*r,l.x,l.y)}}function yt(t,e,i=.6){return t*(1+2*((Number.isFinite(e)?pt(e,0,1):.5)-.5)*pt(i,0,1))}const gt=new WeakMap;function mt(t,e){const i=gt.get(t);if(!i)return t.measureText(e).width;const n=t.font+"\0"+e,r=i.get(n);if(void 0!==r)return r;const o=t.measureText(e).width;return i.size<1024&&i.set(n,o),o}const bt=(t,e)=>({x:(t.x+e.x)/2,y:(t.y+e.y)/2}),vt=(t,e,i)=>({x:t.x+(e.x-t.x)*i,y:t.y+(e.y-t.y)*i}),wt=t=>Number.isFinite(t.x)&&Number.isFinite(t.y);function _t(t,e){return{x:e.timeScale.indexToX(e.dataLayer.timeToIndexFloat(t.time)),y:e.priceScale.priceToY(t.price)}}function Mt(t,e,i,n,r){const o=t.props?.[e];return"number"==typeof o&&Number.isFinite(o)?Math.max(n,Math.min(r,o)):i}function kt(t,e){return(t.style.levels??e).filter(t=>!1!==t.enabled&&Number.isFinite(t.ratio))}function St(t,e,i,n=0,r=1){if(!wt(t)||!wt(e))return[];const o=e.x-t.x,s=e.y-t.y;if(0===o&&0===s)return t.x>=0&&t.x<=i.plotWidth&&t.y>=0&&t.y<=i.plotHeight?[t,t]:[];for(const[e,l,a]of[[t.x,o,i.plotWidth],[t.y,s,i.plotHeight]]){if(0===l){if(e<0||e>a)return[];continue}const t=-e/l,i=(a-e)/l;if((n=Math.max(n,Math.min(t,i)))>(r=Math.min(r,Math.max(t,i))))return[]}return[vt(t,e,n),vt(t,e,r)]}function Pt(t,e,i){if(t.x===e.x)return St(t,e,i.rc);const n=e.x>=t.x;return St(t,e,i.rc,!0===(n?i.drawing.style.extendLeft:i.drawing.style.extendRight)?-1/0:0,!0===(n?i.drawing.style.extendRight:i.drawing.style.extendLeft)?1/0:1)}function Tt(t,e){if(!t.every(wt))return[];let i=t.slice();for(const[t,n,r]of[["x",0,1],["x",e.plotWidth,-1],["y",0,1],["y",e.plotHeight,-1]]){const e=i;i=[];for(let o=0;o<e.length;o++){const s=e[o],l=e[(o+1)%e.length],a=(s[t]-n)*r>=0,c=(l[t]-n)*r>=0;a&&i.push(s),a!==c&&i.push(vt(s,l,(n-s[t])/(l[t]-s[t])))}}return i}function Lt(t,e,i){let n=!1;for(let r=0,o=i.length-1;r<i.length;o=r++){const s=i[r],l=i[o];s.y>e!=l.y>e&&t<(l.x-s.x)*(e-s.y)/(l.y-s.y)+s.x&&(n=!n)}return n}function Ft(t,e,i,n,r){const o=Math.max(2,Math.min(96,Math.ceil(Math.abs(r)*Math.max(Math.abs(e),Math.abs(i))/4)));return Array.from({length:o+1},(s,l)=>{const a=n+r*l/o;return{x:t.x+e*Math.cos(a),y:t.y+i*Math.sin(a)}})}const It=2*Math.PI;function At(t,e){return wt(t)&&Number.isFinite(t.x*e)&&Number.isFinite(t.y*e)}function Ct(t,e){const i=t.arc;return void 0===i?t.points.length>=2&&t.points.every(t=>At(t,e)):function(t){return wt(t.center)&&[t.rx,t.ry,t.start,t.sweep,t.start+t.sweep].every(Number.isFinite)&&t.rx>0&&t.ry>0}(i)&&At(i.center,e)&&Number.isFinite(i.rx*e)&&Number.isFinite(i.ry*e)}function Nt(t,e){return{x:t.center.x+t.rx*Math.cos(e),y:t.center.y+t.ry*Math.sin(e)}}function zt(t,e){if(0===e.sweep)return!1;const i=((t-e.start)*Math.sign(e.sweep)%It+It)%It;return Math.abs(e.sweep)>=It-1e-12||i<=Math.abs(e.sweep)+1e-12}function Ot(t,e,i){if(i.rx===i.ry&&Math.abs(i.sweep)>=It-1e-12)return Math.abs(Math.hypot(t-i.center.x,e-i.center.y)-i.rx);const n=Nt(i,i.start),r=Nt(i,i.start+i.sweep),o=Math.hypot(t-n.x,e-n.y),s=Math.hypot(t-r.x,e-r.y),l=Math.min(o,s);if(i.rx===i.ry)return zt(Math.atan2(e-i.center.y,t-i.center.x),i)?Math.abs(Math.hypot(t-i.center.x,e-i.center.y)-i.rx):l;const a=n=>{const r=Nt(i,i.start+i.sweep*n);return Math.hypot(t-r.x,e-r.y)};let c=l,h=s<o?32:0;for(let t=0;t<=32;t++){const e=a(t/32);e<c&&(c=e,h=t)}let d=Math.max(0,(h-1)/32),u=Math.min(1,(h+1)/32);const p=Math.min(80,Math.max(32,2*Math.ceil(Math.log2(Math.max(i.rx,i.ry)))+8));for(let t=0;t<p;t++){const t=d+(u-d)/3,e=u-(u-d)/3;a(t)<a(e)?u=e:d=t}return Math.min(c,a((d+u)/2))}function Dt(t,e,i){const n=(t-i.center.x)/i.rx,r=(e-i.center.y)/i.ry;if(n*n+r*r>1)return!1;if(Math.abs(i.sweep)>=It-1e-12)return!0;if(!0===i.sector)return zt(Math.atan2(r,n),i);const o=i.start+i.sweep/2;return n*Math.cos(o)+r*Math.sin(o)>=Math.cos(Math.abs(i.sweep)/2)}function $t(t,e){const{ctx:i,rc:n,style:r}=t,o=n.dpr;i.save(),i.strokeStyle=r.color,i.lineWidth=Math.max(1,r.lineWidth*o),i.lineJoin="round",i.lineCap="round",i.setLineDash("dashed"===r.lineStyle?[6*o,4*o]:"dotted"===r.lineStyle?[o,3*o]:[]);for(const t of e.paths)if(Ct(t,o)){if(i.beginPath(),void 0!==t.arc){const e=t.arc;!0===e.sector&&i.moveTo(e.center.x*o,e.center.y*o),e.rx===e.ry?i.arc(e.center.x*o,e.center.y*o,e.rx*o,e.start,e.start+e.sweep,e.sweep<0):i.ellipse(e.center.x*o,e.center.y*o,e.rx*o,e.ry*o,0,e.start,e.start+e.sweep,e.sweep<0)}else{i.moveTo(t.points[0].x*o,t.points[0].y*o);for(let e=1;e<t.points.length;e++)i.lineTo(t.points[e].x*o,t.points[e].y*o)}!0===t.closed&&i.closePath(),!0===t.fill&&!0===r.fill&&(i.save(),i.globalAlpha*=Math.max(0,Math.min(1,r.fillOpacity??.12)),i.fillStyle=r.fillColor??r.color,i.fill("evenodd"),i.restore()),!1!==t.stroke&&(i.strokeStyle=t.color??r.color,i.stroke())}const s=t.drawing.text,l=(s?.fontSize??11)*o;i.font=`${!0===s?.italic?"italic ":""}${!0===s?.bold?"700 ":""}${l}px ${s?.fontFamily||"ui-sans-serif, system-ui, sans-serif"}`,i.textAlign="left",i.textBaseline="bottom";const a=[],c=l/o*1.3;for(const t of e.labels??[]){if(!At(t.at,o))continue;let{x:e,y:h}=t.at;if(e>=0&&e<=n.plotWidth&&h>=0&&h<=n.plotHeight){const r=mt(i,t.text)/o;e=Math.max(0,Math.min(e,n.plotWidth-r));let s=!1;for(const t of[0,-1,1,-2,2,-3,3,-4]){const i=h+t*c;if(!(i<l/o||i>n.plotHeight)&&!a.some(t=>e<t.x+t.width+3&&e+r+3>t.x&&Math.abs(i-t.y)<c-.01)){h=i,a.push({x:e,y:h,width:r}),s=!0;break}}if(!s)continue}i.fillStyle=s?.color??t.color??r.color,i.fillText(t.text,e*o,h*o)}i.restore()}function Ht(t,e){return{...t,draw(t){const i=t.pts.map(e=>({x:e.x/t.rc.dpr,y:e.y/t.rc.dpr}));$t(t,e({pts:i,drawing:{...t.drawing,style:t.style},rc:t.rc}))},distance:(t,i,n)=>t<0||i<0||t>n.rc.plotWidth||i>n.rc.plotHeight?null:function(t,e,i,n,r=1){let o=1/0;for(const s of i.paths){if(!Ct(s,r))continue;if(void 0!==s.arc){const i=s.arc;if(!0===s.fill&&!0===n.style.fill&&(n.style.fillOpacity??.12)>0&&Dt(t,e,i))return 0;if(!1!==s.stroke&&(o=Math.min(o,Ot(t,e,i)),!0===i.sector||!0===s.closed)){const n=Nt(i,i.start),r=Nt(i,i.start+i.sweep);!0===i.sector?(o=Math.min(o,k(t,e,i.center,n)),!0===s.closed&&(o=Math.min(o,k(t,e,r,i.center)))):o=Math.min(o,k(t,e,r,n))}continue}const i=s.points;if(!0===s.fill&&!0===n.style.fill&&(n.style.fillOpacity??.12)>0&&Lt(t,e,i))return 0;if(!1!==s.stroke){for(let n=1;n<i.length;n++)o=Math.min(o,k(t,e,i[n-1],i[n]));!0===s.closed&&(o=Math.min(o,k(t,e,i[i.length-1],i[0])))}}return Number.isFinite(o)?o:null}(t,i,e(n),n.drawing,n.rc.dpr)}}const Wt=et([U,J,q]),Bt=[{ratio:0},{ratio:.5},{ratio:1}],Rt=[0,.382,.618,1,1.272,1.618,2,2.618,3.618,4.236].map(t=>({ratio:t})),Vt=[0,.382,.5,.618,1].map(t=>({ratio:t,color:n(t)})),jt=(t,e)=>({points:[t,e]});function Et(t,e,i){const n=e.x-t.x;if(0===n)return[t,e];const r=!0===i.drawing.style.extendLeft?0:Math.min(t.x,e.x),o=!0===i.drawing.style.extendRight?i.rc.plotWidth:Math.max(t.x,e.x),s=i=>({x:i,y:t.y+(e.y-t.y)*(i-t.x)/n});return n>0?[s(r),s(o)]:[s(o),s(r)]}function Ut(t,e){const[i,n]=t.pts,r=Pt(i,n,t),o=Pt(e[0],e[1],t),s=[];if(!0===t.drawing.style.fill){const r=Et(i,n,t),o=Et(e[0],e[1],t);s.push({points:Tt([r[0],r[1],o[1],o[0]],t.rc),closed:!0,fill:!0,stroke:!1})}return s.push({points:r},{points:o}),{paths:s}}const Jt=Ht({id:"disjoint-channel",name:"Disjoint Channel",points:4,alertValue:v("disjoint"),alertLevels:b,defaultStyle:{fill:!0},settings:Wt},t=>t.pts.length<4?{paths:[]}:Ut(t,t.pts.slice(2,4))),qt=Ht({id:"flat-top-bottom",name:"Flat Top/Bottom",points:3,alertValue:v("flat"),alertLevels:b,defaultStyle:{fill:!0},settings:Wt,constrain(t){const e=t.map(t=>({...t}));return e.length>=3&&(e[2].time=(e[0].time+e[1].time)/2),e}},t=>t.pts.length<3?{paths:[]}:Ut(t,[{x:t.pts[0].x,y:t.pts[2].y},{x:t.pts[1].x,y:t.pts[2].y}]));function Yt(t,e,i){let n=0,r=t.length;for(;n<r;){const o=n+r>>>1;t[o].time<e||i&&t[o].time===e?n=o+1:r=o}return n}const Kt=Ht({id:"regression-channel",name:"Regression Channel",points:2,defaultStyle:{fill:!0,showLabels:!0},settings:et([U,J,q,Y,G,{path:"props.deviation",label:"Deviation multiplier",kind:"number",min:.01,max:20,step:.1,group:"behavior"}])},t=>{if(t.pts.length<2)return{paths:[]};const e=t.rc.bars?.()??[],[i,n]=t.drawing.points,r=Yt(e,Math.min(i.time,n.time),!1),o=Yt(e,Math.max(i.time,n.time),!0);let s=0,l=0,a=0,c=0,h=0,d=0,u=0,p=0,f=0,x=0;for(let i=r;i<o;i++){const n=e[i],r=n.close,o=t.rc.dataLayer.timeToIndex?.(n.time)??t.rc.dataLayer.timeToIndexFloat(n.time);if(!Number.isFinite(o)||!Number.isFinite(r))continue;0===s&&(u=n.time,f=o),p=n.time,x=o,s++;const y=o-l,g=r-a;l+=y/s,a+=g/s,c+=y*(o-l),h+=y*(r-a),d+=g*(r-a)}if(0===s)return{paths:[],labels:!1===t.drawing.style.showLabels?[]:[{at:t.pts[0],text:"No bars in range"}]};const y=c>0?h/c:0,g=Math.sqrt(Math.max(0,d-y*h)/s)*Mt(t.drawing,"deviation",2,.01,20),m=d>0?Math.max(0,Math.min(1,y*h/d)):1,b=(e,i,n)=>{return _t({time:e,price:(r=i,a+y*(r-l)+n)},t.rc);var r},v=[0,g,-g].map(e=>({points:Pt(b(u,f,e),b(p,x,e),t)}));if(!0===t.drawing.style.fill){const e=Et(b(u,f,g),b(p,x,g),t),i=Et(b(u,f,-g),b(p,x,-g),t);v.push({points:Tt([e[0],e[1],i[1],i[0]],t.rc),closed:!0,fill:!0,stroke:!1})}return{paths:v,labels:!1===t.drawing.style.showLabels?[]:[{at:b(u,f,g),text:`R^2 ${m.toFixed(3)} ${s} bars`}]}});function Gt(t,e,i){return Ht({id:t,name:e,points:3,defaultStyle:{fill:!0,showLabels:!0,levels:c(Bt)},settings:et([U,J,K,G])},t=>{if(t.pts.length<3)return{paths:[]};const[e,n,r]=t.pts,o=bt(n,r),[l,a]=t.drawing.points,c={time:"schiff"===i?l.time:(l.time+a.time)/2,price:(l.price+a.price)/2},h="standard"===i?e:_t(c,t.rc),d="inside"===i?o:h,u="inside"===i?{x:r.x-h.x,y:r.y-h.y}:{x:o.x-h.x,y:o.y-h.y},p=e=>St(e,{x:e.x+u.x,y:e.y+u.y},t.rc,0,1/0),f=[],x=[],y=kt(t.drawing,Bt);for(const e of y){const i=e.color,l=0===e.ratio?[d]:[vt(o,n,e.ratio),vt(o,r,e.ratio)];for(const n of l){const r=p(n);f.push({points:r,color:i}),!1!==t.drawing.style.showLabels&&2===r.length&&x.push({at:{x:r[0].x+4,y:r[0].y-4},text:e.label??s(e.ratio),color:i})}}const g=y.reduce((t,e)=>Math.max(t,Math.abs(e.ratio)),0);if(!0===t.drawing.style.fill&&g>0){const e=vt(o,n,g),i=vt(o,r,g),s=Math.hypot(u.x,u.y);if(s>0){const n=(Math.max(Math.hypot(e.x,e.y),Math.hypot(i.x,i.y))+Math.hypot(t.rc.plotWidth,t.rc.plotHeight))/s+1,r=t=>({x:t.x+u.x*n,y:t.y+u.y*n});f.unshift({points:Tt([e,r(e),r(i),i],t.rc),closed:!0,fill:!0,stroke:!1})}}return f.push(jt(n,r)),"inside"===i&&f.push(jt(h,r),jt(e,n)),{paths:f,labels:x}})}const Xt=Ht({id:"info-line",name:"Info Line",points:2,angleLock:!0,defaultStyle:{showLabels:!0},settings:et([U,q,Y,G])},t=>{if(t.pts.length<2)return{paths:[]};const[e,i]=t.pts,[n,r]=t.drawing.points,o=r.price-n.price,s=o>=0?"+":"",l=0===n.price?"n/a":`${s}${(o/Math.abs(n.price)*100).toFixed(2)}%`,a=Math.abs(Math.round(t.rc.dataLayer.timeToIndexFloat(r.time)-t.rc.dataLayer.timeToIndexFloat(n.time)));return{paths:[{points:Pt(e,i,t)}],labels:!1===t.drawing.style.showLabels?[]:[{at:{x:(e.x+i.x)/2,y:(e.y+i.y)/2-8},text:`${s}${t.rc.priceScale.format(o)} (${l}) ${a} bars`}]}}),Zt=Ht({id:"trend-angle",name:"Trend Angle",points:2,angleLock:!0,alertValue:g(),defaultStyle:{showLabels:!0},settings:et([U,Y,G])},t=>{if(t.pts.length<2)return{paths:[]};const[e,i]=t.pts,n=Math.atan2(i.y-e.y,i.x-e.x),r=Math.max(12,Math.min(40,Math.hypot(i.x-e.x,i.y-e.y)/3));return{paths:[jt(e,i),jt(e,{x:e.x+1.5*r,y:e.y}),{points:Ft(e,r,r,0,n)}],labels:!1===t.drawing.style.showLabels?[]:[{at:{x:e.x+r+6,y:e.y-6},text:`${(180*-n/Math.PI).toFixed(1)} deg`}]}}),Qt=Ht({id:"fib-extension-two-point",name:"Fib Extension (Two Point)",points:2,alertValue:M(2,Rt),alertLevels:_(Rt),defaultStyle:{levels:c(Rt),showLabels:!0,fill:!0,fillOpacity:.06},settings:et([U,K,J,q,G])},t=>{if(t.pts.length<2)return{paths:[]};const[e,i]=t.pts,[r,o]=t.drawing.points,l=[],a=[],c=kt(t.drawing,Rt);if(!0===t.drawing.style.fill&&e.x!==i.x){const n=c.map(e=>t.rc.priceScale.priceToY(r.price+(o.price-r.price)*e.ratio)).filter(Number.isFinite).sort((t,e)=>t-e),s=!0===t.drawing.style.extendLeft?0:Math.min(e.x,i.x),a=!0===t.drawing.style.extendRight?t.rc.plotWidth:Math.max(e.x,i.x);for(let e=1;e<n.length;e++)n[e]!==n[e-1]&&l.push({points:Tt([{x:s,y:n[e-1]},{x:a,y:n[e-1]},{x:a,y:n[e]},{x:s,y:n[e]}],t.rc),closed:!0,fill:!0,stroke:!1})}l.push(jt(e,i));for(const h of c){const c=r.price+(o.price-r.price)*h.ratio,d=t.rc.priceScale.priceToY(c),u=h.color??n(h.ratio);l.push({points:Pt({x:e.x,y:d},{x:i.x,y:d},t),color:u}),!1!==t.drawing.style.showLabels&&a.push({at:{x:Math.min(e.x,i.x)+4,y:d-3},text:`${h.label??s(h.ratio)} ${t.rc.priceScale.format(c)}`,color:u})}return{paths:l,labels:a}});function te(t,e){if(t.pts.length<2)return{paths:[]};const[i,r]=t.pts,o=[],s=[];o.push({points:[{x:i.x,y:r.y},r,{x:r.x,y:i.y}]});const l=1.2*(t.drawing.text?.fontSize??11),a=[],c=(n,r,o,c,h)=>{const d=Math.min(i.x,r.x),u=Math.max(i.x,r.x),p=Math.min(i.y,r.y),f=Math.max(i.y,r.y);if(!e||!1===t.drawing.style.showLabels||a.length>=32)return;const x=e(c);if(!(!Number.isFinite(x)||x<=0||!Number.isFinite(l)||l<=0||(o?f-p<l:u-d<x)))for(const e of[0,-l,l]){let y,g;o?(y=r.x>=i.x?r.x+4:r.x-x-4,(y<0||y+x>t.rc.plotWidth)&&(y=r.x>=i.x?r.x-x-4:r.x+4),g=Math.max(p+l,Math.min(f,n.y+l/2+e))):(y=Math.max(d,Math.min(u-x,n.x-x/2+e)),g=r.y<i.y?r.y-4:r.y+l+4,(g-l<0||g>t.rc.plotHeight)&&(g=r.y<i.y?r.y+l+4:r.y-4));const m={x:y,y:g-l,width:x,height:l};if(!(y<0||y+x>t.rc.plotWidth||m.y<0||g>t.rc.plotHeight)&&!a.some(t=>y<t.x+t.width+3&&y+x+3>t.x&&m.y<t.y+t.height+3&&m.y+l+3>t.y))return a.push(m),void s.push({at:{x:y,y:g},text:c,color:h})}};for(const e of kt(t.drawing,Vt)){const s=[{x:r.x,y:i.y+(r.y-i.y)*e.ratio}];1!==e.ratio&&s.push({x:i.x+(r.x-i.x)*e.ratio,y:r.y}),s.forEach((s,l)=>{const a=St(i,s,t.rc,0,1/0),h=e.color??n(e.ratio);if(o.push({points:a,color:h}),2===a.length){const t={x:i.x+(Math.sign(s.x-i.x)||Math.sign(r.x-i.x))*Math.abs(r.x-i.x),y:i.y+(Math.sign(s.y-i.y)||Math.sign(r.y-i.y))*Math.abs(r.y-i.y)},n=Math.abs(e.ratio),o=vt(i,s,1/Math.max(1,n));c(o,t,n<=1?0===l:0!==l,e.label??String(e.ratio),h)}})}return{paths:o,labels:s}}const ee={...Ht({id:"fib-speed-resistance-fan",name:"Fib Speed Resistance Fan",points:2,defaultStyle:{levels:c(Vt),showLabels:!0},settings:et([U,K,G])},t=>te(t)),draw(t){const{ctx:e,rc:i}=t,n=t.drawing.text;let r;e.save();try{e.font=`${!0===n?.italic?"italic ":""}${!0===n?.bold?"700 ":""}${(n?.fontSize??11)*i.dpr}px ${n?.fontFamily||"ui-sans-serif, system-ui, sans-serif"}`,r=te({rc:i,drawing:{...t.drawing,style:t.style},pts:t.pts.map(t=>({x:t.x/i.dpr,y:t.y/i.dpr}))},t=>e.measureText(t).width/i.dpr)}finally{e.restore()}$t(t,r)}},ie=Ht({id:"icon-stamp",name:"Icon Stamp",points:1,defaultStyle:{fill:!0,fillOpacity:.8},settings:et([U,J,{path:"props.shape",label:"Shape",kind:"select",group:"behavior",options:["star","diamond","circle","arrow up","arrow down","check","cross"].map(t=>({value:t.replace(" ","-"),label:t[0].toUpperCase()+t.slice(1)}))},{path:"props.size",label:"Size",kind:"number",min:8,max:160,step:2,group:"behavior"}])},t=>{if(t.pts.length<1)return{paths:[]};const e=t.pts[0],i=Mt(t.drawing,"size",28,8,160)/2,n=(t,n)=>({x:e.x+t*i,y:e.y+n*i}),r=t.drawing.props?.shape;let o;if("diamond"===r)o=[n(0,-1),n(1,0),n(0,1),n(-1,0)];else if("circle"===r)o=Ft(e,i,i,0,2*Math.PI);else if("arrow-up"===r||"arrow-down"===r){const t="arrow-up"===r?1:-1;o=[[0,-1],[1,0],[.35,0],[.35,1],[-.35,1],[-.35,0],[-1,0]].map(([e,i])=>n(e,i*t))}else o="check"===r?[[-1,0],[-.65,-.3],[-.2,.2],[.75,-.85],[1,-.55],[-.2,.85]].map(([t,e])=>n(t,e)):"cross"===r?[[-1,-.65],[-.65,-1],[0,-.35],[.65,-1],[1,-.65],[.35,0],[1,.65],[.65,1],[0,.35],[-.65,1],[-1,.65],[-.35,0]].map(([t,e])=>n(t,e)):Array.from({length:10},(t,e)=>{const i=-Math.PI/2+e*Math.PI/5,r=e%2==0?1:.45;return n(Math.cos(i)*r,Math.sin(i)*r)});return{paths:[{points:o,closed:!0,fill:!0}]}}),ne=[Jt,qt,Kt,Gt("pitchfork","Pitchfork","standard"),Gt("schiff-pitchfork","Schiff Pitchfork","schiff"),Gt("modified-schiff-pitchfork","Modified Schiff Pitchfork","modified"),Gt("inside-pitchfork","Inside Pitchfork","inside"),Xt,Zt,Qt,ee,ie],re=2*Math.PI,oe=(1+Math.sqrt(5))/2,se=t=>t.map(t=>({ratio:t})),le=se([.236,.382,.5,.618,.786,1,1.618,2.618]),ae=le.slice(0,6),ce=se([0,.382,.618,1,1.382,1.618,2,2.382,2.618,3]),he=se([0,.25,.382,.5,.618,.75,1]),de=se(Array.from({length:12},(t,e)=>e+1)),ue=se([.236,.382,.5,.618,.786,1,1.618,2.618,4.236,6.854,11.09]),pe=et([U,K,G]),fe=et([U,J,K,G]),xe=(t,e,i)=>({points:[t,e],color:i}),ye=(t,e,i=e,n=0,r=re,o)=>({points:[],arc:{center:t,rx:e,ry:i,start:n,sweep:r},color:o}),ge=(t,e=2)=>t.pts.length>=e&&t.pts.every(wt),me=(t,e,i)=>!1===t.drawing.style.showLabels?[]:[{at:i,text:e.label??String(e.ratio),color:e.color}];function be(t){return ge(t)?{paths:[xe(t.pts[0],t.pts[1])]}:{paths:[]}}const ve=Ht({id:"trend-fib-time",name:"Trend Fib Time",points:3,defaultStyle:{levels:c(ce)},settings:pe},t=>{if(!ge(t,3))return be(t);const[e,i,n]=t.drawing.points,r=e=>t.rc.dataLayer.timeToIndexFloat(e),o=r(i.time)-r(e.time),s=r(n.time),l=[],a=[];for(const e of kt(t.drawing,ce)){const i=t.rc.timeScale.indexToX(s+o*e.ratio);!Number.isFinite(i)||i<0||i>t.rc.plotWidth||(l.push(xe({x:i,y:0},{x:i,y:t.rc.plotHeight},e.color)),a.push(...me(t,e,{x:i+3,y:15})))}return{paths:l,labels:a}});function we(t,e,i){const n="wedge"===i?ae:le;return Ht({id:t,name:e,points:"wedge"===i?3:2,defaultStyle:{fill:!1,levels:c(n)},settings:fe},t=>{if(!ge(t,"wedge"===i?3:2))return be(t);const[e,r,o]=t.pts,s=Math.hypot(r.x-e.x,r.y-e.y);if(!Number.isFinite(s)||0===s)return be(t);let l=0,a=re;const c=[],h=[];if("circle"!==i)if(l=Math.atan2(r.y-e.y,r.x-e.x),c.push(xe(e,r)),"arc"===i)l-=Math.PI/2,a=Math.PI;else{const t=Math.atan2(o.y-e.y,o.x-e.x);a=(t-l+3*Math.PI)%re-Math.PI,c.push(xe(e,{x:e.x+s*Math.cos(t),y:e.y+s*Math.sin(t)}))}let d=0;for(const r of kt(t.drawing,n)){const n=s*r.ratio;if(!(n>0&&Number.isFinite(n)))continue;d=Math.max(d,n),c.push(ye(e,n,n,l,a,r.color));const o="circle"===i?0:l+a/2;h.push(...me(t,r,{x:e.x+n*Math.cos(o)+3,y:e.y+n*Math.sin(o)}))}if(d>0&&!0===t.drawing.style.fill){const t=ye(e,d,d,l,a);t.arc.sector="circle"!==i,c.unshift({...t,fill:!0,stroke:!1,closed:!0})}return{paths:c,labels:h}})}const _e=Ht({id:"fib-spiral",name:"Fib Spiral",points:2,settings:et([U])},t=>{if(!ge(t))return{paths:[]};const[e,i]=t.pts,n=Math.hypot(i.x-e.x,i.y-e.y);if(!Number.isFinite(n)||n<.5)return be(t);const r=Math.log(oe)/(Math.PI/2),o=Math.atan2(i.y-e.y,i.x-e.x),s=Math.max(-16*Math.PI,Math.log(.5/n)/r),l=[],a=[s];for(let t=Math.ceil(s/(Math.PI/2));t<2;t++)a.push(t*Math.PI/2);a.push(Math.PI);for(let t=1;t<a.length;t++){const i=a[t-1],s=a[t],c=Math.max(2,Math.min(32,Math.ceil((s-i)*Math.sqrt(n*Math.exp(r*s)/.8))));for(let a=1===t?0:1;a<=c;a++){const t=i+(s-i)*a/c,h=n*Math.exp(r*t);l.push({x:e.x+h*Math.cos(o+t),y:e.y+h*Math.sin(o+t)})}}return{paths:[{points:l}]}}),Me=Ht({id:"gann-square",name:"Gann Square",points:2,defaultStyle:{fill:!1,levels:c(he)},settings:fe},t=>{if(!ge(t))return{paths:[]};const[e,i]=t.pts,[n,r]=t.drawing.points,o=i.x-e.x,s=i.y-e.y,l=Math.min(e.x,i.x),a=Math.max(e.x,i.x),c=Math.min(e.y,i.y),h=Math.max(e.y,i.y),d=[],u=[];!0===t.drawing.style.fill&&d.push({points:[{x:l,y:c},{x:a,y:c},{x:a,y:h},{x:l,y:h}],closed:!0,fill:!0,stroke:!1});const p=t.rc.dataLayer.timeToIndexFloat(n.time),f=t.rc.dataLayer.timeToIndexFloat(r.time);for(const e of kt(t.drawing,he)){const i=t.rc.timeScale.indexToX(p+(f-p)*e.ratio),o=t.rc.priceScale.priceToY(n.price+(r.price-n.price)*e.ratio);d.push(xe({x:l,y:o},{x:a,y:o},e.color),xe({x:i,y:c},{x:i,y:h},e.color)),u.push(...me(t,e,{x:l+3,y:o-3}))}for(const[i,n,r]of[[1,1/3,"3x1"],[1,.5,"2x1"],[1,1,"1x1"],[.5,1,"1x2"],[1/3,1,"1x3"]]){const l={x:e.x+o*i,y:e.y+s*n};d.push(xe(e,l)),!1!==t.drawing.style.showLabels&&u.push({at:{x:l.x+3,y:l.y},text:r})}if(0!==o&&0!==s)for(const t of[.25,.5,.75,1])d.push(ye(e,Math.abs(o)*t,Math.abs(s)*t,o>0?0:Math.PI,Math.sign(o*s)*Math.PI/2));return{paths:d,labels:u}});function ke(t,e,i,n,r,o){const s=[-Math.PI,0];for(const r of[i,n]){const i=(r-t.x)/e;Math.abs(i)<1&&s.push(-Math.acos(i))}for(const i of[r,o]){const n=(i-t.y)/e;if(n>-1&&n<0){const t=Math.asin(n);s.push(t,-Math.PI-t)}}s.sort((t,e)=>t-e);const l=[];for(let a=1;a<s.length;a++){const c=s[a-1],h=s[a]-c;if(h<1e-12)continue;const d=c+h/2,u=t.x+e*Math.cos(d),p=t.y+e*Math.sin(d);u<i||u>n||p<r||p>o||l.push(ye(t,e,e,c,h))}return l}const Se=Ht({id:"dedekind-tessellation",name:"Dedekind Tessellation",points:2,settings:et([U,{path:"props.maxCurvature",label:"Max curvature",kind:"number",min:1,max:64,step:1,group:"behavior"}])},t=>{if(!ge(t))return{paths:[]};const[e,i]=t.pts,n=Math.min(e.x,i.x),r=Math.max(e.x,i.x),o=Math.min(e.y,i.y),s=Math.max(e.y,i.y),l=s-o;if(l<.75||r-n<.75||!Number.isFinite(l))return be(t);const a=Math.max(0,n),c=Math.min(t.rc.plotWidth,r),h=Math.max(0,o),d=Math.min(t.rc.plotHeight,s);if(a>c||h>d)return{paths:[]};const u=[{x:n,y:o},{x:r,y:o},{x:r,y:s},{x:n,y:s}],p=u.map((e,i)=>({points:St(e,u[(i+1)%4],t.rc)})),f=(a-n)/l,x=(c-n)/l,y=Math.ceil(f-.5)+.5,g=Math.min(2048,Math.floor(x-y)+1);for(let t=0;t<g;t++){const e=n+(y+t)*l;p.push(xe({x:e,y:d},{x:e,y:h}))}const m=Math.min(Math.round(Mt(t.drawing,"maxCurvature",24,1,64)),Math.floor(l/.75));for(let t=1;t<=m&&p.length<4096;t++){if(t%2==0&&t%8!=0)continue;const e=l/t;if(!(s-e>d))for(let i=0;i<t&&p.length<4096;i++){const r=i*i-1;if(r%t!==0||t%8==0&&Math.abs(r/t)%2!=1)continue;const o=Math.ceil(f-1/t-i/t),u=Math.floor(x+1/t-i/t),y=Math.min(4096-p.length,u-o+1);for(let r=0;r<y;r++){const u={x:n+(o+r+i/t)*l,y:s};p.push(...ke(u,e,a,c,h,d))}}}return{paths:p}});function Pe(t,e,i,n){const r=i?ue:de;return Ht({id:t,name:e,points:2,defaultStyle:{levels:c(r)},settings:et([U,K,G,...i?[]:[{path:"props.waveCount",label:"Wave limit",kind:"number",min:1,max:12,step:1,group:"behavior"}],...n?[{path:"props.mach",label:"Mach number",kind:"number",min:1.01,max:20,step:.5,group:"behavior"}]:[]])},t=>{if(!ge(t))return{paths:[]};const[e,o]=t.pts,s=bt(e,o),l=Math.hypot(o.x-e.x,o.y-e.y)/2;if(!Number.isFinite(l)||l<.5)return be(t);const a=(o.x-e.x)/(2*l),c=(o.y-e.y)/(2*l),h=n?Mt(t.drawing,"mach",2,1.01,20):1,d={x:s.x-h*l*a,y:s.y-h*l*c},u=kt(t.drawing,r).filter(t=>t.ratio>0).slice().sort((t,e)=>t.ratio-e.ratio);i||u.splice(Math.round(Mt(t.drawing,"waveCount",6,1,12)));const p=[],f=[];let x=0;for(const e of u){const i=l*e.ratio,n={x:d.x+h*i*a,y:d.y+h*i*c};Number.isFinite(i)&&wt(n)&&(x=Math.max(x,i),p.push(ye(n,i,i,0,re,e.color)),f.push(...me(t,e,{x:n.x+a*i+4,y:n.y+c*i})))}if(0===x)return{paths:[]};const y=h*x+2*l;if(n){const t=1/h,e=Math.sqrt(1-t*t);for(const i of[-1,1])p.push(xe(d,{x:d.x+y*(a*e-i*c*t),y:d.y+y*(c*e+i*a*t)}))}else p.push(xe({x:d.x-c*y,y:d.y+a*y},{x:d.x+c*y,y:d.y-a*y}));return{paths:p,labels:f}})}const Te=[ve,we("fib-circles","Fib Circles","circle"),we("fib-speed-resistance-arcs","Fib Speed Resistance Arcs","arc"),we("fib-wedge","Fib Wedge","wedge"),_e,Me,Se,Pe("sonic","Sonic",!1,!1),Pe("supersonic","Supersonic",!1,!0),Pe("golden-sonic","Golden Sonic",!0,!1),Pe("golden-supersonic","Golden Supersonic",!0,!0)],Le="#16a34a",Fe="#dc2626",Ie={min:1.272,max:1.414},Ae={min:.74,max:.83},Ce={gartley:{ab:{min:.55,max:.68},bc:{min:.382,max:.886},cd:{min:1.13,max:1.618},ad:{min:.74,max:.83}},bat:{ab:{min:.382,max:.5},bc:{min:.382,max:.886},cd:{min:1.618,max:2.618},ad:{min:.84,max:.92}},butterfly:{ab:{min:.74,max:.83},bc:{min:.382,max:.886},cd:{min:1.618,max:2.618},ad:{min:1.272,max:1.618}},crab:{ab:{min:.382,max:.618},bc:{min:.382,max:.886},cd:{min:2.618,max:3.618},ad:{min:1.55,max:1.69}},shark:{ab:{min:.382,max:.618},bc:{min:1.13,max:1.618},cd:{min:1.618,max:2.24},ad:{min:.886,max:1.13}}},Ne=et([U,Y,G]),ze=et([U,J,Y,G]),Oe=(t,e,i)=>({points:St(t,e,i.rc)});function De(t,e){return Number.isFinite(t)&&Number.isFinite(e)&&Math.abs(e)>=1e-9?Math.abs(t)/Math.abs(e):null}function $e(t,e,i){const n=t[e]?.price,r=t[i]?.price;return Number.isFinite(n)&&Number.isFinite(r)?Math.abs(r-n):NaN}function He(t){return null!==t.value&&void 0!==t.band&&t.value>=t.band.min&&t.value<=t.band.max}function We(t,e,i,n){const r=e.x-t.x,o=e.y-t.y,s=n.x-i.x,l=n.y-i.y,a=s*o-l*r;if(!Number.isFinite(a)||Math.abs(a)<1e-9)return null;const c=((i.y-t.y)*r-(i.x-t.x)*o)/a;return!Number.isFinite(c)||c<0||c>1?null:{x:i.x+c*s,y:i.y+c*l}}function Be(t,e){const i=e.pts.slice(0,t.labels.length),n=e.drawing.points.slice(0,t.labels.length);if(i.length<2)return{paths:[]};const r=[];if(!0===t.fill)for(const t of[[0,1,2],[2,3,4]]){const n=t.map(t=>i[t]);n.every(t=>void 0!==t&&wt(t))&&r.push({points:Tt(n,e.rc),closed:!0,fill:!0,stroke:!1})}for(let t=1;t<i.length;t++)r.push(Oe(i[t-1],i[t],e));if(void 0!==t.neckline&&i.length>=t.labels.length){const[n,o]=t.neckline,s=i[n],l=i[o],a=We(s,l,i[0],i[1])??s,c=i.length-1,h=We(s,l,i[c-1],i[c])??l;r.push(...function(t,e,i){const n=St(t,e,i.rc);if(n.length<2)return[];const[r,o]=n,s=o.x-r.x,l=o.y-r.y,a=Math.hypot(s,l);if(!(a>0&&Number.isFinite(a)))return[{points:n}];const c=[];for(let t=0;t<a;t+=12){const e=t/a,i=Math.min(t+7,a)/a;c.push({points:[{x:r.x+s*e,y:r.y+l*e},{x:r.x+s*i,y:r.y+l*i}]})}return c}(a,h,e))}if(!1===e.drawing.style.showLabels)return{paths:r};const o=function(t,e){return e.flatMap((i,n)=>{const r=t.labels[n];if(!r||!wt(i))return[];const o=e[n-1]?.y??i.y,s=e[n+1]?.y??i.y,l=i.y<=(o+s)/2;return[{at:{x:i.x+4,y:i.y+(l?-6:14)},text:r}]})}(t,i),s=void 0!==t.harmonic?function(t,e){const i=$e(t,0,1),n=$e(t,1,2);if("cypher"===e){const e=$e(t,0,3);return[{name:"AB/XA",value:De(n,i),band:{min:.382,max:.618},anchor:2},{name:"XC/XA",value:De(e,i),band:Ie,anchor:3},{name:"CD/XC",value:De($e(t,3,4),e),band:Ae,anchor:4}]}const r=$e(t,2,3);return[{name:"AB/XA",value:De(n,i),band:e.ab,anchor:2},{name:"BC/AB",value:De(r,n),band:e.bc,anchor:3},{name:"CD/BC",value:De($e(t,3,4),r),band:e.cd,anchor:4},{name:"AD/XA",value:De($e(t,1,4),i),band:e.ad,anchor:4}]}(n,t.harmonic):void 0!==t.ratios?function(t,e){if("xabcd"===e){const e=$e(t,0,1),i=$e(t,1,2),n=$e(t,2,3);return[{name:"AB/XA",value:De(i,e),anchor:2},{name:"BC/AB",value:De(n,i),anchor:3},{name:"CD/BC",value:De($e(t,3,4),n),anchor:4}]}const i=$e(t,0,1),n=$e(t,1,2);return[{name:"BC/AB",value:De(n,i),anchor:2},{name:"CD/BC",value:De($e(t,2,3),n),anchor:3}]}(n,t.ratios):[];if(o.push(...function(t,e){const i=[];for(let n=0;n<t.length;n++){const r=t[n];if(null===r.value||!Number.isFinite(r.value))continue;const o=e[r.anchor],s=e[Math.max(0,r.anchor-1)];if(!(o&&s&&wt(o)&&wt(s)))continue;const l=void 0!==r.band,a=l&&He(r),c=l?a?" OK":" OUT":"",h=bt(s,o);i.push({at:{x:h.x+6,y:h.y+(n===t.length-1&&r.anchor===t[n-1]?.anchor?14:-3)},text:`${r.name} ${r.value.toFixed(3)}${c}`,color:l?a?Le:Fe:void 0})}return i}(s,i)),void 0!==t.harmonic&&i.length>=t.labels.length){const e=function(t,e){if(t.length<e)return!1;let i=t[1].price-t[0].price;if(!Number.isFinite(i)||0===i)return!1;for(let n=2;n<e;n++){const e=t[n].price-t[n-1].price;if(!Number.isFinite(e)||0===e||Math.sign(e)===Math.sign(i))return!1;i=e}return!0}(n,t.labels.length),r=e&&s.length>0&&s.every(He),l=i[t.labels.length-1];l&&wt(l)&&o.push({at:{x:l.x+4,y:l.y+30},text:`${t.name}: ${e?r?"valid":"outside range":"invalid sequence"}`,color:r?Le:Fe})}return{paths:r,labels:o}}function Re(t){return Ht({id:t.id,name:t.name,points:t.labels.length,defaultStyle:!0===t.fill?{fill:!0,fillOpacity:.12,showLabels:!0}:{showLabels:!0},settings:!0===t.fill?ze:Ne},e=>Be(t,e))}const Ve=["X","A","B","C","D"],je=[Re({id:"xabcd-pattern",name:"XABCD Pattern",labels:Ve,fill:!0,ratios:"xabcd"}),Re({id:"abcd-pattern",name:"ABCD Pattern",labels:["A","B","C","D"],ratios:"abcd"}),Re({id:"elliott-impulse",name:"Elliott Impulse",labels:["1","2","3","4","5"]}),Re({id:"elliott-correction",name:"Elliott Correction",labels:["A","B","C"]}),Re({id:"head-shoulders",name:"Head and Shoulders",labels:["","LS","","H","","RS",""],neckline:[2,4]}),Re({id:"gartley",name:"Gartley",labels:Ve,fill:!0,harmonic:Ce.gartley}),Re({id:"bat",name:"Bat",labels:Ve,fill:!0,harmonic:Ce.bat}),Re({id:"butterfly",name:"Butterfly",labels:Ve,fill:!0,harmonic:Ce.butterfly}),Re({id:"crab",name:"Crab",labels:Ve,fill:!0,harmonic:Ce.crab}),Re({id:"shark",name:"Shark",labels:Ve,fill:!0,harmonic:Ce.shark}),Re({id:"cypher",name:"Cypher",labels:Ve,fill:!0,harmonic:"cypher"})];function Ee(t,e,i,n){return"number"==typeof t&&Number.isFinite(t)?Math.max(i,Math.min(n,t)):e}function Ue(t,e,i=!1){let n=0,r=t.length;for(;n<r;){const o=n+r>>>1;(i?t[o].time<=e:t[o].time<e)?n=o+1:r=o}return n}function Je(t,e,i,n,r){return 0===t?"empty":e?r||i>0||n>0?"partial":"ready":i>0?"missing-volume":n>0?"invalid-data":"zero-volume"}function qe(t,e){switch(e){case"close":return t.close;case"hl2":return t.low/2+t.high/2;case"ohlc4":return t.open/4+t.high/4+t.low/4+t.close/4;default:return t.high/3+t.low/3+t.close/3}}function Ye(t,e,i={}){const n={status:"empty",historyPartial:!1,points:[],missingVolumeBars:0,invalidPriceBars:0};if(!Number.isFinite(e))return n;n.historyPartial=t.length>0&&e<t[0].time;const r=Ue(t,e);let o=0,s=0,l=0,a=0,c=0,h=!1;for(let e=r;e<t.length;e++){const r=t[e],d=r.volume;if(void 0===d||!Number.isFinite(d)||d<0){n.missingVolumeBars++,h=!0;continue}const u=qe(r,i.source??"hlc3");if(Number.isFinite(u)){if(d>0){if(0===o&&(l=u),d>s){const t=s/d;o*=t,c*=t,s=d}const t=d/s,e=o+t,i=u-l-a;a+=i*(t/e),c+=t*i*(u-l-a),o=e}if(o>0){const t=l+a,e=Math.sqrt(Math.max(0,c/o));if(!Number.isFinite(t)||!Number.isFinite(e))return{...n,status:"invalid-data",points:[]};n.points.push({time:r.time,value:t,deviation:e,breakBefore:h}),h=!1}}else n.invalidPriceBars++,h=!0}return n.status=Je(t.length-r,o>0,n.missingVolumeBars,n.invalidPriceBars,n.historyPartial),n}function Ke(t,e,i,n={}){const r={status:"empty",historyPartial:!1,rows:[],totalVolume:0,poc:null,valueAreaLow:null,valueAreaHigh:null,missingVolumeBars:0,invalidPriceBars:0};if(!Number.isFinite(e)||!Number.isFinite(i))return r;r.historyPartial=t.length>0&&Math.min(e,i)<t[0].time;const o=Ue(t,Math.min(e,i)),s=Ue(t,Math.max(e,i),!0);let l=1/0,a=-1/0,c=0;for(let e=o;e<s;e++){const i=t[e],n=i.volume;if(void 0===n||!Number.isFinite(n)||n<0){r.missingVolumeBars++;continue}const o=i.low,s=i.high;if(!Number.isFinite(o)||!Number.isFinite(s)||s<o){r.invalidPriceBars++;continue}l=Math.min(l,o),a=Math.max(a,s);const h=n-c,d=r.totalVolume+h;c=d-r.totalVolume-h,r.totalVolume=d}if(r.status=Je(s-o,r.totalVolume>0,r.missingVolumeBars,r.invalidPriceBars,r.historyPartial),!Number.isFinite(r.totalVolume)||r.totalVolume>0&&!Number.isFinite(a-l))return{...r,status:"invalid-data",totalVolume:0};if(r.totalVolume<=0)return r;const h=Math.round(Ee(n.rows,48,4,200)),d=[l];for(let t=1;t<h;t++){const e=l+t/h*(a-l);e>d[d.length-1]&&e<a&&d.push(e)}d.push(a);const u=d.length-1,p=(a-l)/u,f=t=>d[t],x=new Float64Array(u),y=new Float64Array(u+1),g=t=>{let e=0,i=u;for(;e<i;){const n=e+i+1>>>1;d[n]<=t?e=n:i=n-1}return Math.min(u-1,e)};for(let e=o;e<s;e++){const i=t[e],n=i.volume,o=i.low,s=i.high;if(void 0===n||!Number.isFinite(n)||n<=0||!Number.isFinite(o)||!Number.isFinite(s)||s<o)continue;const l=n/r.totalVolume,a=g(o),c=g(s);if(a===c){x[a]+=l;continue}const h=s-o;if(x[a]+=l*((f(a+1)-o)/h),x[c]+=l*((s-f(c))/h),c>a+1){const t=l*(p/h);y[a+1]+=t,y[c]-=t}}let m=0,b=0;for(let t=0;t<u;t++){m+=y[t];const e=1===u?0:m*((f(t+1)-f(t))/p);x[t]=Math.max(0,Math.min(1,x[t]+e))*r.totalVolume,x[t]>x[b]&&(b=t),r.rows.push({low:f(t),high:f(t+1),volume:x[t],valueArea:!1})}let v=b,w=b,_=x[b];const M=r.totalVolume*(Ee(n.valueArea,70,1,100)/100);for(;_<M&&(v>0||w<u-1);){_+=(v>0?x[v-1]:-1)>=(w<u-1?x[w+1]:-1)?x[--v]:x[++w]}for(let t=v;t<=w;t++)r.rows[t].valueArea=!0;return r.poc=f(b)/2+f(b+1)/2,r.valueAreaLow=f(v),r.valueAreaHigh=f(w+1),r}function Ge(t){const e=t.props?.source;return"close"===e||"hl2"===e||"ohlc4"===e?e:"hlc3"}function Xe(t,e,i){return{x:i.rc.timeScale.indexToX(i.rc.dataLayer.timeToIndexFloat(t)),y:i.rc.priceScale.priceToY(e)}}function Ze(t){const e=t.historyPartial?" (partial history)":"";switch(t.status){case"partial":return e+(t.missingVolumeBars>0||t.invalidPriceBars>0?" (partial volume/data)":"");case"missing-volume":return": volume unavailable"+e;case"zero-volume":return": zero volume"+e;case"invalid-data":return": data unavailable"+e;case"empty":return": no bars in range"+e;default:return""}}function Qe(t,e){for(const i of e.drawing.points){const n=Xe(i.time,i.price,e);for(const[i,r]of[[{x:n.x-4,y:n.y},{x:n.x+4,y:n.y}],[{x:n.x,y:n.y-4},{x:n.x,y:n.y+4}]]){const n=St(i,r,e.rc);n.length>0&&t.lines.push(n)}}}function ti(t){const e={lines:[],bands:[],rects:[],label:"",labelAt:{x:0,y:0},status:"empty"},i=t.drawing.points[0];if(!i)return e;const n=Ye(t.rc.bars?.()??[],i.time,{source:Ge(t.drawing)});e.status=n.status;const r=!1!==t.drawing.props?.showBands,o=Ee(t.drawing.props?.bandMultiplier,1,0,5),s=n.points[n.points.length-1];e.labelAt=s?Xe(s.time,s.value,t):Xe(i.time,i.price,t),e.label=`AVWAP${s?` ${t.rc.priceScale.format(s.value)}`:""}${Ze(n)}`;for(const i of r&&o>0?[0,-o,o]:[0]){let r;for(const o of n.points){const n=Xe(o.time,o.value+o.deviation*i,t);o.breakBefore&&(r=void 0);const s=St(r||{x:n.x-2,y:n.y},n,t.rc);s.length>0&&(0===i?e.lines:e.bands).push(s),r=n}}return s||Qe(e,t),e}function ei(t){const e={lines:[],bands:[],rects:[],label:"",labelAt:{x:0,y:0},status:"empty"},[i,n]=t.drawing.points;if(!i||!n)return e;const r=Ke(t.rc.bars?.()??[],i.time,n.time,{rows:Ee(t.drawing.props?.rows,48,4,200),valueArea:Ee(t.drawing.props?.valueArea,70,1,100)});e.status=r.status;const o=Xe(i.time,i.price,t),s=Xe(n.time,n.price,t),l=Math.min(o.x,s.x),a=Math.max(o.x,s.x),c=Math.max(0,...r.rows.map(t=>t.volume)),h=Math.max(12,a-l)*Ee(t.drawing.props?.width,35,5,100)/100,d="right"===t.drawing.props?.side,u=!1!==t.drawing.props?.showValueArea;e.label=`Volume profile (estimated)${Ze(r)}`,e.labelAt={x:l,y:Math.min(o.y,s.y)};for(const i of r.rows){const n=t.rc.priceScale.priceToY(i.low),r=t.rc.priceScale.priceToY(i.high);if(![n,r].every(Number.isFinite)||c<=0||i.volume<=0)continue;const o=h*i.volume/c,s=d?a-o:l,p=d?a:l+o,f=Math.max(0,Math.min(n,r)),x=Math.min(t.rc.plotHeight,Math.max(n,r)+(n===r?1:0)),y={left:Math.max(0,s),right:Math.min(t.rc.plotWidth,p),top:f,bottom:x,area:u&&i.valueArea};y.left<y.right&&y.top<y.bottom&&e.rects.push(y)}const p=(e,i)=>{if(null===e)return;const n=t.rc.priceScale.priceToY(e),r=St({x:l,y:n},{x:a===l?a+12:a,y:n},t.rc);r.length>0&&i.push(r)};return!1!==t.drawing.props?.showPoc&&p(r.poc,e.lines),u&&(p(r.valueAreaLow,e.bands),p(r.valueAreaHigh,e.bands)),null!==r.valueAreaHigh&&(e.labelAt.y=t.rc.priceScale.priceToY(r.rows[r.rows.length-1].high)),0===r.rows.length&&Qe(e,t),e}function ii(t,e){const{ctx:i,rc:n,drawing:r,style:o}=t,s=n.dpr;if(!(n.plotWidth>0&&n.plotHeight>0))return;const l=e({drawing:r,rc:n,pts:[]});i.save(),i.beginPath(),i.rect(0,0,n.plotWidth*s,n.plotHeight*s),i.clip(),i.strokeStyle=o.color,i.fillStyle=o.color,i.lineWidth=Math.max(1,o.lineWidth*s);const a="dashed"===o.lineStyle?[6*s,4*s]:"dotted"===o.lineStyle?[2*s,3*s]:[];i.setLineDash(a);for(const t of l.rects)i.save(),i.globalAlpha*=Ee(o.fillOpacity,.4,0,1)*(t.area?1:.45),i.fillRect(t.left*s,t.top*s,(t.right-t.left)*s,(t.bottom-t.top)*s),i.restore();const c=t=>{if(0!==t.length){i.beginPath();for(const e of t)i.moveTo(e[0].x*s,e[0].y*s),i.lineTo(e[1].x*s,e[1].y*s);i.stroke()}};if(c(l.lines),i.save(),i.globalAlpha*=.6,c(l.bands),i.restore(),(!1!==o.showLabels||"ready"!==l.status)&&l.label&&Number.isFinite(l.labelAt.x)&&Number.isFinite(l.labelAt.y)){const t=r.text,e=Ee(t?.fontSize,11,6,96);i.fillStyle=t?.color??o.color,i.font=`${t?.italic?"italic ":""}${t?.bold?"bold ":""}${e*s}px ${t?.fontFamily??"sans-serif"}`,i.textAlign="left",i.textBaseline="bottom";const a=i.measureText(l.label).width/s,c=Math.max(4,Math.min(n.plotWidth-a-4,l.labelAt.x+4)),h=Math.max(e+3,Math.min(n.plotHeight-3,l.labelAt.y-5));i.fillText(l.label,c*s,h*s)}i.restore()}function ni(t,e,i,n){if(t<0||e<0||t>i.rc.plotWidth||e>i.rc.plotHeight)return null;const r=n(i);let o=1/0;for(const i of[...r.lines,...r.bands])o=Math.min(o,k(t,e,i[0],i[1]));if(Ee(i.drawing.style.fillOpacity,.4,0,1)>0)for(const i of r.rects)o=Math.min(o,A(t,e,{x:i.left,y:i.top},{x:i.right,y:i.bottom},!0));return Number.isFinite(o)?o:null}const ri={id:"anchored-vwap",name:"Anchored VWAP",points:1,defaultStyle:{showLabels:!0,lineWidth:2},settings:et([U,Y,G,{path:"props.source",label:"Price source",kind:"select",group:"behavior",defaultValue:"hlc3",options:[{value:"hlc3",label:"HLC3"},{value:"close",label:"Close"},{value:"hl2",label:"HL2"},{value:"ohlc4",label:"OHLC4"}]},{path:"props.showBands",label:"Deviation bands",kind:"boolean",group:"behavior",defaultValue:!0},{path:"props.bandMultiplier",label:"Band multiplier",kind:"number",min:0,max:5,step:.25,group:"behavior",defaultValue:1}]),draw:t=>ii(t,ti),distance:(t,e,i)=>ni(t,e,i,ti)},oi={id:"fixed-range-volume-profile",name:"Fixed Range Volume Profile",points:2,defaultStyle:{showLabels:!0,fillOpacity:.4},settings:et([U,Y,G,{path:"style.fillOpacity",label:"Histogram opacity",kind:"opacity",min:0,max:1,step:.05,group:"fill",defaultValue:.4},{path:"props.rows",label:"Price rows",kind:"number",min:4,max:200,step:1,group:"behavior",defaultValue:48},{path:"props.valueArea",label:"Value area (%)",kind:"number",min:1,max:100,step:1,group:"behavior",defaultValue:70},{path:"props.width",label:"Range width (%)",kind:"number",min:5,max:100,step:1,group:"behavior",defaultValue:35},{path:"props.side",label:"Histogram side",kind:"select",group:"behavior",defaultValue:"left",options:[{value:"left",label:"Left"},{value:"right",label:"Right"}]},{path:"props.showPoc",label:"Point of control",kind:"boolean",group:"behavior",defaultValue:!0},{path:"props.showValueArea",label:"Value area levels",kind:"boolean",group:"behavior",defaultValue:!0}]),draw:t=>ii(t,ei),distance:(t,e,i)=>ni(t,e,i,ei)},si=new Map;function li(t){si.set(t.id,t)}function ai(t){const e=si.get(t);if(void 0===e)throw new Error(`openalgo-charts: unknown drawing tool "${t}"`);return e}function ci(t){return si.has(t)}function hi(){return Array.from(si.values())}function di(t){const e=t.split("+").map(t=>t.trim().toLowerCase());return{key:e[e.length-1]??"",alt:e.includes("alt"),ctrl:e.includes("ctrl")||e.includes("control"),shift:e.includes("shift")}}function ui(t){const e=(t.key??"").toLowerCase();if(""===e)return null;const i=!0===t.altKey,n=!0===t.ctrlKey||!0===t.metaKey,r=!0===t.shiftKey;if(!i&&!n)return null;for(const t of si.values()){if(void 0===t.shortcut)continue;const o=di(t.shortcut);if(o.key===e&&o.alt===i&&o.ctrl===n&&o.shift===r)return t.id}return null}function pi(){const t={};for(const e of si.values())void 0!==e.shortcut&&(t[e.id]=e.shortcut);return t}const fi=et([U]);function xi(t){return si.get(t)?.settings??fi}const yi={path:"style.fillOpacity",label:"Opacity",kind:"opacity",min:0,max:1,step:.01,group:"fill"},gi={...Y,label:"Show price"},mi={path:"style.showStats",label:"Show stats",kind:"boolean",group:"behavior"},bi={path:"style.pressure",label:"Pen pressure",kind:"boolean",group:"line"},vi=[{path:"style.accountSize",label:"Account size",kind:"number",min:0,step:1e3,group:"behavior"},{path:"style.risk",label:"Risk %",kind:"number",min:0,max:100,step:.1,group:"behavior"},{path:"props.showHeader",label:"Show direction and ratio",kind:"boolean",group:"behavior"},{path:"props.showLossSize",label:"Show loss and size",kind:"boolean",group:"behavior"},{path:"props.showTargetLabel",label:"Show target label",kind:"boolean",group:"behavior"},{path:"props.showStopLabel",label:"Show stop label",kind:"boolean",group:"behavior"},{path:"props.showPrices",label:"Show level prices",kind:"boolean",group:"behavior"},{path:"props.profitColor",label:"Profit zone",kind:"color",group:"fill"},{path:"props.lossColor",label:"Loss zone",kind:"color",group:"fill"}];function wi(t){const{ctx:e,rc:i,style:n}=t,r=i.dpr;e.strokeStyle=n.color,e.lineWidth=Math.max(1,Math.round(n.lineWidth*r)),e.lineJoin="round",e.lineCap="round",e.setLineDash("dashed"===n.lineStyle?[6*r,4*r]:"dotted"===n.lineStyle?[1*r,3*r]:[])}function _i(t){return t.style.fillColor??t.style.color}function Mi(t,e){if(!0!==t.style.fill)return;const{ctx:i}=t;i.save(),i.globalAlpha=t.style.fillOpacity??.12,i.fillStyle=_i(t),e(),i.restore()}const ki={value:""};function Si(t){return t.text??ki}function Pi(t,e,i){const n=i??(!0===t.bold?"700":"");return`${!0===t.italic?"italic ":""}${""===n?"":`${n} `}${e}px ${void 0===t.fontFamily||""===t.fontFamily?"ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif":t.fontFamily}`}function Ti(t,e){const i=t.text?.value;return void 0===i||""===i?e:i}function Li(t,e,i,n,r,o={}){const{ctx:s,rc:l,style:a}=t,c=Si(t.drawing),h=(c.fontSize??11)*l.dpr;s.save(),s.setLineDash([]),s.font=Pi(c,h),s.textBaseline="middle",s.textAlign="left";const d=mt(s,e),u=!0===o.center?i-d/2:i;s.globalAlpha=.85,s.fillStyle=l.theme.background,s.fillRect(u-2*l.dpr,n-.75*h,d+4*l.dpr,1.5*h),void 0!==o.tint&&(s.globalAlpha=.22,s.fillStyle=o.tint,s.fillRect(u-2*l.dpr,n-.75*h,d+4*l.dpr,1.5*h)),s.globalAlpha=1,s.fillStyle=c.color??r??a.color,s.fillText(e,u,n),s.restore()}function Fi(t,e,i,n,r,o={}){const{ctx:s,rc:l}=t,a=l.dpr,c=Si(t.drawing),h=(c.fontSize??11)*a;s.save(),s.setLineDash([]),s.globalAlpha=1,s.font=Pi(c,h),s.textBaseline="middle",s.textAlign="left";const d=5*a,u=3*a,p=1.35*h;let f=0;for(const t of e)f=Math.max(f,mt(s,t));const x=f+2*d,y=p*e.length+2*u,g=o.align??"left",m="center"===g?i-x/2:"right"===g?i-x:i,b=o.place??"above",v="above"===b?n-y-4*a:"below"===b?n+4*a:n-y/2;s.beginPath(),D(s,m,v,x,y,3*a),s.fillStyle=r,s.fill(),s.fillStyle=c.color??O(r);for(let t=0;t<e.length;t++)s.fillText(e[t],m+d,v+u+p*(t+.5));return s.restore(),y}function Ii(t,e=0){const[i,n]=Math.abs(t).toFixed(e).split(".");return(t<0?"-":"")+i.replace(/\B(?=(\d{3})+(?!\d))/g,",")+(void 0===n?"":"."+n)}function Ai(t){const e=new Date(1e3*t),i=t=>String(t).padStart(2,"0");return`${e.getUTCFullYear()}-${i(e.getUTCMonth()+1)}-${i(e.getUTCDate())}`}function Ci(t){const e=Math.max(0,Math.round(Math.abs(t))),i=Math.floor(e/86400),n=Math.floor(e%86400/3600),r=Math.floor(e%3600/60);return i>0?`${i}d ${n}h`:n>0?`${n}h ${r}m`:`${r}m`}function Ni(t,e,i){const n=Math.max(8,5*t.style.lineWidth)*t.rc.dpr,r=Math.atan2(i.y-e.y,i.x-e.x);t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(i.x-n*Math.cos(r-.4),i.y-n*Math.sin(r-.4)),t.ctx.lineTo(i.x-n*Math.cos(r+.4),i.y-n*Math.sin(r+.4)),t.ctx.closePath(),t.ctx.fillStyle=t.style.color,t.ctx.fill()}const zi="#26a69a",Oi="#ef5350";function Di(t){if(!0!==t.style.showStats)return;const[e,i]=t.pts,[n,r]=t.drawing.points,o=r.price-n.price,s=0!==n.price?o/n.price*100:0,l=o>=0,a=l?"+":"",c=t.rc.dataLayer.timeToIndexFloat(n.time),h=t.rc.dataLayer.timeToIndexFloat(r.time),d=Math.abs(Math.round(h-c)),u=180*Math.atan2(e.y-i.y,i.x-e.x)/Math.PI,p=`${a}${t.formatPrice(o)} (${a}${s.toFixed(2)}%) ${Ii(d)} bars ${u.toFixed(1)} deg`;Li(t,p,(e.x+i.x)/2,(e.y+i.y)/2-10*t.rc.dpr,t.style.color,{tint:l?zi:Oi,center:!0})}function $i(t,e,i){const{ctx:n,style:r}=t,o=t.pts;if(n.save(),n.globalAlpha=i,n.lineCap="round",n.lineJoin="round",!0!==r.pressure)return n.strokeStyle=r.color,n.lineWidth=e,n.beginPath(),xt(n,o),n.stroke(),void n.restore();const s=o.length,l=[],a=[];for(let i=0;i<s;i++){const n=o[0===i?0:i-1],r=o[i===s-1?s-1:i+1],c=Math.hypot(r.x-n.x,r.y-n.y)||1,h=-(r.y-n.y)/c,d=(r.x-n.x)/c,u=t.drawing.points[i]?.pressure,p=yt(e,u??.5)/2;l.push({x:o[i].x+h*p,y:o[i].y+d*p}),a.push({x:o[i].x-h*p,y:o[i].y-d*p})}a.reverse(),n.fillStyle=r.color,n.beginPath(),xt(n,l.concat(a)),n.closePath(),n.fill(),n.restore()}function Hi(t,e){return(t??e).filter(t=>!1!==t.enabled&&Number.isFinite(t.ratio))}function Wi(t){return t.color??n(t.ratio)}function Bi(t){return t.label??s(t.ratio)}function Ri(t,e,i,n){return{id:t,name:e,points:2,angleLock:!0,alertValue:g(i,n),defaultStyle:{extendLeft:i,extendRight:n},settings:et([U,q,mi]),draw:t=>{const[e,r]=N(t.pts[0],t.pts[1],t.rc.plotWidth*t.rc.dpr,t.style.extendLeft??i,t.style.extendRight??n);wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(r.x,r.y),t.ctx.stroke(),t.ctx.setLineDash([]),Di(t)},distance:(t,e,r)=>{const o=r.drawing.style.extendLeft??i,s=r.drawing.style.extendRight??n;if(o&&s)return S(t,e,r.pts[0],r.pts[1]);const[l,a]=N(r.pts[0],r.pts[1],r.rc.plotWidth,o,s);return k(t,e,l,a)}}}const Vi={...Ri("trend-line","Trend Line",!1,!1),shortcut:"Alt+T"},ji=Ri("ray","Ray",!1,!0),Ei=Ri("extended-line","Extended Line",!0,!0),Ui={id:"arrow",name:"Arrow",points:2,angleLock:!0,settings:et([U,mi]),draw:t=>{const[e,i]=t.pts;wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(i.x,i.y),t.ctx.stroke(),Ni(t,e,i),t.ctx.setLineDash([]),Di(t)},distance:(t,e,i)=>k(t,e,i.pts[0],i.pts[1])},Ji={id:"horizontal-line",name:"Horizontal Line",points:1,shortcut:"Alt+H",alertValue:m(),defaultStyle:{showLabels:!0},settings:et([U,gi,G]),draw:t=>{const e=Math.round(t.pts[0].y)+.5;wi(t),t.ctx.beginPath(),t.ctx.moveTo(0,e),t.ctx.lineTo(t.rc.plotWidth*t.rc.dpr,e),t.ctx.stroke(),t.ctx.setLineDash([]),!1!==t.style.showLabels&&Li(t,t.formatPrice(t.drawing.points[0].price),4*t.rc.dpr,e-8*t.rc.dpr)},distance:(t,e,i)=>P(e,i.pts[0].y)},qi={id:"horizontal-ray",name:"Horizontal Ray",points:1,shortcut:"Alt+J",alertValue:m(!0),defaultStyle:{showLabels:!0},settings:et([U,gi,G]),draw:t=>{const e=Math.round(t.pts[0].y)+.5;wi(t),t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,e),t.ctx.lineTo(t.rc.plotWidth*t.rc.dpr,e),t.ctx.stroke(),t.ctx.setLineDash([]),!1!==t.style.showLabels&&Li(t,t.formatPrice(t.drawing.points[0].price),t.pts[0].x+4*t.rc.dpr,e-8*t.rc.dpr)},distance:(t,e,i)=>t<i.pts[0].x?null:P(e,i.pts[0].y)},Yi={id:"vertical-line",name:"Vertical Line",points:1,shortcut:"Alt+V",settings:fi,draw:t=>{const e=Math.round(t.pts[0].x)+.5;wi(t),t.ctx.beginPath(),t.ctx.moveTo(e,0),t.ctx.lineTo(e,t.rc.plotHeight*t.rc.dpr),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>T(t,i.pts[0].x)},Ki={id:"cross-line",name:"Cross Line",points:1,shortcut:"Alt+C",settings:fi,draw:t=>{const e=Math.round(t.pts[0].x)+.5,i=Math.round(t.pts[0].y)+.5;wi(t),t.ctx.beginPath(),t.ctx.moveTo(e,0),t.ctx.lineTo(e,t.rc.plotHeight*t.rc.dpr),t.ctx.moveTo(0,i),t.ctx.lineTo(t.rc.plotWidth*t.rc.dpr,i),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>Math.min(T(t,i.pts[0].x),P(e,i.pts[0].y))},Gi=et([U,J,Q]),Xi={id:"rectangle",name:"Rectangle",points:2,defaultStyle:{fill:!0},settings:Gi,draw:t=>{const e=F(t.pts[0],t.pts[1]);Mi(t,()=>t.ctx.fillRect(e.x0,e.y0,e.x1-e.x0,e.y1-e.y0)),wi(t),t.ctx.strokeRect(e.x0,e.y0,e.x1-e.x0,e.y1-e.y0),t.ctx.setLineDash([]),xn(t,e)},distance:(t,e,i)=>A(t,e,i.pts[0],i.pts[1],!0===i.drawing.style.fill)},Zi={id:"ellipse",name:"Ellipse",points:2,defaultStyle:{fill:!0},settings:Gi,draw:t=>{const e=F(t.pts[0],t.pts[1]),i=(e.x0+e.x1)/2,n=(e.y0+e.y1)/2,r=Math.max(1,(e.x1-e.x0)/2),o=Math.max(1,(e.y1-e.y0)/2),s=()=>{t.ctx.beginPath(),t.ctx.ellipse(i,n,r,o,0,0,2*Math.PI)};Mi(t,()=>{s(),t.ctx.fill()}),wi(t),s(),t.ctx.stroke(),t.ctx.setLineDash([]),xn(t,e,"center","middle")},distance:(t,e,i)=>C(t,e,i.pts[0],i.pts[1],!0===i.drawing.style.fill)},Qi={id:"parallel-channel",name:"Parallel Channel",points:3,alertValue:v("parallel"),alertLevels:b,defaultStyle:{fill:!0},settings:Gi,draw:t=>{const[e,i,n]=t.pts,r=n.y-(e.y+.5*(i.y-e.y)),o={x:e.x,y:e.y+r},s={x:i.x,y:i.y+r};Mi(t,()=>{t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(i.x,i.y),t.ctx.lineTo(s.x,s.y),t.ctx.lineTo(o.x,o.y),t.ctx.closePath(),t.ctx.fill()}),wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(i.x,i.y),t.ctx.moveTo(o.x,o.y),t.ctx.lineTo(s.x,s.y),t.ctx.stroke(),t.ctx.setLineDash([]),xn(t,I([e,i,o,s]))},distance:(t,e,i)=>{const[n,r,o]=i.pts,s=o.y-(n.y+.5*(r.y-n.y)),l=k(t,e,n,r),a=k(t,e,{x:n.x,y:n.y+s},{x:r.x,y:r.y+s});return Math.min(l,a)}},tn=et([U,K,J,q,G]);function en(t,e,i){return{id:t,name:e,points:i,alertValue:M(i,h),alertLevels:_(h),defaultStyle:{showLabels:!0,levels:c(h),fill:!0,fillOpacity:.06},settings:tn,draw:t=>{const e=Hi(t.style.levels,h),n=t.drawing.points,r=t.rc.dpr,o=2===i?n[0].price:n[2].price,s=n[1].price-n[0].price,l=Math.min(t.pts[0].x,t.pts[i-1].x),a=Math.max(t.pts[0].x,t.pts[i-1].x),c=!0===t.style.extendLeft?0:l,d=!0===t.style.extendRight?t.rc.plotWidth*r:a;wi(t),t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,t.pts[0].y),t.ctx.lineTo(t.pts[1].x,t.pts[1].y),3===i&&t.ctx.lineTo(t.pts[2].x,t.pts[2].y),t.ctx.stroke();let u=null;for(const i of e){const e=o+s*i.ratio,n=Math.round(t.rc.priceScale.priceToY(e)*r)+.5,l=Wi(i);!0===t.style.fill&&null!==u&&(t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.06,t.ctx.fillStyle=t.style.fillColor??l,t.ctx.fillRect(c,Math.min(u,n),d-c,Math.abs(n-u)),t.ctx.restore()),u=n,t.ctx.strokeStyle=l,t.ctx.beginPath(),t.ctx.moveTo(c,n),t.ctx.lineTo(d,n),t.ctx.stroke(),!1!==t.style.showLabels&&Li(t,`${Bi(i)} ${t.formatPrice(e)}`,c+4*r,n-8*r,l)}t.ctx.setLineDash([])},distance:(t,e,n)=>{const r=Hi(n.drawing.style.levels,h),o=n.drawing.points,s=2===i?o[0].price:o[2].price,l=o[1].price-o[0].price,a=!0===n.drawing.style.extendLeft?0:Math.min(n.pts[0].x,n.pts[i-1].x),c=!0===n.drawing.style.extendRight?n.rc.plotWidth:Math.max(n.pts[0].x,n.pts[i-1].x);if(t<a-4||t>c+4)return null;let d=1/0;for(const t of r){const i=Math.abs(e-n.rc.priceScale.priceToY(s+l*t.ratio));i<d&&(d=i)}return d}}}const nn=en("fib-retracement","Fib Retracement",2),rn=en("fib-extension","Fib Extension",3),on=et([j,E,yi,Y,G]),sn={id:"measure",name:"Measure",points:2,defaultStyle:{fill:!0,showLabels:!0,fillOpacity:.14},settings:on,draw:t=>{const e=F(t.pts[0],t.pts[1]),i=t.rc.dpr,n=t.drawing.points,r=n[1].price-n[0].price,o=0!==n[0].price?r/n[0].price*100:0,s=r>=0,l=s?zi:Oi;t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.14,t.ctx.fillStyle=l,t.ctx.fillRect(e.x0,e.y0,e.x1-e.x0,e.y1-e.y0),t.ctx.restore(),wi(t),t.ctx.strokeStyle=l,t.ctx.strokeRect(e.x0,e.y0,e.x1-e.x0,e.y1-e.y0),t.ctx.setLineDash([]);const a=(e.x0+e.x1)/2,c=t.pts[0].y,h=t.pts[1].y;if(t.ctx.strokeStyle=l,t.ctx.beginPath(),t.ctx.moveTo(e.x0,c),t.ctx.lineTo(e.x1,c),t.ctx.stroke(),Ni(t,{x:e.x0,y:c},{x:e.x1,y:c}),t.ctx.beginPath(),t.ctx.moveTo(a,c),t.ctx.lineTo(a,h),t.ctx.stroke(),Ni(t,{x:a,y:c},{x:a,y:h}),!1===t.style.showLabels)return;const d=t.rc.dataLayer.timeToIndexFloat(n[0].time),u=t.rc.dataLayer.timeToIndexFloat(n[1].time),p=Math.abs(Math.round(u-d)),f=s?"+":"",x=[`${f}${t.formatPrice(r)} (${f}${o.toFixed(2)}%)`,`${Ii(p)} bars, ${Ci(n[1].time-n[0].time)}`],y=t.rc.bars?.();if(void 0!==y&&y.length>0){const t=Math.min(n[0].time,n[1].time),e=Math.max(n[0].time,n[1].time);let i=0,r=!1,o=0,s=y.length;for(;o<s;){const e=o+s>>>1;y[e].time<t?o=e+1:s=e}for(let t=o;t<y.length;t++){const n=y[t];if(n.time>e)break;void 0!==n.volume&&(i+=n.volume,r=!0)}r&&x.push(`Vol ${function(t){const e=Math.abs(t);return e>=1e9?`${(t/1e9).toFixed(2)}B`:e>=1e6?`${(t/1e6).toFixed(2)}M`:e>=1e3?`${(t/1e3).toFixed(2)}K`:Ii(t)}(i)}`)}Fi(t,x,a,Math.max(c,h)+6*i,l,{align:"center",place:"below"})},distance:(t,e,i)=>A(t,e,i.pts[0],i.pts[1],!0)};function ln(t){return!Number.isFinite(t)||t<=0?"0":t>=100?Ii(t):t>=1?Ii(t,2).replace(/\.?0+$/,""):t.toPrecision(3).replace(/\.?0+$/,"")}function an(t,e,i){return{id:t,name:e,points:2,defaultStyle:{showLabels:!0,fillOpacity:.13,accountSize:1e5,risk:1},settings:et([U,yi,Y,vi,G]),expand:(t,e)=>function(t,e,i){const n=t[0],r=t[1]??n,o=i?1:-1;if(void 0!==e.toPixel&&void 0!==e.fromPixel){const t=e.toPixel(n),i=e.toPixel(r);if(null!==t&&null!==i){const r=Math.abs(i.y-t.y)>=6,s=r?i.y:t.y-64*o*2,l=t.x+(r?Math.max(Math.abs(i.x-t.x),48):150),a=t.y-(s-t.y)/2,c=e.fromPixel({x:l,y:s}),h=e.fromPixel({x:l,y:a});if(null!==c&&null!==h)return[{...n},c,h]}}const s=r.price!==n.price,l=s?r.price-n.price:o*(.02*Math.abs(n.price)||2),a=Math.max(5,Math.round(.08*e.visibleBars)),c=Math.max(1,e.barSeconds)*a,h=s&&r.time>n.time?r.time:n.time+c;return[{...n},{time:h,price:n.price+l},{time:h,price:n.price-l/2}]}(t,e,i),constrain:(t,e)=>function(t,e){const i=t.map(t=>({...t}));if(i.length<3)return i;const[n,r,o]=i,s=Math.sign(r.price-n.price),l=Math.sign(o.price-n.price);return 0===s||0===l||s!==l||(1===e?o.price=2*n.price-o.price:r.price=2*n.price-r.price),i}(t,e),draw:t=>{const e=t.drawing.points;if(e.length<2||t.pts.length<2)return;const[n,r]=e,o=e.length>=3?e[2]:{time:r.time,price:n.price-(r.price-n.price)/2},s=t.rc.dpr,l=t.pts.map(t=>t.x),a=Math.min(...l),c=Math.max(...l),h=t.rc.priceScale.priceToY(n.price)*s,d=t.rc.priceScale.priceToY(r.price)*s,u=t.rc.priceScale.priceToY(o.price)*s,p=t.drawing.props??{},f="string"==typeof p.profitColor?p.profitColor:zi,x="string"==typeof p.lossColor?p.lossColor:Oi,y=(e,i,n)=>{t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.13,t.ctx.fillStyle=n,t.ctx.fillRect(a,Math.min(e,i),c-a,Math.abs(i-e)),t.ctx.restore()};y(h,d,f),y(h,u,x),wi(t);for(const[e,i]of[[d,f],[u,x],[h,t.style.color]]){const n=Math.round(e)+.5;t.ctx.strokeStyle=i,t.ctx.beginPath(),t.ctx.moveTo(a,n),t.ctx.lineTo(c,n),t.ctx.stroke()}if(t.ctx.setLineDash([]),!1===t.style.showLabels)return;const g=Math.abs(n.price-o.price),m=Math.abs(r.price-n.price),b=g>0?m/g:0,v=t=>(0!==n.price?t/n.price*100:0).toFixed(2),w=r.price===n.price?i?"Long":"Short":r.price>n.price?"Long":"Short",_=t.style.accountSize??0,M=_>0?_*(t.style.risk??0)/100:0,k=g>0?M/g:0,S=(a+c)/2,P=e=>!0===p.showPrices?` @ ${t.formatPrice(e)}`:"";let T=Math.min(h,d,u);!1!==p.showLossSize&&M>0&&(T-=Fi(t,[`Loss ${Ii(M,2)} Size ${ln(k)}`],S,T,t.rc.theme.background,{align:"center",place:"above"})+4*s),!1!==p.showHeader&&Fi(t,[`${w} R:R ${b.toFixed(2)}`],S,T,t.rc.theme.background,{align:"center",place:"above"}),!1!==p.showTargetLabel&&m>0&&Fi(t,[`Target +${v(m)}%${P(r.price)}`],S,(h+d)/2,f,{align:"center",place:"middle"}),!1!==p.showStopLabel&&g>0&&Fi(t,[`Stop -${v(g)}%${P(o.price)}`],S,(h+u)/2,x,{align:"center",place:"middle"})},distance:(t,e,i)=>{const n=i.drawing.points;if(i.pts.length<2||n.length<2)return null;const r=i.pts.map(t=>t.x),o=Math.min(...r),s=Math.max(...r);if(t<o-4||t>s+4)return null;const[l,a]=n,c=n.length>=3?n[2].price:l.price-(a.price-l.price)/2,h=[l.price,a.price,c].map(t=>i.rc.priceScale.priceToY(t)),d=Math.min(...h),u=Math.max(...h);return e>=d&&e<=u?0:Math.min(Math.abs(e-d),Math.abs(e-u))}}}const cn=an("long-position","Long Position",!0),hn=an("short-position","Short Position",!1),dn=1.35,un=14;function pn(t,e,i,n){const r=i.split("\n");if(!0!==e.wrap)return r;const o=[];for(const e of r){const i=e.split(/\s+/).filter(t=>""!==t);if(0===i.length){o.push("");continue}let r=i[0];for(let e=1;e<i.length;e++){const s=`${r} ${i[e]}`;t.measureText(s).width>n?(o.push(r),r=i[e]):r=s}o.push(r)}return o}function fn(t,e,i,n){const r=(e.fontSize??un)*n;t.font=Pi(e,r);const o=pn(t,e,i,(e.wrapWidth??220)*n);let s=0;for(const e of o)s=Math.max(s,t.measureText(e).width);const l=r*dn;return{lines:o,width:s+10*n,height:o.length*l+10*n,lineHeight:l}}function xn(t,e,i="left",n="top"){const{ctx:r,rc:o,style:s}=t,l=Si(t.drawing);if(""===l.value)return;const a=o.dpr,c=(l.fontSize??un)*a,h=6*a;r.save(),r.setLineDash([]),r.font=Pi(l,c),r.fillStyle=l.color??s.color,r.textBaseline="top";const d=pn(r,l,l.value,Math.max(20*a,e.x1-e.x0-2*h)),u=c*dn,p=d.length*u,f=l.align??i;r.textAlign=f;const x="center"===f?(e.x0+e.x1)/2:"right"===f?e.x1-h:e.x0+h;let y;if("outside"===l.position)y=e.y0-p-h;else{const t=l.valign??n;y="middle"===t?(e.y0+e.y1-p)/2:"bottom"===t?e.y1-p-h:e.y0+h}for(const t of d)r.fillText(t,x,y),y+=u;r.restore()}const yn={id:"text",name:"Text",points:1,defaultText:{value:"Text",fontSize:un},settings:et([Z],{textIsContent:!0}),draw:t=>{const{ctx:e,rc:i,style:n}=t,r=i.dpr,o=Si(t.drawing),s=fn(e,o,Ti(t.drawing,"Text"),r),l=t.pts[0].x,a=o.valign??"top",c="middle"===a?t.pts[0].y-s.height/2:"bottom"===a?t.pts[0].y-s.height:t.pts[0].y;e.save(),e.setLineDash([]),!0===o.background&&(e.globalAlpha=o.backgroundOpacity??1,e.fillStyle=o.backgroundColor??i.theme.background,e.beginPath(),e.roundRect(l,c,s.width,s.height,4*r),e.fill(),e.globalAlpha=1),!0===o.border&&(e.strokeStyle=o.borderColor??n.color,e.lineWidth=Math.max(1,n.lineWidth*r),e.beginPath(),e.roundRect(l,c,s.width,s.height,4*r),e.stroke()),e.font=Pi(o,(o.fontSize??un)*r),e.fillStyle=o.color??n.color,e.textBaseline="top";const h=o.align??"left";e.textAlign=h;const d="center"===h?l+s.width/2:"right"===h?l+s.width-5*r:l+5*r;let u=c+5*r;for(const t of s.lines)e.fillText(t,d,u),u+=s.lineHeight;e.restore()},distance:(t,e,i)=>{const n=Si(i.drawing),r=Ti(i.drawing,"Text"),o=n.fontSize??un,s=i.pts[0],l=mn(),a=null===l?{width:r.length*o*.6+10,height:o*dn+10}:fn(l,n,r,1),c=n.valign??"top",h="middle"===c?s.y-a.height/2:"bottom"===c?s.y-a.height:s.y;return t>=s.x-3&&t<=s.x+a.width+3&&e>=h-3&&e<=h+a.height+3?0:null}};let gn;function mn(){if(void 0!==gn)return gn;try{gn="undefined"==typeof document?null:document.createElement("canvas").getContext("2d")}catch{gn=null}return gn}const bn={id:"path",name:"Path",points:0,settings:fi,draw:t=>{if(!(t.pts.length<2)){wi(t),t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,t.pts[0].y);for(let e=1;e<t.pts.length;e++)t.ctx.lineTo(t.pts[e].x,t.pts[e].y);t.ctx.stroke(),t.ctx.setLineDash([]),Ni(t,t.pts[t.pts.length-2],t.pts[t.pts.length-1])}},distance:(t,e,i)=>i.pts.length<2?null:L(t,e,i.pts)},vn={id:"brush",name:"Brush",points:0,freehand:!0,defaultStyle:{lineWidth:2},settings:et([U,bi]),draw:t=>{t.pts.length<2||(wi(t),$i(t,Math.max(1,Math.round(t.style.lineWidth*t.rc.dpr)),1),t.ctx.setLineDash([]))},distance:(t,e,i)=>i.pts.length<2?null:L(t,e,i.pts)},wn={id:"rotated-rectangle",name:"Rotated Rectangle",points:3,defaultStyle:{fill:!0,fillOpacity:.12},settings:Gi,draw:t=>{if(t.pts.length<3)return;const e=_n(t.pts[0],t.pts[1],t.pts[2]);t.ctx.beginPath(),t.ctx.moveTo(e[0].x,e[0].y);for(let i=1;i<4;i++)t.ctx.lineTo(e[i].x,e[i].y);t.ctx.closePath(),Mi(t,()=>t.ctx.fill()),wi(t),t.ctx.stroke(),t.ctx.setLineDash([]),xn(t,I(e),"center","middle")},distance:(t,e,i)=>{if(i.pts.length<3)return null;const n=_n(i.pts[0],i.pts[1],i.pts[2]);if(!0===i.drawing.style.fill&&function(t,e,i){let n=!1;for(let r=0,o=i.length-1;r<i.length;o=r++){const s=i[r],l=i[o];s.y>e!=l.y>e&&t<(l.x-s.x)*(e-s.y)/(l.y-s.y)+s.x&&(n=!n)}return n}(t,e,n))return 0;let r=1/0;for(let i=0;i<4;i++)r=Math.min(r,k(t,e,n[i],n[(i+1)%4]));return r}};function _n(t,e,i){const n=e.x-t.x,r=e.y-t.y,o=Math.hypot(n,r)||1,s=-r/o,l=n/o,a=(i.x-e.x)*s+(i.y-e.y)*l;return[t,e,{x:e.x+s*a,y:e.y+l*a},{x:t.x+s*a,y:t.y+l*a}]}const Mn={id:"double-curve",name:"Double Curve",points:3,settings:fi,draw:t=>{if(t.pts.length<3)return;const[e,i,n]=t.pts;wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.bezierCurveTo(i.x,i.y,e.x+n.x-i.x,e.y+n.y-i.y,n.x,n.y),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>{if(i.pts.length<3)return null;const[n,r,o]=i.pts;return L(t,e,function(t,e,i,n,r){const o=[];for(let s=0;s<=r;s++){const l=s/r,a=1-l,c=a*a*a,h=3*a*a*l,d=3*a*l*l,u=l*l*l;o.push({x:c*t.x+h*e.x+d*i.x+u*n.x,y:c*t.y+h*e.y+d*i.y+u*n.y})}return o}(n,r,{x:n.x+o.x-r.x,y:n.y+o.y-r.y},o,24))}};const kn={id:"cyclic-lines",name:"Cyclic Lines",points:2,defaultStyle:{lineStyle:"dashed"},settings:fi,draw:t=>{const[e,i]=t.pts,n=i.x-e.x;if(!Number.isFinite(n)||Math.abs(n)<.5)return;const r=t.rc.plotHeight*t.rc.dpr,o=t.rc.plotWidth*t.rc.dpr;wi(t),t.ctx.beginPath();for(let i=0;i<=12;i++){const s=Math.round(e.x+n*i)+.5;s<0||s>o||(t.ctx.moveTo(s,0),t.ctx.lineTo(s,r))}t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>{const n=i.pts[1].x-i.pts[0].x;if(!Number.isFinite(n)||Math.abs(n)<.5)return null;const r=Math.round((t-i.pts[0].x)/n);return r<0||r>12?null:Math.abs(t-(i.pts[0].x+n*r))}},Sn={id:"time-cycles",name:"Time Cycles",points:2,settings:fi,draw:t=>{const[e,i]=t.pts,n=i.x-e.x;if(!Number.isFinite(n)||Math.abs(n)<1)return;const r=Math.abs(n)/2;wi(t),t.ctx.beginPath();for(let i=0;i<12;i++){const o=e.x+n*i+n/2;o+r<0||o-r>t.rc.plotWidth*t.rc.dpr||(t.ctx.moveTo(o+r,e.y),t.ctx.arc(o,e.y,r,0,Math.PI,!0))}t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>{const[n,r]=i.pts,o=r.x-n.x;if(!Number.isFinite(o)||Math.abs(o)<1)return null;const s=Math.abs(o)/2;let l=1/0;for(let i=0;i<12;i++){const r=n.x+o*i+o/2;e>n.y+2||(l=Math.min(l,Math.abs(Math.hypot(t-r,e-n.y)-s)))}return Number.isFinite(l)?l:null}},Pn={id:"sine-line",name:"Sine Line",points:2,settings:fi,draw:t=>{const e=Tn(t.pts[0],t.pts[1]);if(null!==e){wi(t),t.ctx.beginPath(),t.ctx.moveTo(e[0].x,e[0].y);for(let i=1;i<e.length;i++)t.ctx.lineTo(e[i].x,e[i].y);t.ctx.stroke(),t.ctx.setLineDash([])}},distance:(t,e,i)=>{const n=Tn(i.pts[0],i.pts[1]);return null===n?null:L(t,e,n)}};function Tn(t,e){const i=e.x-t.x,n=e.y-t.y;if(!Number.isFinite(i)||Math.abs(i)<1)return null;const r=[];for(let e=0;e<=64;e++){const o=e/64;r.push({x:t.x+i*o,y:t.y+n*Math.sin(o*Math.PI*2)})}return r}const Ln=et([V,X,G],{textIsContent:!0}),Fn={id:"price-label",name:"Price Label",points:1,settings:et([V,{...X,label:"Text (blank shows the price)"},G]),draw:t=>{const e=t.pts[0],i=t.rc.dpr,n=Ti(t.drawing,t.formatPrice(t.drawing.points[0].price)),r=An(t,[n],e.x+18*i,e.y-30*i,12);Cn(t,r,e,t.style.color),Nn(t,r,[n],t.style.color,12)},distance:(t,e,i)=>{const n=i.pts[0],r=n.x+18,o=n.y-30-11;return t>=r&&t<=r+64&&e>=o&&e<=o+22||Math.hypot(t-n.x,e-n.y)<=10?0:null}},In={id:"callout",name:"Callout",points:2,defaultText:{value:"Note",fontSize:12},settings:Ln,draw:t=>{if(t.pts.length<2)return;const[e,i]=t.pts,n=Ti(t.drawing,"Note").split("\n"),r=An(t,n,i.x,i.y,12);Cn(t,r,e,t.style.color),Nn(t,r,n,t.style.color,12)},distance:(t,e,i)=>{if(i.pts.length<2)return null;const[n,r]=i.pts;return Math.abs(t-r.x)<=60&&Math.abs(e-r.y)<=16?0:k(t,e,n,r)}};function An(t,e,i,n,r){const o=t.rc.dpr,s=Si(t.drawing),l=(s.fontSize??r)*o;t.ctx.save(),t.ctx.font=Pi(s,l);let a=0;for(const i of e)a=Math.max(a,t.ctx.measureText(i).width);t.ctx.restore();const c=a+14*o,h=1.4*l*e.length+8*o;return{x:i-c/2,y:n-h/2,w:c,h:h}}function Cn(t,e,i,n){const r=t.rc.dpr,o=e.x+e.w/2,s=e.y+e.h/2,l=Math.atan2(i.y-s,i.x-o),a=5*r,c=o+Math.cos(l)*(e.w/2-a),h=s+Math.sin(l)*(e.h/2-a);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.fillStyle=n,t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(c-Math.sin(l)*a,h+Math.cos(l)*a),t.ctx.lineTo(c+Math.sin(l)*a,h-Math.cos(l)*a),t.ctx.closePath(),t.ctx.fill(),t.ctx.beginPath(),D(t.ctx,e.x,e.y,e.w,e.h,5*r),t.ctx.fill(),t.ctx.beginPath(),t.ctx.arc(i.x,i.y,2.5*r,0,2*Math.PI),t.ctx.fill(),t.ctx.restore()}function Nn(t,e,i,n,r){const o=t.rc.dpr,s=Si(t.drawing),l=(s.fontSize??r)*o;t.ctx.save(),t.ctx.setLineDash([]),t.ctx.font=Pi(s,l),t.ctx.textAlign="center",t.ctx.textBaseline="middle",t.ctx.fillStyle=s.color??O(n);const a=1.4*l,c=e.y+e.h/2-a*(i.length-1)/2;for(let n=0;n<i.length;n++)t.ctx.fillText(i[n],e.x+e.w/2,c+a*n);t.ctx.restore()}const zn=et([U,J]),On={id:"flag-mark",name:"Flag Mark",points:1,defaultStyle:{fill:!0,fillOpacity:.95},settings:zn,draw:t=>{const e=t.pts[0],i=t.rc.dpr,n=22*i,r=15*i,o=11*i;wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(e.x,e.y-n),t.ctx.stroke(),t.ctx.setLineDash([]),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y-n),t.ctx.lineTo(e.x+r,e.y-n+.28*o),t.ctx.lineTo(e.x+.72*r,e.y-n+.5*o),t.ctx.lineTo(e.x+r,e.y-n+.72*o),t.ctx.lineTo(e.x,e.y-n+o),t.ctx.closePath(),!1!==t.style.fill&&(t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.95,t.ctx.fillStyle=_i(t),t.ctx.fill(),t.ctx.restore()),t.ctx.stroke()},distance:(t,e,i)=>{const n=i.pts[0];return t>=n.x-4&&t<=n.x+16&&e>=n.y-24&&e<=n.y+2||Math.hypot(t-n.x,e-n.y)<=8?0:null}};function Dn(t,e,i,n,r){const o=t.rc.dpr,s=Si(t.drawing),l=(s.fontSize??r.size)*o;t.ctx.save(),t.ctx.font=Pi(s,l);let a=0;for(const i of e)a=Math.max(a,t.ctx.measureText(i).width);return t.ctx.restore(),{x:i,y:n,w:a+16*o,h:1.45*l*e.length+10*o}}function $n(t){return t.drawing.text?.backgroundColor??t.style.color}function Hn(t,e,i,n,r=6){const o=t.rc.dpr,s=Si(t.drawing),l=(s.fontSize??n.size)*o,a=$n(t);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.globalAlpha=s.backgroundOpacity??n.opacity,t.ctx.fillStyle=a,t.ctx.beginPath(),D(t.ctx,e.x,e.y,e.w,e.h,r*o),t.ctx.fill(),t.ctx.globalAlpha=1,!0===s.border&&(t.ctx.strokeStyle=s.borderColor??t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*o),t.ctx.stroke()),t.ctx.font=Pi(s,l),t.ctx.textAlign="left",t.ctx.textBaseline="middle",t.ctx.fillStyle=s.color??O(a);const c=1.45*l,h=e.y+e.h/2-c*(i.length-1)/2;for(let n=0;n<i.length;n++)t.ctx.fillText(i[n],e.x+8*o,h+c*n);t.ctx.restore()}function Wn(t,e,i,n,r,o){return t>=i&&t<=i+r&&e>=n&&e<=n+o?0:null}function Bn(t,e,i){return{w:120,h:1.45*(t.text?.fontSize??i.size)*Math.max(1,e)+10}}const Rn=(t,e)=>Ti(t,e).split("\n").length,Vn=et([V,j,tt],{textIsContent:!0}),jn=et([V,tt],{textIsContent:!0}),En={size:12,opacity:.95},Un={id:"note",name:"Note",points:1,defaultText:{value:"Note",fontSize:En.size},settings:Vn,draw:t=>{const e=t.rc.dpr,i=t.pts[0],n=Ti(t.drawing,"Note").split("\n"),r=Dn(t,n,i.x+16*e,i.y-34*e,En);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.strokeStyle=t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*e),t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(r.x,r.y+r.h),t.ctx.stroke(),t.ctx.fillStyle=t.style.color,t.ctx.beginPath(),t.ctx.arc(i.x,i.y,3.5*e,0,2*Math.PI),t.ctx.fill(),t.ctx.restore(),Hn(t,r,n,En)},distance:(t,e,i)=>{const n=i.pts[0],r=Bn(i.drawing,Rn(i.drawing,"Note"),En),o=Wn(t,e,n.x+16,n.y-34,r.w,r.h);return null!==o?o:Math.hypot(t-n.x,e-n.y)<=8?0:null}},Jn={size:12,opacity:.95},qn={id:"balloon",name:"Balloon",points:1,defaultText:{value:"Balloon",fontSize:Jn.size},settings:jn,draw:t=>{const e=t.rc.dpr,i=t.pts[0],n=Ti(t.drawing,"Balloon").split("\n"),r=Dn(t,n,0,0,Jn),o={x:i.x-r.w/2,y:i.y-r.h-12*e,w:r.w,h:r.h};t.ctx.save(),t.ctx.setLineDash([]),t.ctx.globalAlpha=t.drawing.text?.backgroundOpacity??Jn.opacity,t.ctx.fillStyle=$n(t),t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(i.x-6*e,o.y+o.h),t.ctx.lineTo(i.x+6*e,o.y+o.h),t.ctx.closePath(),t.ctx.fill(),t.ctx.restore(),Hn(t,o,n,Jn,8)},distance:(t,e,i)=>{const n=i.pts[0],r=Bn(i.drawing,Rn(i.drawing,"Balloon"),Jn),o=Wn(t,e,n.x-r.w/2,n.y-r.h-12,r.w,r.h);return null!==o?o:Math.hypot(t-n.x,e-n.y)<=8?0:null}},Yn={size:11,opacity:.92},Kn={id:"comment",name:"Comment",points:1,defaultText:{value:"Comment",fontSize:Yn.size},settings:jn,draw:t=>{const e=t.rc.dpr,i=t.pts[0],n=Ti(t.drawing,"Comment").split("\n"),r=Dn(t,n,0,0,Yn),o={x:i.x+8*e,y:i.y-r.h-10*e,w:r.w,h:r.h};t.ctx.save(),t.ctx.setLineDash([]),t.ctx.globalAlpha=t.drawing.text?.backgroundOpacity??Yn.opacity,t.ctx.fillStyle=$n(t),t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(o.x,o.y+o.h-5*e),t.ctx.lineTo(o.x+10*e,o.y+o.h),t.ctx.closePath(),t.ctx.fill(),t.ctx.restore(),Hn(t,o,n,Yn,3)},distance:(t,e,i)=>{const n=i.pts[0],r=Bn(i.drawing,Rn(i.drawing,"Comment"),Yn),o=Wn(t,e,n.x+8,n.y-r.h-10,r.w,r.h);return null!==o?o:Math.hypot(t-n.x,e-n.y)<=8?0:null}},Gn={size:11,opacity:.95},Xn={id:"signpost",name:"Signpost",points:1,defaultText:{value:"Event",fontSize:Gn.size},settings:Vn,draw:t=>{const e=t.rc.dpr,i=t.pts[0],n=34*e,r=Ti(t.drawing,"Event").split("\n"),o=Dn(t,r,0,0,Gn);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.strokeStyle=t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*e),t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(i.x,i.y-n),t.ctx.stroke(),t.ctx.fillStyle=t.style.color,t.ctx.beginPath(),t.ctx.arc(i.x,i.y,3*e,0,2*Math.PI),t.ctx.fill(),t.ctx.restore(),Hn(t,{x:i.x-o.w/2,y:i.y-n-o.h,w:o.w,h:o.h},r,Gn,4)},distance:(t,e,i)=>{const n=i.pts[0],r=Bn(i.drawing,Rn(i.drawing,"Event"),Gn),o=Wn(t,e,n.x-r.w/2,n.y-34-r.h,r.w,r.h);return null!==o?o:Math.abs(t-n.x)<=5&&e>=n.y-34&&e<=n.y+4?0:null}},Zn={size:11,opacity:.95},Qn={id:"price-note",name:"Price Note",points:1,defaultText:{value:"Note",fontSize:Zn.size},settings:Vn,draw:t=>{const e=t.rc.dpr,i=t.pts[0],n=[t.formatPrice(t.drawing.points[0].price),...Ti(t.drawing,"Note").split("\n")],r=Dn(t,n,i.x+14*e,i.y-12*e,Zn);t.ctx.save(),t.ctx.setLineDash([]),t.ctx.strokeStyle=t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*e),t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(r.x,i.y),t.ctx.stroke(),t.ctx.restore(),Hn(t,r,n,Zn,4)},distance:(t,e,i)=>{const n=i.pts[0],r=Bn(i.drawing,Rn(i.drawing,"Note")+1,Zn),o=Wn(t,e,n.x+14,n.y-12,r.w,r.h);return null!==o?o:Math.hypot(t-n.x,e-n.y)<=8?0:null}};function tr(t,e,i,n){const r=(e.fontSize??11)*n,o=Array.from({length:Math.max(...i.map(t=>t.length))},()=>0);t?.save();try{for(let s=0;s<i.length;s++){null!==t&&(t.font=Pi(e,r,0===s?!0===e.bold?"800":"600":void 0));for(let e=0;e<o.length;e++){const l=i[s][e]??"",a=null===t?l.length*r*.6:mt(t,l);o[e]=Math.max(o[e],a+14*n)}}}finally{t?.restore()}return o}const er={id:"table",name:"Table",points:1,defaultText:{value:"Level|Price\nEntry|-\nStop|-",fontSize:11},settings:et([V,{...j,label:"Border width"},X,G,{path:"text.backgroundColor",label:"Background color",kind:"color",group:"text"},{path:"text.backgroundOpacity",label:"Background opacity",kind:"opacity",min:0,max:1,step:.01,group:"text"},{path:"text.border",label:"Border",kind:"boolean",group:"text"},{path:"text.borderColor",label:"Border color",kind:"color",group:"text"}],{textIsContent:!0}),draw:t=>{const e=t.rc.dpr,i=t.pts[0],n=Si(t.drawing),r=ir(t.drawing),o=(n.fontSize??11)*e,s=7*e,l=1.7*o;t.ctx.save();const a=tr(t.ctx,n,r,e),c=a.reduce((t,e)=>t+e,0),h=l*r.length;if(t.ctx.setLineDash([]),t.ctx.globalAlpha=n.backgroundOpacity??.92,t.ctx.fillStyle=n.backgroundColor??t.rc.theme.background,t.ctx.beginPath(),D(t.ctx,i.x,i.y,c,h,4*e),t.ctx.fill(),t.ctx.globalAlpha=1,!1!==n.border){t.ctx.strokeStyle=n.borderColor??t.style.color,t.ctx.lineWidth=Math.max(1,t.style.lineWidth*e),t.ctx.stroke(),t.ctx.globalAlpha=.45,t.ctx.beginPath();for(let e=1;e<r.length;e++)t.ctx.moveTo(i.x,i.y+l*e),t.ctx.lineTo(i.x+c,i.y+l*e);let o=i.x;for(let e=0;e<a.length-1;e++)o+=a[e],t.ctx.moveTo(o,i.y),t.ctx.lineTo(o,i.y+h);t.ctx.stroke(),t.ctx.globalAlpha=1}t.ctx.textBaseline="middle",t.ctx.textAlign="left";const d=Pi(n,o,!0===n.bold?"800":"600"),u=Pi(n,o);for(let e=0;e<r.length;e++){t.ctx.fillStyle=n.color??t.style.color,t.ctx.font=0===e?d:u;let o=i.x;for(let n=0;n<a.length;n++)t.ctx.fillText(r[e][n]??"",o+s,i.y+l*e+l/2),o+=a[n]}t.ctx.restore()},distance:(t,e,i)=>{const n=i.pts[0],r=ir(i.drawing),o=i.drawing.text?.fontSize??11,s=tr(mn(),Si(i.drawing),r,1).reduce((t,e)=>t+e,0);return Wn(t,e,n.x,n.y,s,1.7*o*r.length)}};function ir(t){return Ti(t,"Level|Price").split("\n").map(t=>t.split("|"))}const nr={id:"price-range",name:"Price Range",points:2,defaultStyle:{fill:!0,showLabels:!0,fillOpacity:.1},settings:on,draw:t=>{const e=t.rc.dpr,i=t.drawing.points,n=i[1].price-i[0].price,r=0!==i[0].price?n/i[0].price*100:0,o=n>=0,s=o?zi:Oi,l=Math.min(t.pts[0].x,t.pts[1].x),a=Math.max(t.pts[0].x,t.pts[1].x)+(t.pts[0].x===t.pts[1].x?60*e:0),c=t.pts[0].y,h=t.pts[1].y;t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.1,t.ctx.fillStyle=s,t.ctx.fillRect(l,Math.min(c,h),a-l,Math.abs(h-c)),t.ctx.restore(),wi(t),t.ctx.strokeStyle=s,t.ctx.beginPath();for(const e of[c,h])t.ctx.moveTo(l,Math.round(e)+.5),t.ctx.lineTo(a,Math.round(e)+.5);const d=(l+a)/2;t.ctx.moveTo(d,c),t.ctx.lineTo(d,h);const u=h>c?1:-1;if(t.ctx.moveTo(d-4*e,h-5*e*u),t.ctx.lineTo(d,h),t.ctx.lineTo(d+4*e,h-5*e*u),t.ctx.moveTo(d-4*e,c+5*e*u),t.ctx.lineTo(d,c),t.ctx.lineTo(d+4*e,c+5*e*u),t.ctx.stroke(),t.ctx.setLineDash([]),!1===t.style.showLabels)return;const p=o?"+":"";Li(t,`${p}${t.formatPrice(n)} (${p}${r.toFixed(2)}%)`,d+6*e,(c+h)/2,s)},distance:(t,e,i)=>A(t,e,i.pts[0],i.pts[1],!0)},rr={id:"date-range",name:"Date Range",points:2,defaultStyle:{fill:!0,showLabels:!0,fillOpacity:.1},settings:et([U,yi,Y,G]),draw:t=>{const e=t.rc.dpr,i=t.drawing.points,n=t.style.color,r=t.pts[0].x,o=t.pts[1].x,s=Math.min(t.pts[0].y,t.pts[1].y),l=Math.max(t.pts[0].y,t.pts[1].y)+(t.pts[0].y===t.pts[1].y?40*e:0);t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.1,t.ctx.fillStyle=n,t.ctx.fillRect(Math.min(r,o),s,Math.abs(o-r),l-s),t.ctx.restore(),wi(t),t.ctx.beginPath();for(const e of[r,o])t.ctx.moveTo(Math.round(e)+.5,s),t.ctx.lineTo(Math.round(e)+.5,l);const a=(s+l)/2;t.ctx.moveTo(r,a),t.ctx.lineTo(o,a);const c=o>r?1:-1;if(t.ctx.moveTo(o-5*e*c,a-4*e),t.ctx.lineTo(o,a),t.ctx.lineTo(o-5*e*c,a+4*e),t.ctx.moveTo(r+5*e*c,a-4*e),t.ctx.lineTo(r,a),t.ctx.lineTo(r+5*e*c,a+4*e),t.ctx.stroke(),t.ctx.setLineDash([]),!1===t.style.showLabels)return;const h=t.rc.dataLayer.timeToIndexFloat(i[0].time),d=t.rc.dataLayer.timeToIndexFloat(i[1].time);Li(t,`${Math.abs(Math.round(d-h))} bars`,(r+o)/2,s-10*e,n)},distance:(t,e,i)=>A(t,e,i.pts[0],i.pts[1],!0)},or={id:"forecast",name:"Forecast",points:2,defaultStyle:{fill:!0,showLabels:!0,fillOpacity:.12,lineStyle:"dashed"},settings:on,draw:t=>{const e=t.rc.dpr,i=t.drawing.points,[n,r]=t.pts,o=i[1].price-i[0].price,s=0!==i[0].price?o/i[0].price*100:0,l=o>=0,a=l?zi:Oi,c=.35*Math.abs(r.y-n.y)+6*e;if(t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.12,t.ctx.fillStyle=a,t.ctx.beginPath(),t.ctx.moveTo(n.x,n.y),t.ctx.lineTo(r.x,r.y-c),t.ctx.lineTo(r.x,r.y+c),t.ctx.closePath(),t.ctx.fill(),t.ctx.restore(),wi(t),t.ctx.strokeStyle=a,t.ctx.beginPath(),t.ctx.moveTo(n.x,n.y),t.ctx.lineTo(r.x,r.y),t.ctx.stroke(),t.ctx.setLineDash([]),t.ctx.beginPath(),t.ctx.arc(n.x,n.y,3*e,0,2*Math.PI),t.ctx.fillStyle=a,t.ctx.fill(),!1===t.style.showLabels)return;const h=l?"+":"";Fi(t,[t.formatPrice(i[0].price),Ai(i[0].time)],n.x,n.y,a,{align:"center"}),Fi(t,[`${h}${t.formatPrice(o)} (${h}${s.toFixed(2)}%) in ${Ci(i[1].time-i[0].time)}`,`${t.formatPrice(i[1].price)}, ${Ai(i[1].time)}`],r.x,r.y,a,{align:"center",place:"below"});const d=t.rc.bars?.();if(void 0!==d&&d.length>0&&d[d.length-1].time>=i[1].time){let n=!1;for(const t of d)if(!(t.time<i[0].time)){if(t.time>i[1].time)break;if(l?t.high>=i[1].price:t.low<=i[1].price){n=!0;break}}Fi(t,[n?"SUCCESS":"MISSED"],r.x,r.y+36*e,n?"#4a934a":"#8a4a4a",{align:"center",place:"below"})}},distance:(t,e,i)=>k(t,e,i.pts[0],i.pts[1])},sr={id:"circle",name:"Circle",points:2,defaultStyle:{fill:!0},settings:Gi,draw:t=>{const[e,i]=t.pts,n=Math.hypot(i.x-e.x,i.y-e.y);t.ctx.beginPath(),t.ctx.arc(e.x,e.y,n,0,2*Math.PI),Mi(t,()=>t.ctx.fill()),wi(t),t.ctx.stroke(),t.ctx.setLineDash([]),xn(t,{x0:e.x-n,y0:e.y-n,x1:e.x+n,y1:e.y+n},"center","middle")},distance:(t,e,i)=>{const[n,r]=i.pts,o=Math.hypot(r.x-n.x,r.y-n.y),s=Math.hypot(t-n.x,e-n.y);return!0===i.drawing.style.fill&&s<=o?0:Math.abs(s-o)}},lr={id:"triangle",name:"Triangle",points:3,defaultStyle:{fill:!0},settings:Gi,draw:t=>{t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,t.pts[0].y),t.ctx.lineTo(t.pts[1].x,t.pts[1].y),t.ctx.lineTo(t.pts[2].x,t.pts[2].y),t.ctx.closePath(),Mi(t,()=>t.ctx.fill()),wi(t),t.ctx.stroke(),t.ctx.setLineDash([]),xn(t,I(t.pts.slice(0,3)),"center","middle")},distance:(t,e,i)=>L(t,e,[...i.pts,i.pts[0]])},ar={id:"polyline",name:"Polyline",points:0,defaultStyle:{fill:!1},settings:et([U,J]),draw:t=>{if(!(t.pts.length<2)){t.ctx.beginPath(),t.ctx.moveTo(t.pts[0].x,t.pts[0].y);for(let e=1;e<t.pts.length;e++)t.ctx.lineTo(t.pts[e].x,t.pts[e].y);!0===t.style.fill&&(t.ctx.closePath(),Mi(t,()=>t.ctx.fill())),wi(t),t.ctx.stroke(),t.ctx.setLineDash([])}},distance:(t,e,i)=>L(t,e,i.pts)};const cr={id:"arc",name:"Arc",points:3,settings:fi,draw:t=>{const[e,i,n]=t.pts;wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.quadraticCurveTo(2*i.x-(e.x+n.x)/2,2*i.y-(e.y+n.y)/2,n.x,n.y),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>L(t,e,function(t,e,i){const n=2*e.x-(t.x+i.x)/2,r=2*e.y-(t.y+i.y)/2,o=[];for(let e=0;e<=16;e++){const s=e/16,l=1-s;o.push({x:l*l*t.x+2*l*s*n+s*s*i.x,y:l*l*t.y+2*l*s*r+s*s*i.y})}return o}(i.pts[0],i.pts[1],i.pts[2]))},hr={id:"curve",name:"Curve",points:3,settings:fi,draw:t=>{const[e,i,n]=t.pts;wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.quadraticCurveTo(i.x,i.y,n.x,n.y),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,i)=>{const[n,r,o]=i.pts,s=[];for(let t=0;t<=16;t++){const e=t/16,i=1-e;s.push({x:i*i*n.x+2*i*e*r.x+e*e*o.x,y:i*i*n.y+2*i*e*r.y+e*e*o.y})}return L(t,e,s)}};function dr(t,e,i,n="vertical"){return{id:t,name:e,points:1,defaultStyle:{fill:!0,fillOpacity:.9},settings:zn,draw:t=>{const e=t.rc.dpr,{x:r,y:o}=t.pts[0],s=16*e,l=6*e,a=i?1:-1,c=o+2*a*e;if(t.ctx.beginPath(),"vertical"!==n){const i="right"===n?-1:1,a=r+2*i*e;t.ctx.moveTo(a,o),t.ctx.lineTo(a+i*l,o-l),t.ctx.lineTo(a+i*l,o-l/2.4),t.ctx.lineTo(a+i*s,o-l/2.4),t.ctx.lineTo(a+i*s,o+l/2.4),t.ctx.lineTo(a+i*l,o+l/2.4),t.ctx.lineTo(a+i*l,o+l),t.ctx.closePath()}else t.ctx.moveTo(r,c),t.ctx.lineTo(r-l,c+a*l),t.ctx.lineTo(r-l/2.4,c+a*l),t.ctx.lineTo(r-l/2.4,c+a*s),t.ctx.lineTo(r+l/2.4,c+a*s),t.ctx.lineTo(r+l/2.4,c+a*l),t.ctx.lineTo(r+l,c+a*l),t.ctx.closePath();!1!==t.style.fill&&(t.ctx.save(),t.ctx.globalAlpha=t.style.fillOpacity??.9,t.ctx.fillStyle=_i(t),t.ctx.fill(),t.ctx.restore()),wi(t),t.ctx.stroke(),t.ctx.setLineDash([])},distance:(t,e,n)=>{const r=n.pts[0],o=i?e-r.y:r.y-e;return Math.abs(t-r.x)<=8&&o>=-4&&o<=20?0:null}}}const ur=dr("arrow-up","Arrow Up",!0),pr=dr("arrow-down","Arrow Down",!1),fr=dr("arrow-left","Arrow Left",!0,"left"),xr=dr("arrow-right","Arrow Right",!0,"right"),yr={id:"highlighter",name:"Highlighter",points:0,freehand:!0,defaultStyle:{lineWidth:12,fillOpacity:.28},settings:et([V,j,yi,bi]),draw:t=>{t.pts.length<2||(t.ctx.setLineDash([]),$i(t,Math.max(2,(t.style.lineWidth||12)*t.rc.dpr),t.style.fillOpacity??.28))},distance:(t,e,i)=>{const n=L(t,e,i.pts);return n<=Math.max(6,(i.drawing.style.lineWidth??12)/2)?0:n}},gr=et([j,E,K,G]),mr={id:"fib-channel",name:"Fib Channel",points:3,alertValue:M("channel",h),alertLevels:_(h),defaultStyle:{showLabels:!0,levels:c(h)},settings:et([j,E,K,q,G]),draw:t=>{const e=Hi(t.style.levels,h),[i,n,r]=t.pts,o=t.rc.dpr,s=r.x-n.x,l=r.y-n.y,a=!0===t.style.extendLeft,c=!0===t.style.extendRight;wi(t);for(const r of e){const[e,h]=N({x:i.x+s*r.ratio,y:i.y+l*r.ratio},{x:n.x+s*r.ratio,y:n.y+l*r.ratio},t.rc.plotWidth*o,a,c),d=Wi(r);t.ctx.strokeStyle=d,t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(h.x,h.y),t.ctx.stroke(),!1!==t.style.showLabels&&Li(t,Bi(r),n.x+s*r.ratio+4*o,n.y+l*r.ratio,d)}t.ctx.setLineDash([])},distance:(t,e,i)=>{const n=Hi(i.drawing.style.levels,h),[r,o,s]=i.pts,l=!0===i.drawing.style.extendLeft,a=!0===i.drawing.style.extendRight;let c=1/0;for(const h of n){const[n,d]=N({x:r.x+(s.x-o.x)*h.ratio,y:r.y+(s.y-o.y)*h.ratio},{x:o.x+(s.x-o.x)*h.ratio,y:o.y+(s.y-o.y)*h.ratio},i.rc.plotWidth,l,a),u=k(t,e,n,d);u<c&&(c=u)}return c}},br={id:"fib-time-zone",name:"Fib Time Zone",points:2,defaultStyle:{showLabels:!0,levels:c(f)},settings:et([U,K,G]),draw:t=>{const[e,i]=t.pts,n=i.x-e.x;if(Math.abs(n)<.5)return;const r=t.rc.dpr,o=t.rc.plotWidth*r,s=Hi(t.style.levels,f);wi(t);for(const i of s){const s=e.x+n*i.ratio;if(s<-10||s>o+10)continue;const l=i.color??t.style.color;t.ctx.strokeStyle=l,t.ctx.beginPath(),t.ctx.moveTo(Math.round(s)+.5,0),t.ctx.lineTo(Math.round(s)+.5,t.rc.plotHeight*r),t.ctx.stroke(),!1!==t.style.showLabels&&Li(t,i.label??String(i.ratio),s+3*r,12*r,l)}t.ctx.setLineDash([])},distance:(t,e,i)=>{const[n,r]=i.pts,o=r.x-n.x;if(Math.abs(o)<.5)return null;let s=1/0;for(const e of Hi(i.drawing.style.levels,f))s=Math.min(s,Math.abs(t-(n.x+o*e.ratio)));return Number.isFinite(s)?s:null}},vr={id:"fib-fan",name:"Fib Fan",points:2,defaultStyle:{showLabels:!0,levels:c(d)},settings:gr,draw:t=>{const e=Hi(t.style.levels,d),[i,n]=t.pts,r=t.rc.dpr,o=t.rc.plotWidth*r;wi(t);for(const s of e){const e=N(i,{x:n.x,y:i.y+(n.y-i.y)*s.ratio},o,!1,!0)[1],l=Wi(s);t.ctx.strokeStyle=l,t.ctx.beginPath(),t.ctx.moveTo(i.x,i.y),t.ctx.lineTo(e.x,e.y),t.ctx.stroke(),!1!==t.style.showLabels&&Li(t,Bi(s),n.x+4*r,i.y+(n.y-i.y)*s.ratio,l)}t.ctx.setLineDash([])},distance:(t,e,i)=>{const n=Hi(i.drawing.style.levels,d),[r,o]=i.pts;let s=1/0;for(const l of n)s=Math.min(s,k(t,e,r,N(r,{x:o.x,y:r.y+(o.y-r.y)*l.ratio},i.rc.plotWidth,!1,!0)[1]));return Number.isFinite(s)?s:null}},wr={id:"gann-fan",name:"Gann Fan",points:2,defaultStyle:{showLabels:!0,levels:c(p)},settings:gr,draw:t=>{const[e,i]=t.pts,n=t.rc.dpr,r=t.rc.plotWidth*n,s=i.x-e.x,a=i.y-e.y;if(Math.abs(s)<.5)return;const c=Hi(t.style.levels,p);wi(t),c.forEach((c,h)=>{const d=N(e,{x:e.x+s,y:e.y+a*c.ratio},r,!1,!0)[1],u=c.color??o(h);t.ctx.strokeStyle=u,t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(d.x,d.y),t.ctx.stroke(),!1!==t.style.showLabels&&Li(t,c.label??l(c.ratio),i.x+4*n,e.y+a*c.ratio,u)}),t.ctx.setLineDash([])},distance:(t,e,i)=>{const[n,r]=i.pts,o=r.x-n.x,s=r.y-n.y;if(Math.abs(o)<.5)return null;let l=1/0;for(const r of Hi(i.drawing.style.levels,p))l=Math.min(l,k(t,e,n,N(n,{x:n.x+o,y:n.y+s*r.ratio},i.rc.plotWidth,!1,!0)[1]));return Number.isFinite(l)?l:null}},_r={id:"gann-box",name:"Gann Box",points:2,defaultStyle:{fill:!0,fillOpacity:.05,showLabels:!0,levels:c(u)},settings:et([U,J,K,G]),draw:t=>{const[e,i]=t.pts,n=F(e,i),r=t.rc.dpr,o=t.drawing.points,s=Hi(t.style.levels,u);Mi(t,()=>t.ctx.fillRect(n.x0,n.y0,n.x1-n.x0,n.y1-n.y0)),wi(t),t.ctx.beginPath(),t.ctx.moveTo(e.x,e.y),t.ctx.lineTo(i.x,i.y),t.ctx.stroke();for(const l of s){const s=Math.round(e.x+(i.x-e.x)*l.ratio)+.5,a=o[0].price+(o[1].price-o[0].price)*l.ratio,c=Math.round(t.rc.priceScale.priceToY(a)*r)+.5,h=Wi(l);t.ctx.strokeStyle=h,t.ctx.beginPath(),t.ctx.moveTo(s,n.y0),t.ctx.lineTo(s,n.y1),t.ctx.moveTo(n.x0,c),t.ctx.lineTo(n.x1,c),t.ctx.stroke(),!1!==t.style.showLabels&&Li(t,Bi(l),n.x1+4*r,c,h)}t.ctx.setLineDash([])},distance:(t,e,i)=>A(t,e,i.pts[0],i.pts[1],!0===i.drawing.style.fill)},Mr=[Vi,ji,Ei,Ui,Ji,qi,Yi,Ki,Xi,Zi,Qi,nn,rn,cn,hn,or,sn,nr,rr,sr,lr,ar,cr,hr,wn,Mn,ur,pr,yr,vn,mr,br,vr,wr,_r,kn,Sn,Pn,yn,bn,Fn,In,On,Un,qn,Kn,Xn,Qn,er,fr,xr,...ne,...Te,...je,ri,oi];let kr=!1;function Sr(){if(!kr){kr=!0;for(const t of Mr)li(t)}}const Pr=2,Tr=t=>Number.isFinite(t.zIndex)?t.zIndex:0;function Lr(t){return t.slice().sort((t,e)=>Tr(t)-Tr(e))}function Fr(t,e){return ci(t)&&!0===ai(t).freehand&&e>2?[0,e-1]:Array.from({length:e},(t,e)=>e)}class Ir{constructor(t="top"){Object.defineProperty(this,"_order",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_drawings",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_host",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_selected",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_hovered",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_snap",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_preview",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_below",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_above",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_touch",{enumerable:!0,configurable:!0,writable:!0,value:!1}),this._order=t}attached(t){this._host=t}detached(){this._host=null}zOrder(){return this._order}autoscaleInfo(){return null}setDrawings(t){this._drawings=Lr(t),this._host?.requestUpdate()}setSelected(t){t.length===this._selected.length&&t.every((t,e)=>t===this._selected[e])||(this._selected=t.slice(),this._host?.requestUpdate())}setHovered(t){t!==this._hovered&&(this._hovered=t,this._requestOverlay())}hovered(){return this._hovered}setSnapPoint(t){(null===t?null!==this._snap:null===this._snap||this._snap.time!==t.time||this._snap.price!==t.price)&&(this._snap=null===t?null:{time:t.time,price:t.price},this._requestOverlay())}snapPoint(){return this._snap}setPointerType(t){this._touch="touch"===t}setPreview(t){this._preview=t,this._host?.requestUpdate()}setBelow(t){this._below!==t&&(null!==this._below&&(this._below._above=null),this._below=t,null!==t&&(null!==t._above&&t._above!==this&&(t._above._below=null),t._above=this,t._host?.requestUpdate()),this._host?.requestUpdate())}_requestOverlay(){null===this._above&&this._host?.requestUpdate()}_isTouch(t){const e=t.pointerType;return"touch"===e||"mouse"!==e&&"pen"!==e&&this._touch}_grabRadius(t){return this._isTouch(t)?12:6}_handleRadius(t){return this._isTouch(t)?10:5}_project(t,e,i){return{x:t.timeScale.indexToX(t.dataLayer.timeToIndexFloat(e)),y:t.priceScale.priceToY(i)}}_points(t,e){return e.points.map(e=>this._project(t,e.time,e.price))}_handled(){if(0===this._selected.length)return[];const t=new Set(this._selected);return this._drawings.filter(e=>t.has(e.id)&&!0!==e.locked&&!1!==e.visible)}_hoverHandled(){const t=this._hovered;if(null===t||this._selected.includes(t))return null;const e=this._drawings.find(e=>e.id===t);return void 0===e||!0===e.locked||!1===e.visible?null:e}draw(t,e){t.save(),t.beginPath(),t.rect(0,0,Math.round(e.plotWidth*e.dpr),Math.round(e.plotHeight*e.dpr)),t.clip();try{!function(t,e){const i=gt.get(t);gt.set(t,new Map);try{return e()}finally{i?gt.set(t,i):gt.delete(t)}}(t,()=>this._drawContent(t,this._drawingContext(e)))}finally{t.restore()}}_drawingContext(t){return t.readoutPriceScale?{...t,priceScale:t.readoutPriceScale}:t}_drawContent(t,e){const i=new Set(this._selected),n=null===this._preview?this._drawings:[...this._drawings,this._preview];for(const r of n){if(!1===r.visible||!ci(r.tool))continue;const n=ai(r.tool),o=this._points(e,r);if(0===o.length||!o.every(t=>Number.isFinite(t.x)&&Number.isFinite(t.y)))continue;if(r.points.length<Math.max(1,n.points)){r===this._preview&&this._drawPlacementGuide(t,e,o,r);continue}const s={color:r.style.color??e.theme.lineColor,lineWidth:r.style.lineWidth??1.5,...r.style},l=e.dpr;t.save(),r===this._preview&&(t.globalAlpha=.7),n.draw({ctx:t,rc:e,drawing:r,selected:i.has(r.id),pts:o.map(t=>({x:t.x*l,y:t.y*l})),style:{...s,color:s.color,lineWidth:s.lineWidth},formatPrice:t=>e.priceScale.format(t)}),t.restore()}if(null!==this._above)return;const r=[this._hoverHandled(),this._below?._hoverHandled()??null];for(const i of r)null!==i&&this._drawHandles(t,e,this._points(e,i),i.tool,!0);for(const i of this._handled())this._drawHandles(t,e,this._points(e,i),i.tool,!1);if(null!==this._below)for(const i of this._below._handled())this._drawHandles(t,e,this._points(e,i),i.tool,!1);null!==this._snap&&this._drawSnapRing(t,e,this._project(e,this._snap.time,this._snap.price))}_drawPlacementGuide(t,e,i,n){const r=e.dpr;t.save(),t.globalAlpha*=.7,t.strokeStyle=n.style.color??e.theme.lineColor,t.lineWidth=Math.max(1,(n.style.lineWidth??1.5)*r),t.setLineDash([3*r,3*r]),t.beginPath(),t.moveTo(i[0].x*r,i[0].y*r);for(let e=1;e<i.length;e++)t.lineTo(i[e].x*r,i[e].y*r);t.stroke(),t.setLineDash([]);for(const n of i)t.beginPath(),t.arc(n.x*r,n.y*r,3*r,0,2*Math.PI),t.fillStyle=e.theme.background,t.fill(),t.stroke();t.restore()}_drawHandles(t,e,i,n,r){const o=e.dpr;t.save(),t.setLineDash([]),t.lineWidth=Math.max(1,Math.round((r?1:1.5)*o)),r&&(t.globalAlpha=.6);for(const r of Fr(n,i.length)){const n=i[r];t.beginPath(),t.arc(n.x*o,n.y*o,5*o,0,2*Math.PI),t.fillStyle=e.theme.background,t.fill(),t.strokeStyle=e.theme.lineColor,t.stroke()}t.restore()}_drawSnapRing(t,e,i){if(!Number.isFinite(i.x)||!Number.isFinite(i.y))return;const n=e.dpr;t.save(),t.setLineDash([]),t.globalAlpha=.9,t.lineWidth=Math.max(1,Math.round(1.5*n)),t.strokeStyle=e.theme.axisText,t.beginPath(),t.arc(i.x*n,i.y*n,5.5*n,0,2*Math.PI),t.stroke(),t.restore()}hitTest(t,e,i){if(i=this._drawingContext(i),null!==this._above)return null;const n=this._hitHandle(t,e,i)??this._below?._hitHandle(t,e,i)??null;return null!==n?n:this._hitBody(t,e,i)??this._below?._hitBody(t,e,i)??null}_hitHandle(t,e,i){const n=this._handleRadius(i)+2;for(const r of this._handled()){const o=this._points(i,r);for(const i of Fr(r.tool,o.length))if(Math.hypot(t-o[i].x,e-o[i].y)<=n)return{externalId:`draw:${r.id}#${i}`,zOrder:"top",distance:0,cursor:"grabbing",draggable:!0}}return null}_hitBody(t,e,i){const n=this._grabRadius(i);let r=null;for(let o=this._drawings.length-1;o>=0;o--){const s=this._drawings[o];if(!1===s.visible||!0===s.locked||!ci(s.tool))continue;const l=ai(s.tool);if(s.points.length<Math.max(1,l.points))continue;const a=l.distance(t,e,{pts:this._points(i,s),drawing:s,rc:i});if(null!==a&&Number.isFinite(a)&&!(a>n)&&((null===r||a<r.distance)&&(r={id:s.id,distance:a}),0===a))break}return null===r?null:{externalId:`draw:${r.id}`,zOrder:"top",distance:r.distance,cursor:"move",draggable:!0}}}const Ar=t=>"object"==typeof t&&null!==t&&!Array.isArray(t),Cr=t=>"number"==typeof t&&Number.isFinite(t),Nr=(t,e)=>"string"==typeof t&&e.includes(t),zr=t=>"__proto__"===t||"constructor"===t||"prototype"===t,Or=["solid","dashed","dotted"],Dr=["left","center","right"],$r=["top","middle","bottom"],Hr=["inside","outside"],Wr=["color","fillColor"],Br=["lineWidth","fillOpacity","accountSize","risk"],Rr=["fill","extendLeft","extendRight","showLabels","showStats","pressure"],Vr=["color","fontFamily","backgroundColor","borderColor"],jr=["fontSize","wrapWidth","backgroundOpacity"],Er=["bold","italic","wrap","background","border"];let Ur=1;function Jr(t){const e=Array.isArray(t)?t:Ar(t)&&Array.isArray(t.drawings)?t.drawings:null,i=[];if(null===e)return{version:2,drawings:i};const n=new Set;for(const t of e){const e=Yr(t);if(null!==e){for(;""===e.id||n.has(e.id);)e.id="m"+Ur++;n.add(e.id),i.push(e)}}const r=qr(Ar(t)?t.groups:void 0,i);return{version:2,drawings:i,...r.length?{groups:r}:{}}}function qr(t,e){if(!Array.isArray(t))return[];const i=new Set(e.map(t=>t.id)),n=new Set,r=new Set,o=[];for(const e of t){if(!Ar(e)||"string"!=typeof e.id||!e.id.trim()||n.has(e.id)||"string"!=typeof e.name||!e.name.trim()||!Array.isArray(e.members))continue;const t=[];for(const n of e.members)"string"==typeof n&&i.has(n)&&!r.has(n)&&(t.push(n),r.add(n));t.length&&(n.add(e.id),o.push({id:e.id,name:e.name.trim(),members:t}))}return o}function Yr(t){if(!Ar(t))return null;const e=t.tool;if("string"!=typeof e||""===e)return null;const i=function(t){if(!Array.isArray(t)||0===t.length)return null;const e=[];for(const i of t){if(!Ar(i)||!Cr(i.time)||!Cr(i.price))return null;const t={time:i.time,price:i.price};Cr(i.pressure)&&i.pressure>=0&&i.pressure<=1&&(t.pressure=i.pressure),e.push(t)}return e}(t.points);if(null===i)return null;const n=void 0===t.paneIndex?0:t.paneIndex;if(!Cr(n)||n<0||!Number.isInteger(n))return null;const r=Ar(t.style)?t.style:{},o={id:"string"==typeof t.id?t.id:Cr(t.id)?String(t.id):"",tool:e,points:i,style:Kr(r,e),paneIndex:n,zIndex:Cr(t.zIndex)?t.zIndex:0},s=(l=t.text,(Ar(l)&&"string"==typeof l.value?Gr(l.value,l):null)??function(t){return"string"!=typeof t.text?null:Gr(t.text,{color:t.fontColor,fontSize:t.fontSize,fontFamily:t.fontFamily,bold:"bold"===t.fontWeight||void 0,italic:"italic"===t.fontStyle||void 0,align:t.textAlign,valign:t.textVAlign,position:t.textPosition,wrap:t.wrap,wrapWidth:t.wrapWidth,background:t.background,backgroundColor:t.backgroundColor,backgroundOpacity:t.backgroundOpacity,border:t.border,borderColor:t.borderColor})}(r));var l;return null!==s&&(o.text=s),Ar(t.props)&&(o.props=Xr(t.props,0)),"boolean"==typeof t.locked&&(o.locked=t.locked),"boolean"==typeof t.visible&&(o.visible=t.visible),Cr(t.createdAt)&&(o.createdAt=t.createdAt),o}function Kr(t,e){const i={};for(const e of Wr){const n=t[e];"string"==typeof n&&(i[e]=n)}for(const e of Br){const n=t[e];Cr(n)&&(i[e]=n)}for(const e of Rr){const n=t[e];"boolean"==typeof n&&(i[e]=n)}Nr(t.lineStyle,Or)&&(i.lineStyle=t.lineStyle);const r=function(t,e){if(!Array.isArray(t))return null;const i=[];return t.forEach((t,r)=>{if(Cr(t))i.push(function(t,e,i){return"fib-time-zone"===t?{ratio:e}:"gann-fan"===t?{ratio:e,color:o(i)}:{ratio:e,color:n(e)}}(e,t,r));else if(Ar(t)&&Cr(t.ratio)){const e={ratio:t.ratio};"string"==typeof t.color&&(e.color=t.color),"boolean"==typeof t.enabled&&(e.enabled=t.enabled),"string"==typeof t.label&&(e.label=t.label),i.push(e)}}),0===i.length&&t.length>0?null:i}(t.levels,e);return null!==r&&(i.levels=r),i}function Gr(t,e){const i={value:t};for(const t of Vr){const n=e[t];"string"==typeof n&&(i[t]=n)}for(const t of jr){const n=e[t];Cr(n)&&(i[t]=n)}for(const t of Er){const n=e[t];"boolean"==typeof n&&(i[t]=n)}return Nr(e.align,Dr)&&(i.align=e.align),Nr(e.valign,$r)&&(i.valign=e.valign),Nr(e.position,Hr)&&(i.position=e.position),i}function Xr(t,e){const i={};for(const[n,r]of Object.entries(t)){if(zr(n))continue;const t=Zr(r,e);void 0!==t&&(i[n]=t)}return i}function Zr(t,e){return null===t||"boolean"==typeof t||"string"==typeof t?t:"number"==typeof t?Number.isFinite(t)?t:void 0:e>=8?void 0:Array.isArray(t)?t.map(t=>Zr(t,e+1)??null):Ar(t)?Xr(t,e+1):void 0}const Qr="openalgo-charts/drawings",to=2,eo=4096;let io=null;function no(){io=null}function ro(){const t=globalThis.navigator,e=t?.clipboard;return void 0===e||"function"!=typeof e.writeText||"function"!=typeof e.readText?null:e}function oo(t){const e={...t,points:t.points.map(so),style:lo(t.style)};return void 0!==t.text&&(e.text={...t.text}),void 0!==t.props&&(e.props=JSON.parse(JSON.stringify(t.props))),e}function so(t){return void 0===t.pressure?{time:t.time,price:t.price}:{time:t.time,price:t.price,pressure:t.pressure}}function lo(t){const e={...t};return void 0!==t.levels&&(e.levels=t.levels.map(t=>({...t}))),e}function ao(t){return JSON.stringify({[Qr]:{version:2,drawings:t.map(t=>({tool:t.tool,points:t.points.map(so),style:t.style??{},...void 0===t.text?{}:{text:t.text},...void 0===t.props?{}:{props:t.props},paneIndex:t.paneIndex,zIndex:Number.isFinite(t.zIndex)?t.zIndex:0,...void 0===t.locked?{}:{locked:t.locked},...void 0===t.visible?{}:{visible:t.visible}}))}})}const co=t=>"object"==typeof t&&null!==t&&!Array.isArray(t),ho=t=>"number"==typeof t&&Number.isFinite(t),uo=t=>"string"==typeof t&&t.length<=eo,po=t=>"__proto__"===t||"constructor"===t||"prototype"===t,fo=(t,e)=>"string"==typeof t&&e.includes(t),xo=["text","fontSize","fontFamily","fontWeight","fontStyle","background","backgroundColor","backgroundOpacity","border","borderColor","wrap","wrapWidth","textAlign","textVAlign","textPosition","fontColor"];function yo(t){if(!Array.isArray(t)||t.length>64)return null;const e=[];for(const i of t)if(ho(i))e.push({ratio:i});else{if(!co(i)||!ho(i.ratio))return null;{const t={ratio:i.ratio};uo(i.color)&&(t.color=i.color),"boolean"==typeof i.enabled&&(t.enabled=i.enabled),uo(i.label)&&(t.label=i.label),e.push(t)}}return e}function go(t){if(!co(t)||!uo(t.value))return null;const e={value:t.value};for(const i of["color","fontFamily","backgroundColor","borderColor"]){const n=t[i];uo(n)&&(e[i]=n)}for(const i of["fontSize","wrapWidth","backgroundOpacity"]){const n=t[i];ho(n)&&(e[i]=n)}for(const i of["bold","italic","wrap","background","border"]){const n=t[i];"boolean"==typeof n&&(e[i]=n)}return fo(t.align,["left","center","right"])&&(e.align=t.align),fo(t.valign,["top","middle","bottom"])&&(e.valign=t.valign),fo(t.position,["inside","outside"])&&(e.position=t.position),e}function mo(t,e=0){if(!co(t))return null;const i={};let n=0;for(const[r,o]of Object.entries(t)){if(po(r))continue;if(++n>64)return null;const t=bo(o,e);void 0!==t&&(i[r]=t)}return i}function bo(t,e){if("boolean"==typeof t||ho(t))return t;if("string"==typeof t)return t.length<=eo?t:void 0;if(Array.isArray(t)){if(t.length>64||e+1>=3)return;const i=[];for(const n of t){const t=bo(n,e+1);if(void 0===t)return;i.push(t)}return i}if(co(t)){if(e+1>=3)return;return mo(t,e+1)??void 0}}function vo(t){if(!co(t))return null;if("string"!=typeof t.tool||""===t.tool||t.tool.length>eo)return null;if(!ci(t.tool))return null;const e=function(t){if(!Array.isArray(t)||0===t.length||t.length>2e4)return null;const e=[];for(const i of t){if(!co(i)||!ho(i.time)||!ho(i.price))return null;const t={time:i.time,price:i.price};ho(i.pressure)&&i.pressure>=0&&i.pressure<=1&&(t.pressure=i.pressure),e.push(t)}return e}(t.points);if(null===e)return null;const i=function(t){if(void 0===t)return{};if(!co(t))return null;const e={};let i=0;for(const[n,r]of Object.entries(t))if(!po(n)){if(++i>64)return null;if(!xo.includes(n))switch(n){case"color":case"fillColor":uo(r)&&(e[n]=r);break;case"lineWidth":case"fillOpacity":case"accountSize":case"risk":ho(r)&&(e[n]=r);break;case"lineStyle":fo(r,["solid","dashed","dotted"])&&(e[n]=r);break;case"fill":case"extendLeft":case"extendRight":case"showLabels":case"showStats":case"pressure":"boolean"==typeof r&&(e[n]=r);break;case"levels":{const t=yo(r);null!==t&&(e[n]=t);break}default:if("string"==typeof r){if(r.length>eo)return null;e[n]=r}else if("boolean"==typeof r||ho(r))e[n]=r;else if(Array.isArray(r)){if(r.length>64)return null;r.every(ho)&&(e[n]=r.slice())}}}return e}(t.style);if(null===i)return null;const n=void 0===t.paneIndex?0:t.paneIndex;if(!ho(n)||n<0||!Number.isInteger(n))return null;if(void 0!==t.locked&&"boolean"!=typeof t.locked)return null;if(void 0!==t.visible&&"boolean"!=typeof t.visible)return null;const r=go(t.text)??(co(t.style)?function(t){return"string"!=typeof t.text?null:go({value:t.text,color:t.fontColor,fontSize:t.fontSize,fontFamily:t.fontFamily,bold:"bold"===t.fontWeight||void 0,italic:"italic"===t.fontStyle||void 0,align:t.textAlign,valign:t.textVAlign,wrap:t.wrap,wrapWidth:t.wrapWidth,background:t.background,backgroundColor:t.backgroundColor,backgroundOpacity:t.backgroundOpacity,border:t.border,borderColor:t.borderColor,position:t.textPosition})}(t.style):null),o=void 0===t.props?null:mo(t.props),s={tool:t.tool,points:e,style:i,paneIndex:n,zIndex:ho(t.zIndex)?t.zIndex:0};return null!==r&&(s.text=r),null!==o&&(s.props=o),void 0!==t.locked&&(s.locked=t.locked),void 0!==t.visible&&(s.visible=t.visible),ho(t.createdAt)&&(s.createdAt=t.createdAt),s}function wo(t){if("string"!=typeof t||0===t.length)return null;let e;try{e=JSON.parse(t)}catch{return null}if(!co(e))return null;const i=e[Qr];if(!co(i))return null;if(!ho(i.version)||i.version<1||i.version>2)return null;const n=i.drawings;if(!Array.isArray(n)||0===n.length||n.length>512)return null;for(const t of n)if(null===vo(t))return null;const r=Jr(n).drawings;if(r.length!==n.length)return null;const o=[];for(const t of r){const e=vo(t);if(null===e)return null;o.push(e)}return o}class _o{constructor(t={}){Object.defineProperty(this,"_port",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_fallback",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_lastError",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._port=void 0===t.port?ro():t.port,this._fallback=t.fallbackToMemory??!0}setPort(t){this._port=t}setFallbackToMemory(t){this._fallback=t}lastError(){return this._lastError}async write(t){if(0===t.length)return!1;let e;try{e=ao(t)}catch(t){return this._lastError=String(t),!1}this._lastError=null;let i=!1;if(null!==this._port)try{await this._port.writeText(e),i=!0}catch(t){this._lastError=String(t)}else this._lastError="openalgo-charts: no system clipboard available";return this._fallback?(io=e,!0):i}async read(){if(this._lastError=null,null!==this._port)try{const t=wo(await this._port.readText());if(null!==t)return t}catch(t){this._lastError=String(t)}return this._fallback?wo(io):null}}const Mo=Math.PI/4;let ko=1;const So=(t,e)=>t.length===e.length&&t.every((t,i)=>t===e[i]);function Po(t){return!0===t?"strong":"weak"===t||"strong"===t?t:"off"}const To=t=>!0===t.modifiers?.shift||!0===t.shiftKey,Lo="openalgo-charts/drawing-link";function Fo(t,e,i){if(JSON.stringify(e)===JSON.stringify(i))return t;if(null===e||null===i||"object"!=typeof e||"object"!=typeof i||Array.isArray(e)||Array.isArray(i))return i;const n=e,r=i,o={...t};for(const t of new Set([...Object.keys(n),...Object.keys(r)]))JSON.stringify(n[t])!==JSON.stringify(r[t])&&(t in r?o[t]=Fo(o[t],n[t],r[t]):delete o[t]);return o}class Io{constructor(t,e={}){Object.defineProperty(this,"_chart",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_opts",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_clipboard",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_layers",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_drawings",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_groups",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_nextGroup",{enumerable:!0,configurable:!0,writable:!0,value:1}),Object.defineProperty(this,"_linkedPreviews",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_destroyed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_tool",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_pending",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_pendingPane",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"_selection",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_hovered",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_lifted",{enumerable:!0,configurable:!0,writable:!0,value:new Set}),Object.defineProperty(this,"_shift",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_pointerKind",{enumerable:!0,configurable:!0,writable:!0,value:"mouse"}),Object.defineProperty(this,"_undo",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_redo",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_pendingHistory",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_dragStart",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_off",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"_lastCursor",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"_lastBar",{enumerable:!0,configurable:!0,writable:!0,value:null}),this._chart=t,this._opts={magnet:Po(e.magnet),stayInDrawingMode:e.stayInDrawingMode??!1,historyLimit:e.historyLimit??50,pasteOffsetBars:e.pasteOffsetBars??2,pasteOffsetPixels:e.pasteOffsetPixels??16,defaultStyle:e.defaultStyle??{}},this._clipboard=new _o({...void 0===e.clipboard?{}:{port:e.clipboard},...void 0===e.clipboardFallbackToMemory?{}:{fallbackToMemory:e.clipboardFallbackToMemory}}),this._off.push(t.on("click",t=>this._onClick(t))),this._off.push(t.on("crosshair:move",t=>this._onCrosshair(t))),this._off.push(t.on("hover",t=>this._onHover(t))),this._off.push(t.on("drag",t=>this._onDrag(t))),this._off.push(t.on("drag:end",()=>this._onDragEnd())),this._off.push(t.on("drag:cancel",()=>{this.cancelDrag()})),this._off.push(t.on("data:context",()=>{this.cancelDrag()})),this._off.push(t.on("dblclick",()=>{this.finish()})),this._off.push(t.on("drawings:restore",t=>this.fromJSON(t)));const i=t.drawingState();if(null!=i){const t=Jr(i);this._drawings=t.drawings,this._groups=t.groups??[]}this._off.push(t.on("paneRemoved",t=>{const{paneIndex:e}=t;this._remapPanes(t=>t===e?null:t>e?t-1:t)})),this._off.push(t.on("paneMoved",t=>{const{from:e,to:i}=t;this._remapPanes(t=>t===e?i:t===i?e:t)})),this._sync()}setTool(t){if(null!==t&&!ci(t))throw new Error(`openalgo-charts: unknown drawing tool "${t}"`);this.cancelDrag(),this._tool=t,this._pending=[],this._setPlacementMode(null!==t),this._syncPreview(),this._syncSnapRing(),this._chart.emit("draw:tool",{tool:t})}_setPlacementMode(t){const e=this._chart;e.setPlacementMode?.(t)}activeTool(){return this._tool}setOptions(t){const{clipboard:e,magnet:i,...n}=t;this._opts={...this._opts,...n,defaultStyle:t.defaultStyle??this._opts.defaultStyle,magnet:void 0===i?this._opts.magnet:Po(i)},void 0!==e&&this._clipboard.setPort(e),this._syncSnapRing()}magnetMode(){return this._opts.magnet}hovered(){return this._hovered}clipboard(){return this._clipboard}groups(){return this._groups.map(t=>({...t,members:[...t.members]}))}createGroup(t,e){const i=[...new Set(e)].filter(t=>void 0!==this.get(t));if(this._destroyed||!t.trim()||!i.length)return null;let n;do{n="group-"+this._nextGroup++}while(this._groups.some(t=>t.id===n));this._pushUndo();const r={id:n,name:t.trim(),members:i},o=new Set(i);for(const t of this._groups)t.members=t.members.filter(t=>!o.has(t));return this._groups.push(r),this._sync(),this._emitChange(i,"update"),{...r,members:[...i]}}renameGroup(t,e){const i=this._groups.find(e=>e.id===t);return!(!i||!e.trim()||this._destroyed)&&(this._pushUndo(),i.name=e.trim(),this._sync(),this._emitChange(i.members,"update"),!0)}removeGroup(t,e=!1){const i=this._groups.find(e=>e.id===t);return!(!i||this._destroyed)&&(this._pushUndo(),this._groups=this._groups.filter(t=>t!==i),e?this._removeIds(i.members,!1):(this._sync(),this._emitChange(i.members,"update")),!0)}reorder(t,e){const i=this.get(t);if(!i||this._destroyed||-1!==e&&1!==e)return!1;const n=this._drawings.filter(t=>t.paneIndex===i.paneIndex&&t.zIndex<0==i.zIndex<0).sort((t,e)=>t.zIndex-e.zIndex),r=n.indexOf(i),o=r+e;if(o<0||o>=n.length)return!1;this._pushUndo(),[n[r],n[o]]=[n[o],n[r]];const s=new Set(n);let l=0;this._drawings=this._drawings.map(t=>s.has(t)?n[l++]:t),n.forEach((t,e)=>{t.zIndex=i.zIndex<0?e-n.length:e}),this._sync();for(const t of n)this._chart.emit("draw:update",{drawing:t});return this._emitChange(n.map(t=>t.id),"reorder"),!0}_remapPanes(t){const e=this._dragStart;if(e){this._dragStart=null;for(const t of e.items){const e=this.get(t.id);e&&(e.points=t.points.map(t=>({...t})))}this._undo=e.undo,this._redo=e.redo,this._pendingHistory=null,this._lifted.clear(),this._chart.emit("draw:preview-clear",{ids:e.items.map(t=>t.id)})}this._pending=[],this._lastCursor=null,this._linkedPreviews.clear();const i=e=>{const i=Jr(JSON.parse(e));return i.drawings=i.drawings.filter(e=>{const i=t(e.paneIndex);return null!==i&&(e.paneIndex=i,!0)}),i.groups=qr(i.groups,i.drawings),JSON.stringify(i)};for(const t of new Set([...this._undo,...this._redo]))t.before=i(t.before),t.after=i(t.after);const n=[];this._drawings=this._drawings.filter(e=>{const i=t(e.paneIndex);return null===i?(n.push(e),!1):(e.paneIndex=i,!0)});const r=[...this._layers.entries()];this._layers.clear();for(const[e,i]of r){const n=t(e);null!==n&&this._layers.set(n,i)}this._pruneSelection(),this._sync();for(const t of n)this._chart.emit("draw:remove",{drawing:t});this._emitChange(this._drawings.map(t=>t.id),"update")}drawings(){return this._drawings}get(t){return this._drawings.find(e=>e.id===t)}get isDestroyed(){return this._destroyed}applyLinkedDrawing(t,e){if(this._destroyed||!0===this._chart.isDestroyed)return;this._dragStart?.items.some(e=>e.id===t)&&this.cancelDrag();const i=this._drawings.findIndex(e=>e.id===t);if(this._linkedPreviews.delete(t),null===e){if(i<0)return;this._drawings.splice(i,1),this._pruneSelection()}else{const n=oo({...e,id:t});i<0?this._drawings.push(n):this._drawings[i]=n}this._sync(),this._chart.emit("drawing:change",{ids:[t],kind:null===e?"remove":i<0?"add":"update",linked:!0})}setLinkedPreview(t,e){this._destroyed||!0===this._chart.isDestroyed||(null===e?this._linkedPreviews.delete(t):void 0!==this.get(t)&&this._linkedPreviews.set(t,oo({...e,id:t})),this._sync())}reorderLinkedDrawings(t){if(this._destroyed||!0===this._chart.isDestroyed)return;const e=[...new Set(t)].map(t=>this.get(t)).filter(t=>void 0!==t),i=new Set(e.map(t=>t.id));let n=0;const r=this._drawings.map(t=>i.has(t.id)?e[n++]:t);r.every((t,e)=>t===this._drawings[e])||(this._drawings=r,this._sync(),this._chart.emit("drawing:change",{ids:e.map(t=>t.id),kind:"reorder",linked:!0}))}alertInfo(t){const e=this.get(t);if(!e||!ci(e.tool))return{available:!1,reason:"Drawing is unavailable",levels:[]};const i=ai(e.tool);if(!i.alertValue)return{available:!1,reason:"This tool has no numeric alert value",paneIndex:e.paneIndex,levels:[]};const n=i.alertLevels?.(e)??[{id:"line",title:"Line"}];return this._chart.timeToCoordinate&&this._chart.priceToCoordinate&&this._chart.coordinateToPrice?{available:n.length>0,reason:n.length?void 0:"No active drawing levels",paneIndex:e.paneIndex,levels:n.map(t=>({...t}))}:{available:!1,reason:"Drawing coordinate maps are unavailable",levels:[],paneIndex:e.paneIndex}}valueAt(t,e,i){const n=this.get(t);if(!n||!ci(n.tool)||!Number.isFinite(e))return;const r=ai(n.tool),o=this._chart.timeToCoordinate,s=this._chart.coordinateToPrice;if(!r.alertValue||!o||!s)return;const l=[];for(const t of n.points){const e=this._toPixel(t,n.paneIndex);if(!e)return;l.push(e)}const a=o.call(this._chart,e);if(!Number.isFinite(a))return;const c=r.alertValue({drawing:n,pts:l,time:e,x:a,fromY:t=>s.call(this._chart,t,n.paneIndex)},i);return c&&Number.isFinite(c.price)&&(void 0===c.upperPrice||Number.isFinite(c.upperPrice))?{...c,paneIndex:n.paneIndex}:void 0}add(t){this._pushUndo();const e=this._insert(t);return this._sync(),this._chart.emit("draw:add",{drawing:e}),this._emitChange([e.id],"add"),e}_insert(t){const e=ai(t.tool);let i=t.id??this._mintId();for(;void 0===t.id&&void 0!==this.get(i);)i=this._mintId();const{id:n,...r}=t,o={...r,id:i,style:{...this._opts.defaultStyle,...e.defaultStyle,...t.style},zIndex:Number.isFinite(t.zIndex)?t.zIndex:0,createdAt:t.createdAt??Date.now()};return void 0!==o.props?.[Lo]&&(o.props={...o.props},delete o.props[Lo]),void 0===t.text&&void 0===e.defaultText||(o.text={value:"",...e.defaultText,...t.text}),this._drawings.push(o),o}_mintId(){return"d"+ko++}update(t,e){const i=this.get(t);return void 0!==i&&(this._pushUndo(),this._applyPatch(i,e),this._sync(),this._chart.emit("draw:update",{drawing:i}),this._emitChange([t],"update"),!0)}updateMany(t){const e=t.map(t=>({d:this.get(t.id),patch:t.patch})).filter(t=>void 0!==t.d);if(0!==e.length){this._pushUndo();for(const{d:t,patch:i}of e)this._applyPatch(t,i);this._sync();for(const{d:t}of e)this._chart.emit("draw:update",{drawing:t});this._emitChange(e.map(t=>t.d.id),"update")}}_applyPatch(t,e){if(void 0!==e.points){const i=e.points.map(t=>({...t})),n=ci(t.tool)?ai(t.tool):void 0;t.points=void 0===n?.constrain?i:n.constrain(i,function(t,e){if(t.length!==e.length)return null;let i=null;for(let n=0;n<e.length;n++)if(t[n].time!==e[n].time||t[n].price!==e[n].price){if(null!==i)return null;i=n}return i}(t.points,i))}void 0!==e.style&&(t.style={...t.style,...e.style}),void 0!==e.text&&(t.text={...t.text,...e.text}),void 0!==e.props&&(t.props={...t.props,...e.props}),void 0!==e.locked&&(t.locked=e.locked),void 0!==e.visible&&(t.visible=e.visible),void 0!==e.zIndex&&Number.isFinite(e.zIndex)&&(t.zIndex=e.zIndex)}remove(t){return this._removeIds([t],!0).length>0}removeMany(t){this._removeIds(t,!0)}_removeIds(t,e){const i=new Set(t),n=this._drawings.filter(t=>i.has(t.id));if(0===n.length)return[];e&&this._pushUndo(),this._drawings=this._drawings.filter(t=>!i.has(t.id)),this._selection=this._selection.filter(t=>!i.has(t)),this._sync();for(const t of n)this._chart.emit("draw:remove",{drawing:t});return this._emitChange(n.map(t=>t.id),"remove"),n}clear(){0!==this._drawings.length&&(this._removeIds(this._drawings.map(t=>t.id),!0),this._setSelection([]))}select(t,e=!1){const i=null===t?[]:"string"==typeof t?[t]:t,n=[];for(const t of i)void 0===this.get(t)||n.includes(t)||n.push(t);if(!e)return void this._setSelection(n);const r=this._selection.slice();for(const t of n){const e=r.indexOf(t);e>=0?r.splice(e,1):r.push(t)}this._setSelection(r)}selected(){return 0===this._selection.length?null:this._selection[0]}selection(){return this._selection}_setSelection(t){const e=!So(t,this._selection);this._selection=t;for(const e of this._layers.values())e.bottom.setSelected(t),e.top.setSelected(t);e&&(this._chart.emit("draw:select",{id:this.selected()}),this._chart.emit("drawing:select",{ids:t.slice()}))}_emitChange(t,e){null!==this._pendingHistory&&(this._pendingHistory.after=JSON.stringify(this.toJSON()),this._pendingHistory=null),this._chart.emit("drawing:change",{ids:t.slice(),kind:e})}setZIndex(t,e){const i=this.get(t);void 0!==i&&Number.isFinite(e)&&i.zIndex!==e&&(this._pushUndo(),i.zIndex=e,this._sync(),this._chart.emit("draw:update",{drawing:i}),this._emitChange([t],"reorder"))}bringToFront(t){const e=this.get(t);if(void 0===e)return;const i=this._band(e),n=0===i.length?e.zIndex:Math.max(...i.map(t=>t.zIndex));this._reorder(e,n,"end")}sendToBack(t){const e=this.get(t);if(void 0===e)return;const i=this._band(e),n=0===i.length?e.zIndex:Math.min(...i.map(t=>t.zIndex));this._reorder(e,n,"start")}sendBehindSeries(t){const e=this.get(t);void 0!==e&&e.zIndex>=0&&this.setZIndex(t,-1)}bringAboveSeries(t){const e=this.get(t);void 0!==e&&e.zIndex<0&&this.setZIndex(t,0)}_band(t){const e=t.zIndex<0;return this._drawings.filter(i=>i!==t&&i.paneIndex===t.paneIndex&&i.zIndex<0===e)}_reorder(t,e,i){const n=this._drawings.indexOf(t);n===("end"===i?this._drawings.length-1:0)&&t.zIndex===e||(this._pushUndo(),t.zIndex=e,this._drawings.splice(n,1),"end"===i?this._drawings.push(t):this._drawings.unshift(t),this._sync(),this._chart.emit("draw:update",{drawing:t}),this._emitChange([t.id],"reorder"))}nudge(t,e,i){if(0===e&&0===i)return;const n=this._targets(t).filter(t=>!0!==t.locked);if(0!==n.length){this._pushUndo();for(const t of n)t.points=t.points.map(n=>({time:this._offsetTime(n.time,e),price:this._offsetPrice(n.price,t.paneIndex,i)}));this._sync();for(const t of n)this._chart.emit("draw:update",{drawing:t});this._emitChange(n.map(t=>t.id),"update")}}duplicate(t){const e=this._targets(t);if(0===e.length)return[];this._pushUndo();const i=e.map(t=>{const{id:e,createdAt:i,...n}=oo(t);return this._insert({...n,points:this._offsetPoints(t.points,t.paneIndex)})});this._sync();for(const t of i)this._chart.emit("draw:add",{drawing:t});return this._emitChange(i.map(t=>t.id),"add"),this.select(i.map(t=>t.id)),i}async copy(t){const e=this._targets(t);if(0===e.length)return!1;const i=await this._clipboard.write(e);return i&&this._chart.emit("draw:copy",{drawings:e.map(oo)}),i}async cut(t){const e=this._targets(t);if(0===e.length)return!1;if(!await this._clipboard.write(e))return!1;const i=this._removeIds(e.map(t=>t.id),!0);return 0!==i.length&&(this._chart.emit("draw:cut",{drawings:i}),!0)}async paste(){const t=await this._clipboard.read();if(null===t||0===t.length)return[];for(const e of t)if(!ci(e.tool))return[];const e=t.map(t=>{const e=this._clampPane(t.paneIndex);return{...t,paneIndex:e,points:this._offsetPoints(t.points,e)}});this._pushUndo();const i=e.map(t=>this._insert(t));this._sync();for(const t of i)this._chart.emit("draw:add",{drawing:t});return this._emitChange(i.map(t=>t.id),"add"),this._chart.emit("draw:paste",{drawings:i}),this.select(i.map(t=>t.id)),i}_targets(t){const e=null==t?this._selection:"string"==typeof t?[t]:t,i=[];for(const t of e){const e=this.get(t);void 0===e||i.includes(e)||i.push(e)}return i}_clampPane(t){const e=this._chart.panes;if(void 0===e)return t;const i=e.call(this._chart).length;return 0===i?0:Math.min(t,i-1)}_offsetPoints(t,e){const i=this._barSeconds()*this._opts.pasteOffsetBars,n=this._opts.pasteOffsetPixels;return t.map(t=>({time:t.time+i,price:this._offsetPrice(t.price,e,n)}))}_offsetPrice(t,e,i){if(0===i)return t;const n=this._chart.priceToCoordinate,r=this._chart.coordinateToPrice;if(void 0===n||void 0===r)return t;const o=n.call(this._chart,t,e);if(null===o||!Number.isFinite(o))return t;const s=r.call(this._chart,o+i,e);return null!==s&&Number.isFinite(s)?s:t}_offsetTime(t,e){if(0===e)return t;const i=this._chart.timeToCoordinate,n=this._chart.coordinateToTime;if(void 0!==i&&void 0!==n){const r=i.call(this._chart,t);if(Number.isFinite(r)){const t=n.call(this._chart,r+e);if(Number.isFinite(t))return t}}return t+e/8*this._barSeconds()}undo(){this._onDragEnd();const t=this._undo.pop();return void 0!==t&&(this._redo.push(t),this._applyHistory(t.after,t.before,"undo"),!0)}redo(){this._onDragEnd();const t=this._redo.pop();return void 0!==t&&(this._undo.push(t),this._applyHistory(t.before,t.after,"redo"),!0)}_applyHistory(t,e,i){const n=Jr(JSON.parse(t)),r=Jr(JSON.parse(e)),o=n.drawings,s=r.drawings,l=new Map(o.map(t=>[t.id,t])),a=new Map(s.map(t=>[t.id,t])),c=o.filter(t=>a.has(t.id)).map(t=>t.id),h=s.filter(t=>l.has(t.id)).map(t=>t.id),d=[...new Set([...l.keys(),...a.keys()])].filter(t=>JSON.stringify(l.get(t))!==JSON.stringify(a.get(t))||c.indexOf(t)!==h.indexOf(t)),u=new Set(d),p=new Map(this._drawings.map(t=>[t.id,t])),f=new Map((n.groups??[]).map(t=>[t.id,t])),x=new Map((r.groups??[]).map(t=>[t.id,t])),y=new Set([...f.keys(),...x.keys()].filter(t=>JSON.stringify(f.get(t))!==JSON.stringify(x.get(t))));this._groups=this._groups.filter(t=>!y.has(t.id));for(const t of y){const e=x.get(t);e&&this._groups.push(e)}this._drawings=this._drawings.filter(t=>!u.has(t.id)||a.has(t.id)).map(t=>u.has(t.id)?Fo(t,l.get(t.id),a.get(t.id)):t);for(let t=0;t<s.length;t++){const e=s[t];if(!u.has(e.id)||p.has(e.id)||l.has(e.id))continue;const i=s.slice(t+1).find(t=>void 0!==this.get(t.id)),n=void 0===i?this._drawings.length:this._drawings.findIndex(t=>t.id===i.id);this._drawings.splice(n,0,e)}const g=h.filter(t=>c.indexOf(t)!==h.indexOf(t)).map(t=>this.get(t)).filter(t=>void 0!==t),m=new Set(g.map(t=>t.id));let b=0;this._drawings=this._drawings.map(t=>m.has(t.id)?g[b++]:t),this._pruneSelection(),this._sync();for(const t of d){const e=this.get(t),i=p.get(t);void 0!==e?this._chart.emit(void 0===i?"draw:add":"draw:update",{drawing:e,history:!0}):void 0!==i&&this._chart.emit("draw:remove",{drawing:i,history:!0})}this._emitChange(d,i)}_pruneSelection(){const t=this._selection.filter(t=>void 0!==this.get(t));So(t,this._selection)||this._setSelection(t)}canUndo(){return this._undo.length>0}canRedo(){return this._redo.length>0}toJSON(){return{version:2,drawings:this._drawings.map(oo),...this._groups.length?{groups:this._groups.map(t=>({...t,members:[...t.members]}))}:{}}}fromJSON(t){this.cancelDrag(),this._linkedPreviews.clear();const e=Jr(t);this._drawings=e.drawings,this._groups=e.groups??[],this._undo=[],this._redo=[],this._pendingHistory=null,this._setSelection([]),this._sync(),this._chart.emit("draw:restore",{}),this._emitChange(this._drawings.map(t=>t.id),"update")}destroy(){if(!this._destroyed){this.cancelDrag(),this._destroyed=!0,this._linkedPreviews.clear(),this._chart.emit("draw:destroy",{controller:this}),this._setPlacementMode(!1);for(const t of this._off)t();this._off.length=0;for(const t of this._layers.values())t.top.setBelow(null),this._chart.removePrimitive(t.top),this._chart.removePrimitive(t.bottom);this._layers.clear()}}_onCrosshair(t){const e=t.time??null,i=e??(t.point?this._chart.coordinateToTime?.(t.point.x)??null:null),n=t.price??null,r=t.paneIndex??null;this._lastCursor=null!==i&&Number.isFinite(i)&&null!==n&&null!==r?{time:i,price:n,paneIndex:r}:null;const o=t.bar??null;if(this._lastBar=null===o||null===e?null:{time:e,...o},this._shift=To(t),this._notePointer(t),null===i&&null===n&&this._setHovered(null),null!==this._tool&&!0===t.pressed&&this._isFreehand()&&null!==i&&null!==n&&null!==r&&Number.isFinite(i)&&Number.isFinite(n))for(const e of this._coalesced(t,{time:i,price:n},r))this._inkPoint(e,r);else this._syncSnapRing(),null!==this._tool&&this._pending.length>0&&this._syncPreview()}_onHover(t){const e=t.id??null;this._setHovered(null!==e&&e.startsWith("draw:")?e.slice(5).split("#")[0]:null)}_setHovered(t){if(t!==this._hovered){this._hovered=t;for(const e of this._layers.values())e.bottom.setHovered(t),e.top.setHovered(t);this._chart.emit("drawing:hover",{id:t})}}_notePointer(t){const e=(t=>"touch"===t.pointerType||"pen"===t.pointerType?t.pointerType:"mouse")(t);if(e!==this._pointerKind){this._pointerKind=e;for(const t of this._layers.values())t.bottom.setPointerType(e),t.top.setPointerType(e)}}_coalesced(t,e,i){const n=t.samples,r={...e,...this._pressureOf(t.pressure)},o=this._chart.coordinateToTime,s=this._chart.coordinateToPrice;if(!Array.isArray(n)||n.length<2||void 0===o||void 0===s||null===t.point||void 0===t.point)return[r];const l=n[n.length-1],a=t.point.y-l.y;if(!Number.isFinite(a))return[r];const c=[];for(let t=0;t<n.length-1;t++){const e=n[t],r=o.call(this._chart,e.x),l=s.call(this._chart,e.y+a,i);null!==l&&Number.isFinite(r)&&Number.isFinite(l)&&c.push({time:r,price:l,...this._pressureOf(e.pressure)})}return c.push({...r,...this._pressureOf(l.pressure??t.pressure)}),c}_pressureOf(t){return"number"==typeof t&&Number.isFinite(t)&&.5!==t?{pressure:Math.min(1,Math.max(0,t))}:{}}_expand(t,e){if(void 0===t.expand)return e;const i=this._chart.getVisibleLogicalRange(),n=null===i?60:Math.max(1,i.to-i.from),r=this._pendingPane,o=void 0!==this._chart.timeToCoordinate&&void 0!==this._chart.priceToCoordinate&&void 0!==this._chart.coordinateToTime&&void 0!==this._chart.coordinateToPrice,s=t.expand(e,{barSeconds:this._barSeconds(),visibleBars:n,...o?{toPixel:t=>this._toPixel(t,r),fromPixel:t=>this._fromPixel(t,r)}:{}});return void 0===t.constrain?s:t.constrain(s,null)}_barSeconds(){const t=this._chart.dataLayer,e=t.baseIndex,i=e>0?t.indexToTime(e-1):void 0,n=e>=0?t.indexToTime(e):void 0;return void 0!==i&&void 0!==n&&n>i?n-i:60}_isFreehand(){return null!==this._tool&&!0===ai(this._tool).freehand}_inkPoint(t,e){if(0===this._pending.length)this._pendingPane=e;else{if(e!==this._pendingPane)return;{const e=this._pending[this._pending.length-1];if(e.time===t.time&&e.price===t.price)return}}this._pending.push(t),this._syncPreview()}_commit(t){const e=ai(this._tool),i=this.add({tool:e.id,points:t,style:{},paneIndex:this._pendingPane});this._pending=[],this._opts.stayInDrawingMode||(this._tool=null,this._setPlacementMode(!1)),this._syncPreview(),this._syncSnapRing(),this.select(i.id),this._chart.emit("draw:tool",{tool:this._tool})}_finishFreehand(){const t=this._pending;this._pending=[],t.length<2?this._syncPreview():this._commit(this._thinStroke(t,this._pendingPane))}_thinStroke(t,e){const i=[];for(const n of t){const r=this._toPixel(n,e);if(null===r)return t;i.push(r)}const n=ft(i,1.5),r=[];let o=0;for(const e of n){for(;o<i.length&&(i[o].x!==e.x||i[o].y!==e.y);)o++;if(o>=i.length)return t;r.push(t[o]),o++}return r}finish(){if(null===this._tool||0===this._pending.length)return!1;const t=ai(this._tool);if(0!==t.points||!0===t.freehand)return!1;const e=this._pending;return e.length<2?(this._pending=[],this._syncPreview(),!1):(this._commit(e),!0)}cancel(){if(this.cancelDrag())return!0;if(null===this._tool)return!1;const t=this._pending.length>0;return this._pending=[],t&&this._opts.stayInDrawingMode?(this._syncPreview(),!0):(this._tool=null,this._setPlacementMode(!1),this._syncPreview(),this._syncSnapRing(),this._chart.emit("draw:tool",{tool:null}),!0)}popAnchor(){if(null===this._tool||0===this._pending.length)return!1;const t=ai(this._tool);return 0===t.points&&!0!==t.freehand&&(this._pending.pop(),this._syncPreview(),!0)}_onClick(t){if(this._notePointer(t),null!==this._tool){if(this._isFreehand())return void(!0===t.viaDrag&&this._finishFreehand());if(!0===t.viaDrag&&0===this._pending.length)return;if(null===t.price||!Number.isFinite(t.price)||!Number.isFinite(t.time))return;return this._shift=To(t),void this._placePoint(this._aimPoint({time:t.time,price:t.price},t.paneIndex),t.paneIndex)}const e=!0===t.shiftKey||!0===t.ctrlKey||!0===t.metaKey;null!==t.id&&t.id.startsWith("draw:")?this.select(t.id.slice(5).split("#")[0],e):null!==t.id||e||this.select(null)}_placePoint(t,e){0===this._pending.length&&(this._pendingPane=e),this._pending.push(t);const i=ai(this._tool);i.points>0&&this._pending.length>=i.points?this._commit(this._expand(i,this._pending)):this._syncPreview()}_aimPoint(t,e){const i=this._lockedPoint(t,e);return null!==i?i:this._snapPoint(t,e)??t}_lockedPoint(t,e){return this._shift&&null!==this._tool&&1===this._pending.length?!0!==ai(this._tool).angleLock||e!==this._pendingPane?null:this._lockAngle(this._pending[0],t,e):null}_snapPoint(t,e){const i=this._opts.magnet;if("off"===i||0!==e)return null;const n=this._lastBar;if(null===n)return null;const r=[n.open,n.high,n.low,n.close];if("strong"===i){let e=r[0],i=1/0;for(const n of r){const r=Math.abs(n-t.price);r<i&&(i=r,e=n)}return{time:n.time,price:e}}const o=this._chart.priceToCoordinate;if(void 0===o)return null;const s=o.call(this._chart,t.price,e);if(null===s||!Number.isFinite(s))return null;let l=null,a=8;for(const t of r){const i=o.call(this._chart,t,e);if(null===i||!Number.isFinite(i))continue;const n=Math.abs(i-s);n<=a&&(a=n,l=t)}return null===l?null:{time:n.time,price:l}}_onDrag(t){if(!t.id.startsWith("draw:"))return;const[e,i]=t.id.slice(5).split("#"),n=this.get(e);if(void 0===n||!0===n.locked)return;const r=void 0===i?null:Number(i);if(this._notePointer(t),this._shift=To(t),null===this._dragStart||this._dragStart.id!==e||this._dragStart.handle!==r){null!==r||this._selection.includes(e)||this.select(e);const i=null===r?this._targets(this._selection).filter(t=>!0!==t.locked):[n],o=this._undo.slice(),s=this._redo.slice();this._pushUndo(),this._dragStart={id:e,handle:r,undo:o,redo:s,from:{time:t.fromTime??t.time,price:t.fromPrice??t.price},items:i.map(t=>({id:t.id,paneIndex:t.paneIndex,points:t.points.map(t=>({...t}))}))};let l=!1;for(const t of i)t.zIndex<0&&(this._lifted.add(t.id),l=!0);return this._moveDrag(t,n,r),l?this._sync():this._syncDrag(),void this._emitDragPreview()}this._moveDrag(t,n,r),this._syncDrag(),this._emitDragPreview()}_emitDragPreview(){const t=this._dragStart?.items.map(t=>this.get(t.id)).filter(t=>void 0!==t)??[];this._chart.emit("draw:preview",{drawings:t.map(oo)})}cancelDrag(){const t=this._dragStart;if(null===t)return!1;this._dragStart=null;for(const e of t.items){const t=this.get(e.id);void 0!==t&&(t.points=e.points.map(t=>({...t})))}return this._undo=t.undo,this._redo=t.redo,this._pendingHistory=null,this._lifted.clear(),!0!==this._chart.isDestroyed&&this._sync(),this._chart.emit("draw:preview-clear",{ids:t.items.map(t=>t.id)}),!0}_moveDrag(t,e,i){const n=this._dragStart;if(null===i){const e=t.time-n.from.time,i=t.price-n.from.price,r=this._pixelDelta(n.from.price,t.price,t.paneIndex);for(const o of n.items){const n=this.get(o.id);if(void 0===n)continue;const s=o.paneIndex===t.paneIndex;n.points=o.points.map(t=>({...t,time:t.time+e,price:s?t.price+i:this._offsetPrice(t.price,o.paneIndex,r)}))}}else if(i>=0&&i<e.points.length){const r=n.items[0];let o={time:t.time,price:t.price};this._shift&&2===r.points.length&&ci(e.tool)&&!0===ai(e.tool).angleLock&&(o=this._lockAngle(r.points[1-i],o,e.paneIndex)??o);const s=r.points.map((t,e)=>e===i?{...t,...o}:{...t}),l=ci(e.tool)?ai(e.tool):void 0;e.points=void 0===l?.constrain?s:l.constrain(s,i)}}_pixelDelta(t,e,i){const n=this._chart.priceToCoordinate;if(void 0===n)return 0;const r=n.call(this._chart,t,i),o=n.call(this._chart,e,i);return null!==r&&null!==o&&Number.isFinite(r)&&Number.isFinite(o)?o-r:0}_toPixel(t,e){const i=this._chart.timeToCoordinate,n=this._chart.priceToCoordinate;if(void 0===i||void 0===n)return null;const r=i.call(this._chart,t.time),o=n.call(this._chart,t.price,e);return null!==o&&Number.isFinite(r)&&Number.isFinite(o)?{x:r,y:o}:null}_fromPixel(t,e){const i=this._chart.coordinateToTime,n=this._chart.coordinateToPrice;if(void 0===i||void 0===n)return null;const r=i.call(this._chart,t.x),o=n.call(this._chart,t.y,e);return null!==o&&Number.isFinite(r)&&Number.isFinite(o)?{time:r,price:o}:null}_lockAngle(t,e,i){const n=this._toPixel(t,i),r=this._toPixel(e,i);if(null===n||null===r)return null;const o=r.x-n.x,s=r.y-n.y;if(0===o&&0===s)return null;const l=Math.round(Math.atan2(s,o)/Mo)*Mo,a=Math.cos(l),c=Math.sin(l),h=o*a+s*c;return this._fromPixel({x:n.x+h*a,y:n.y+h*c},i)}_onDragEnd(){if(null===this._dragStart)return;const t=this._dragStart.items.map(t=>this.get(t.id)).filter(t=>void 0!==t);this._dragStart=null,this._chart.emit("draw:preview-clear",{ids:t.map(t=>t.id)}),this._lifted.size>0&&(this._lifted.clear(),this._sync());for(const e of t)this._chart.emit("draw:update",{drawing:e});t.length>0&&this._emitChange(t.map(t=>t.id),"update")}_layerFor(t){let e=this._layers.get(t);return void 0===e&&(e={bottom:new Ir("bottom"),top:new Ir("top")},this._chart.addPrimitive(e.bottom,t),this._chart.addPrimitive(e.top,t),e.top.setBelow(e.bottom),e.bottom.setSelected(this._selection),e.top.setSelected(this._selection),this._layers.set(t,e)),e}_onTop(t){return t.zIndex>=0||this._lifted.has(t.id)}_sync(){this._groups=qr(this._groups,this._drawings);const t=new Map;for(const e of this._drawings){const i=this._linkedPreviews.get(e.id)??e;let n=t.get(i.paneIndex);void 0===n&&(n={below:[],above:[]},t.set(i.paneIndex,n)),(this._onTop(i)?n.above:n.below).push(i)}for(const[e,i]of t){const t=this._layerFor(e);t.bottom.setDrawings(i.below),t.top.setDrawings(i.above)}for(const[e,i]of this._layers)t.has(e)||(i.bottom.setDrawings([]),i.top.setDrawings([])),i.bottom.setSelected(this._selection),i.top.setSelected(this._selection);null!==this._hovered&&void 0===this.get(this._hovered)&&this._setHovered(null),this._chart.setDrawingState(this.toJSON())}_syncDrag(){const t=this._dragStart;if(null===t)return;const e=new Set(t.items.map(t=>t.paneIndex));for(const t of e){const e=this._layers.get(t);void 0!==e&&e.top.setDrawings(this._drawings.filter(e=>e.paneIndex===t&&this._onTop(e)))}this._chart.setDrawingState(this.toJSON())}_syncPreview(){for(const t of this._layers.values())t.top.setPreview(null);if(null===this._tool||0===this._pending.length)return;const t=this._lastCursor,e=null===t||t.paneIndex!==this._pendingPane?this._pending:[...this._pending,this._aimPoint({time:t.time,price:t.price},t.paneIndex)];this._layerFor(this._pendingPane).top.setPreview({id:"__preview",tool:this._tool,points:e,style:this._opts.defaultStyle,paneIndex:this._pendingPane,zIndex:0})}_syncSnapRing(){const t=this._lastCursor;let e=null,i=t?.paneIndex??this._pendingPane;null===t||null===this._tool||this._isFreehand()||null!==this._lockedPoint({time:t.time,price:t.price},t.paneIndex)||(e=this._snapPoint({time:t.time,price:t.price},t.paneIndex),i=t.paneIndex);for(const[t,n]of this._layers)n.top.setSnapPoint(t===i?e:null);null===e||this._layers.has(i)||this._layerFor(i).top.setSnapPoint(e)}_pushUndo(){this._onDragEnd();const t=JSON.stringify(this.toJSON());this._pendingHistory={before:t,after:t},this._undo.push(this._pendingHistory),this._undo.length>this._opts.historyLimit&&this._undo.shift(),this._redo=[]}}function Ao(t){return"string"==typeof t?.symbol&&t.symbol.trim()&&"string"==typeof t.exchange&&t.exchange.trim()?JSON.stringify([t.symbol,t.exchange]):null}function Co(t){try{return("function"==typeof t?t():t)??void 0}catch{return}}let No=1;const zo=(()=>{const t=globalThis.crypto;if("function"==typeof t?.randomUUID)return t.randomUUID();if("function"!=typeof t?.getRandomValues)throw new Error("Drawing links require Web Crypto");return Array.from(t.getRandomValues(new Uint8Array(16)),t=>t.toString(16).padStart(2,"0")).join("")})();let Oo=1;function Do(t,e){const i=t.props?.[Lo];return 1===i?.version&&i.context===e&&"string"==typeof i.id&&i.id.length>0?i.id:void 0}class $o{constructor(t={}){Object.defineProperty(this,"_members",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"_enabled",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_broadcasting",{enumerable:!0,configurable:!0,writable:!0,value:!1}),Object.defineProperty(this,"_destroyed",{enumerable:!0,configurable:!0,writable:!0,value:!1}),this._enabled=t.enabled??!1}options(){return{enabled:this._enabled}}add(t,e,i){if(this._destroyed||!0===t.isDestroyed||e.isDestroyed)return;const n=this._members.get(t);if(n?.controller===e)return void(void 0!==i&&(n.resolveContext="function"==typeof i?i:null,this.setContext(t,Co(i))));void 0!==n&&this.remove(t);const r={chart:t,controller:e,context:Ao(Co(i??(()=>t.getDataContext?.()))),resolveContext:"function"==typeof i?i:null,drawings:new Map,off:[]};this._members.set(t,r);for(const e of["draw:add","draw:update","draw:remove"])r.off.push(t.on(e,t=>{const i=t;void 0!==i?.drawing&&this._change(r,i.drawing,"draw:remove"===e,"draw:add"===e&&!0!==i.history)}));r.off.push(t.on("draw:preview",t=>{const e=t?.drawings;void 0!==e&&this._preview(r,e)}),t.on("draw:preview-clear",()=>this._clearMemberPreviews(r)),t.on("drawing:change",t=>{!0!==t?.linked&&this._syncOrder(r)}),t.on("data:context",e=>this.setContext(t,null===r.resolveContext?e:Co(r.resolveContext))),t.on("symbol",()=>this.setContext(t,Co(r.resolveContext??void 0))),t.on("draw:restore",()=>{this._detachDrawings(r,!1),this._reconnect(r)}),t.on("draw:destroy",i=>{i?.controller===e&&this.remove(t)}),t.on("destroy",()=>this.remove(t))),this._reconnect(r)}setOptions(t){if(void 0!==t.enabled&&!this._destroyed){if(!t.enabled)for(const t of this._members.values())this._clearMemberPreviews(t);if(this._enabled=t.enabled,this._enabled)for(const t of this._members.values())this._reconnect(t)}}setContext(t,e){const i=this._members.get(t);if(void 0===i)return;const n=Ao(e);for(const t of i.drawings.values())this._clearPreview(t);n!==i.context&&(i.controller.cancelDrag(),this._detachDrawings(i,!0),i.context=n,this._reconnect(i))}share(t,e){const i=this._members.get(t);if(void 0===i||!this._eligible(i)||this._broadcasting)return 0;let n=0;for(const t of new Set(e??i.controller.drawings().map(t=>t.id))){const e=i.controller.get(t);if(void 0===e||0!==e.paneIndex)continue;const r=i.drawings.get(t)??this._create(i,e);this._commit(r,i,e,!0),n++}return this._syncOrder(i),n}has(t){return this._members.has(t)}remove(t){const e=this._members.get(t);if(void 0!==e){this._detachDrawings(e,!1);for(const t of e.off)t();this._members.delete(t)}}destroy(){this._destroyed=!0;for(const t of[...this._members.keys()])this.remove(t)}_eligible(t){return!this._destroyed&&this._enabled&&null!==t.context&&this._members.get(t.chart)===t&&!0!==t.chart.isDestroyed&&!t.controller.isDestroyed}_create(t,e){const i=t.context,n=Do(e,i)??`${zo}:${Oo++}`;for(const r of this._members.values())for(const o of r.drawings.values())if(o.context===i&&o.lineage===n)return o.bindings.set(t,{id:e.id,local:!0}),t.drawings.set(e.id,o),o;const r={id:e.id,context:i,lineage:n,drawing:oo(e),bindings:new Map([[t,{id:e.id,local:!0}]]),previewFrom:null};return t.drawings.set(e.id,r),r}_reconnect(t){if(this._broadcasting||!this._eligible(t))return;const e=t.context,i=new Set;this._broadcasting=!0;try{for(const n of[...t.controller.drawings()]){if(0!==n.paneIndex||t.drawings.has(n.id))continue;const r=Do(n,e);if(void 0!==r)for(const o of this._members.values()){if(o===t||!this._eligible(o)||o.context!==e)continue;let s=[...o.drawings.values()].find(t=>t.context===e&&t.lineage===r);if(void 0===s){const t=o.controller.drawings().find(t=>0===t.paneIndex&&Do(t,e)===r);void 0!==t&&(s=this._create(o,t))}if(void 0!==s){s.bindings.set(t,{id:n.id,local:!1}),t.drawings.set(n.id,s),t.controller.applyLinkedDrawing(n.id,s.drawing),i.add(o);break}}}for(const e of i)this._orderFrom(e,t)}finally{this._broadcasting=!1}}_change(t,e,i,n){if(this._broadcasting||!this._eligible(t)||0!==e.paneIndex)return;let r=t.drawings.get(e.id);const o=void 0===r&&n;void 0===r&&n&&(r=this._create(t,e)),void 0!==r&&this._commit(r,t,i?null:e,o)}_commit(t,e,i,n){this._broadcasting=!0;try{this._clearPreview(t);const r=null===i?null:{...i,props:{...i.props,[Lo]:{version:1,id:t.lineage,context:t.context}}};null!==r&&Do(i,t.context)!==t.lineage&&e.controller.applyLinkedDrawing(i.id,r),t.drawing=null===r?null:oo({...r,id:t.id});for(const r of[...this._members.values()]){if(r===e||!this._eligible(r)||r.context!==t.context)continue;let o=t.bindings.get(r);if(void 0===o){if(null===i||!n)continue;const e=r.controller.drawings().find(e=>0===e.paneIndex&&!r.drawings.has(e.id)&&Do(e,t.context)===t.lineage);let s=e?.id??t.id;if(void 0===e)for(;void 0!==r.controller.get(s)||r.drawings.has(s);)s="linked"+No++;o={id:s,local:!1},t.bindings.set(r,o),r.drawings.set(s,t)}r.controller.applyLinkedDrawing(o.id,t.drawing)}}finally{this._broadcasting=!1}}_preview(t,e){if(!this._broadcasting&&this._eligible(t))for(const i of e){const e=t.drawings.get(i.id);if(void 0!==e){e.previewFrom=t;for(const[n,r]of e.bindings)n!==t&&this._eligible(n)&&n.context===e.context&&n.controller.setLinkedPreview(r.id,i)}}}_syncOrder(t){if(!this._broadcasting&&this._eligible(t)){this._broadcasting=!0;try{for(const e of this._members.values())e!==t&&this._eligible(e)&&e.context===t.context&&this._orderFrom(t,e)}finally{this._broadcasting=!1}}}_orderFrom(t,e){const i=t.controller.drawings().map(i=>t.drawings.get(i.id)?.bindings.get(e)?.id).filter(t=>void 0!==t);e.controller.reorderLinkedDrawings(i)}_clearPreview(t){if(null!==t.previewFrom){for(const[e,i]of t.bindings)e.controller.setLinkedPreview(i.id,null);t.previewFrom=null}}_clearMemberPreviews(t){for(const e of t.drawings.values())e.previewFrom===t&&this._clearPreview(e)}_detachDrawings(t,e){for(const i of t.drawings.values()){const n=i.bindings.get(t);this._clearPreview(i),i.bindings.delete(t),e&&!1===n?.local&&t.controller.applyLinkedDrawing(n.id,null)}t.drawings.clear()}}function Ho(t={}){return new $o(t)}const Wo="0 0 24 24",Bo=2,Ro={viewBox:Wo,fill:"none",stroke:"currentColor",strokeWidth:2,strokeLinecap:"round",strokeLinejoin:"round"},Vo={cursor:"M12 3v6M12 15v6M3 12h6M15 12h6",magnet:"M6 4v8a6 6 0 0 0 12 0V4M6 9h4M14 9h4","trend-line":"M4 20 20 4",ray:"M4 20 20 4M2 18 6 22","extended-line":"M2 22 22 2",arrow:"M4 20 18 6M18 6h-6M18 6v6","horizontal-line":"M2 12h20","horizontal-ray":"M6 12H22M4 10V14","vertical-line":"M12 2v20","cross-line":"M2 12h20M12 2v20","trend-angle":"M4 20 18 8M4 20h12M8 20a8 8 0 0 0 2-5","info-line":"M4 20 20 4M8 8h8",path:"M2 18 8 8l4 6 4-10",polyline:"M2 18 8 8l4 6 4-10 4 4","parallel-channel":"M2 16 14 4M8 22 20 10","disjoint-channel":"M2 16 14 6M8 22 22 14","flat-bottom":"M2 14 14 4M2 20h20","fib-channel":"M2 14 14 4M4 18 16 8M6 22 18 12","regression-trend":"M2 18 20 6M4 20 22 8M2 14 20 2","flat-top-bottom":"M3 6h18M3 20 21 12M3 6v14","regression-channel":"M3 18 19 6M5 21 21 9M3 12 17 2M10 16h4",pitchfork:"M4 21 16 9M4 9 16 21M10 15 22 3M4 9 10 3M16 21l6-6","schiff-pitchfork":"M4 19 18 5M4 11 12 19M8 15 20 3M4 11l6-6M12 19l8-8","modified-schiff-pitchfork":"M4 20 20 4M4 12 12 20M8 16 20 4M4 12l8-8M12 20l8-8","inside-pitchfork":"M4 20 20 4M6 10 14 18M6 10l6-6M14 18l6-6","fib-extension-two-point":"M3 4h18M3 10h18M3 16h18M3 21h18M6 4v12","fib-speed-resistance-fan":"M3 21V3M3 21h18M3 21 21 3M3 21 12 3M3 21 21 12","icon-stamp":"M12 3l3 6 6 3-6 3-3 6-3-6-6-3 6-3z",rectangle:"M3 5h18v14H3z","rotated-rectangle":"M2 14 10 4l12 6-8 10z",ellipse:"M12 5c5 0 9 3 9 7s-4 7-9 7-9-3-9-7 4-7 9-7z",circle:"M12 3a9 9 0 1 0 0 18 9 9 0 0 0 0-18z",triangle:"M12 4 21 20H3z",arc:"M3 19a12 12 0 0 1 18 0",curve:"M3 18c4-12 14-12 18 0","double-curve":"M3 18c3-9 7-9 9 0 2 9 6 9 9 0",brush:"M3 19c4 0 4-8 8-8s4 8 9 4",highlighter:"M4 16 14 6l4 4-10 10H4z","fib-retracement":"M3 4h18M3 9h18M3 14h18M3 19h18","fib-extension":"M3 5h18M3 12h18M3 19h18M8 5v14","fib-fan":"M3 20 21 4M3 20 21 10M3 20 21 16M3 20h18","fib-time-zone":"M4 3v18M8 3v18M14 3v18M22 3v18","fib-circles":"M12 20A5 5 0 0 1 12 10M12 20A9 9 0 0 1 12 2M10 20h4","fib-spiral":"M12 13A3 3 0 1 1 15 10A7 7 0 1 1 8 3A9 9 0 1 1 21 12","fib-wedge":"M3 20 21 4M3 20 21 12M3 20a12 12 0 0 0 10-6","fib-speed-fan":"M3 20V4M3 20h18M3 20 21 4M3 20 13 4","gann-fan":"M3 20 21 2M3 20 21 9M3 20 21 15M3 20h18","gann-box":"M3 3h18v18H3zM3 3l18 18M3 21 21 3","trend-fib-time":"M3 20 8 8 12 15M12 3v18M16 3v18M22 3v18","fib-speed-resistance-arcs":"M3 21a6 6 0 0 0 6-6M3 21a12 12 0 0 0 12-12M3 21a18 18 0 0 0 18-18M3 21 21 3","gann-square":"M3 3h18v18H3zM9 3v18M15 3v18M3 9h18M3 15h18M3 21 21 3","dedekind-tessellation":"M3 21a9 9 0 0 1 18 0M3 21a4 4 0 0 1 8 0M13 21a4 4 0 0 1 8 0M12 3v18",sonic:"M3 12a9 9 0 0 1 18 0M6 12a6 6 0 0 1 12 0M9 12a3 3 0 0 1 6 0M3 12h18",supersonic:"M3 12 21 3M3 12l18 9M7 12a5 5 0 0 1 10 0M10 12a3 3 0 0 1 6 0","golden-sonic":"M3 12a9 9 0 0 1 18 0M7 12a5 5 0 0 1 10 0M10 12a2 2 0 0 1 4 0M3 16h18M12 16v5","golden-supersonic":"M3 12 21 3M3 12l18 9M6 12a6 6 0 0 1 12 0M10 12a2 2 0 0 1 4 0M18 11v2","xabcd-pattern":"M3 19 7 4 12 16 16 8 21 20M3 19 12 16 21 20M7 4 16 8","abcd-pattern":"M3 18 9 4 15 15 21 3M3 18 15 15M9 4 21 3","elliott-impulse":"M2 21 6 12 9 17 13 5 17 11 22 2","elliott-correction":"M3 4 9 18 15 8 21 21M3 4h4M17 21h4","head-shoulders":"M2 20 5 10 8 17 12 3 16 17 19 10 22 20M3 17h18",gartley:"M3 20 7 3 12 13 16 7 21 17M3 20 12 13 21 17",bat:"M3 21 7 3 12 11 16 6 21 19M7 3 16 6M3 21 21 19",butterfly:"M3 16 7 3 12 14 16 7 21 21M3 16 12 14 21 21",crab:"M3 12 7 3 12 10 16 5 21 22M3 12 12 10 21 22",shark:"M3 17 7 10 12 20 16 3 21 18M3 17 21 18M7 10 16 3",cypher:"M3 20 7 9 12 16 16 3 21 18M3 20 12 16M7 9 16 3",measure:"M4 16h16M4 12v8M20 12v8M8 4h8M12 4v6","anchored-vwap":"M4 4v16M4 15 8 12 12 14 16 8 20 6M2 4h4","fixed-range-volume-profile":"M4 3v18M4 5h8v3H4M4 10h16v3H4M4 15h12v3H4","price-range":"M12 4v16M8 8l4-4 4 4M8 16l4 4 4-4","date-range":"M4 12h16M8 8l-4 4 4 4M16 8l4 4-4 4","date-price-range":"M4 6h16v12H4zM4 12h16M12 6v12","cyclic-lines":"M4 4v16M10 4v16M16 4v16M22 4v16","time-cycles":"M4 12a4 4 0 0 1 8 0 4 4 0 0 0 8 0M4 4v16","sine-line":"M2 12c3-9 6-9 9 0 3 9 6 9 9 0","long-position":"M3 15h18v5H3zM3 5h18v5H3zM12 10v5","short-position":"M3 5h18v5H3zM3 15h18v5H3zM12 10v5",forecast:"M3 18 9 10l4 4 8-10M13 4h8v8","risk-reward-long":"M3 14h18v6H3zM3 4h18v6H3zM12 10v4M6 7h4","risk-reward-short":"M3 4h18v6H3zM3 14h18v6H3zM12 10v4M6 17h4",text:"M4 5h16M12 5v14M8 19h8",note:"M4 20 9 15M9 5h13v10H9zM4 20v-4",callout:"M3 4h18v11H3zM8 15l-2 5 6-5",balloon:"M3 4h18v12H9l-4 4v-4H3z",comment:"M3 5h18v10H3zM6 15v4l4-4",signpost:"M12 21V9M5 3h14v6H5z","price-label":"M3 12 8 7h13v10H8z","price-note":"M2 12h5M7 6h15v12H7zM10 12h9",table:"M3 5h18v14H3zM3 10h18M9 10v9M15 10v9","flag-mark":"M6 21V3M6 3h12l-3 4 3 4H6","arrow-up":"M12 3v18M6 9l6-6 6 6","arrow-down":"M12 3v18M6 15l6 6 6-6","arrow-left":"M3 12h18M9 6l-6 6 6 6","arrow-right":"M3 12h18M15 6l6 6-6 6"};function jo(t){return Vo[t]}function Eo(){return Object.keys(Vo)}const Uo="0 0 16 16",Jo=1.5,qo={viewBox:Uo,fill:"none",stroke:"currentColor",strokeWidth:1.5,strokeLinecap:"round",strokeLinejoin:"round"},Yo={cursor:"M8 2v4M8 10v4M2 8h4M10 8h4",magnet:"M3 2v6a5 5 0 0 0 10 0V2M3 5h3M10 5h3",lock:"M3 7h10v7H3zM5 7V5a3 3 0 0 1 6 0v2",unlock:"M3 7h10v7H3zM5 7V5a3 3 0 0 1 6 0",eye:"M1 8c2-4 4-6 7-6s5 2 7 6c-2 4-4 6-7 6s-5-2-7-6zM10 8a2 2 0 1 1-4 0 2 2 0 0 1 4 0","eye-off":"M1 8c2-4 4-6 7-6s5 2 7 6c-2 4-4 6-7 6s-5-2-7-6zM2 2l12 12",star:"M8 1l2 5h5l-4 3 1 5-4-3-4 3 1-5-4-3h5z","star-filled":"M8 1 12 14 1 6h14L4 14z",trash:"M2 4h12M6 4V2h4v2M3 4l1 10h8l1-10",settings:"M2 4h12M2 8h12M2 12h12M5 2v4M11 6v4M7 10v4",undo:"M3 6h7a4 4 0 0 1 0 8H6M6 3 3 6l3 3",redo:"M13 6H6a4 4 0 0 0 0 8h4M10 3l3 3-3 3",copy:"M6 6h8v8H6zM10 6V2H2v8h4",paste:"M4 3H3v11h10V3h-1M6 2h4v2H6z",duplicate:"M6 6h8v8H6zM10 6V2H2v8h4M10 8v4M8 10h4",front:"M2 9h6v5H2zM11 14V2M8 5l3-3 3 3",back:"M2 2h6v5H2zM11 2v12M8 11l3 3 3-3",text:"M3 3h10M8 3v10M6 13h4","chevron-down":"M3 6l5 5 5-5","chevron-right":"M6 3l5 5-5 5",close:"M3 3l10 10M13 3 3 13",plus:"M8 2v12M2 8h12",minus:"M2 8h12",search:"M7 2a5 5 0 1 0 0 10 5 5 0 0 0 0-10zM11 11l3 3",grid:"M2 2h12v12H2zM2 8h12M8 2v12",link:"M6 10l4-4M7 4l1-1a3 3 0 0 1 4 4l-1 1M9 12l-1 1a3 3 0 0 1-4-4l1-1",unlink:"M7 4l1-1a3 3 0 0 1 4 4l-1 1M9 12l-1 1a3 3 0 0 1-4-4l1-1M4 4l8 8",camera:"M2 5h3l1-2h4l1 2h3v8H2zM8 11a2 2 0 1 0 0-4 2 2 0 0 0 0 4z",download:"M8 2v9M4 7l4 4 4-4M2 14h12"},Ko=new Set(["star-filled"]);function Go(t){return Yo[t]}function Xo(){return Object.keys(Yo)}const Zo="http://www.w3.org/2000/svg",Qo="oac-icon-";function ts(t){return String(t).replace(/[&<>"]/g,t=>"&"===t?"&":"<"===t?"<":">"===t?">":""")}function es(t,e,i){const n=t[e];if(void 0===n)throw new Error(`openalgo-charts: no ${i} icon "${e}"`);return n}function is(t){return Number(t.viewBox.split(" ")[2])}function ns(t,e,i,n){const r=ts(n.size??is(e)),o=ts(n.stroke??e.strokeWidth),s=void 0!==n.className&&""!==n.className?` class="${ts(n.className)}"`:"";return`<svg xmlns="${Zo}" viewBox="${ts(e.viewBox)}" width="${r}" height="${r}" fill="${ts(i)}" stroke="${ts(e.stroke)}" stroke-width="${o}" stroke-linecap="${ts(e.strokeLinecap)}" stroke-linejoin="${ts(e.strokeLinejoin)}"${s} aria-hidden="true">${t}</svg>`}function rs(t,e={}){return ns(`<path d="${ts(es(Vo,t,"tool"))}"/>`,Ro,Ro.fill,e)}function os(t,e={}){const i=es(Yo,t,"chrome"),n=Ko.has(t)?"currentColor":qo.fill;return ns(`<path d="${ts(i)}"/>`,qo,n,e)}function ss(t=Object.keys(Vo)){let e="";for(const i of new Set(t)){const t=es(Vo,i,"tool");e+=`<symbol id="${ts(Qo+i)}" viewBox="${ts(Ro.viewBox)}"><path d="${ts(t)}"/></symbol>`}return`<svg xmlns="${Zo}" style="display:none" aria-hidden="true">${e}</svg>`}function ls(t,e={}){return es(Vo,t,"tool"),ns(`<use href="#${ts(Qo+t)}"/>`,Ro,Ro.fill,e)}function as(t,e={}){const i=es(Vo,t,"tool"),n=e.size??20;if(!(Number.isFinite(n)&&n>0&&n<=128))throw new RangeError(`openalgo-charts: cursor size ${n} is outside 1..128`);const[r,o]=e.hotspot??[n/2,n/2],s=e.color??"#fff",l=e.halo??function(t){const e=/^#([0-9a-f]{3,8})$/i.exec(t.trim());if(!e)return"#000";const i=e[1],n=i.length<6?i.split("").map(t=>t+t).join(""):i;return(.2126*parseInt(n.slice(0,2),16)+.7152*parseInt(n.slice(2,4),16)+.0722*parseInt(n.slice(4,6),16))/255>.5?"#000":"#fff"}(s),a=e.fallback??"crosshair",c=is(Ro)/n,h=Math.round(100*(2+2*c))/100,d=`<svg xmlns="${Zo}" viewBox="${Ro.viewBox}" width="${n}" height="${n}" fill="none" stroke-linecap="${Ro.strokeLinecap}" stroke-linejoin="${Ro.strokeLinejoin}"><path d="${i}" stroke="${ts(l)}" stroke-width="${h}"/><path d="${i}" stroke="${ts(s)}" stroke-width="2"/></svg>`;return`url("data:image/svg+xml,${encodeURIComponent(d)}") ${Math.round(r)} ${Math.round(o)}, ${a}`}const cs=1,hs=10;function ds(t,e){if(e.editingText)return null;const i=t.key??"";if(""===i)return null;const n=!0===t.ctrlKey||!0===t.metaKey,r=!0===t.shiftKey;if(!0===t.altKey)return null;if(!0===e.placing&&!n&&!r){if("Escape"===i)return{type:"cancel"};if("Enter"===i)return{type:"finish"};if("Backspace"===i)return{type:"popAnchor"}}const o=e.hasSelection||e.hasTarget;if(n)switch(i.toLowerCase()){case"z":return r?{type:"redo"}:{type:"undo"};case"y":return r?null:{type:"redo"};case"c":return!r&&o?{type:"copy"}:null;case"x":return!r&&o?{type:"cut"}:null;case"v":return r?null:{type:"paste"};case"d":return!r&&o?{type:"duplicate"}:null;default:return null}if("Delete"===i||"Backspace"===i)return!r&&o?{type:"delete"}:null;if(!e.hasSelection)return null;const s=r?10:1;switch(i){case"ArrowLeft":return{type:"nudge",dx:-s,dy:0};case"ArrowRight":return{type:"nudge",dx:s,dy:0};case"ArrowUp":return{type:"nudge",dx:0,dy:-s};case"ArrowDown":return{type:"nudge",dx:0,dy:s};default:return null}}const us="draw";Sr();export{Te as ADVANCED_GEOMETRY_TOOLS,ne as ADVANCED_LINE_TOOLS,H as ALIGN_OPTIONS,ri as ANCHORED_VWAP,cr as ARC,Ui as ARROW,pr as ARROW_DOWN,fr as ARROW_LEFT,xr as ARROW_RIGHT,ur as ARROW_UP,qn as BALLOON,vn as BRUSH,Mr as BUILTIN_DRAWING_TOOLS,In as CALLOUT,Yo as CHROME_ICONS,qo as CHROME_ICON_ATTRS,Ko as CHROME_ICON_FILLED,Jo as CHROME_ICON_STROKE,Uo as CHROME_ICON_VIEWBOX,sr as CIRCLE,V as COLOR_FIELD,Kn as COMMENT,Ki as CROSS_LINE,hr as CURVE,r as CYCLE_PALETTE,kn as CYCLIC_LINES,rr as DATE_RANGE,h as DEFAULT_FIB,d as DEFAULT_FIB_FAN,f as DEFAULT_FIB_TIME_ZONE,u as DEFAULT_GANN_BOX,p as DEFAULT_GANN_FAN,Mn as DOUBLE_CURVE,Qr as DRAWING_CLIPBOARD_KEY,to as DRAWING_CLIPBOARD_VERSION,Pr as DRAWING_STATE_VERSION,Vo as DRAWING_TOOL_ICONS,us as DRAW_TIER,_o as DrawingClipboard,Io as DrawingController,Ir as DrawingLayer,$o as DrawingLinkGroup,Zi as ELLIPSE,Ei as EXTENDED_LINE,q as EXTEND_FIELDS,mr as FIB_CHANNEL,rn as FIB_EXTENSION,vr as FIB_FAN,nn as FIB_RETRACEMENT,br as FIB_TIME_ZONE,J as FILL_FIELDS,oi as FIXED_RANGE_VOLUME_PROFILE,On as FLAG_MARK,G as FONT_FIELDS,R as FONT_OPTIONS,or as FORECAST,_r as GANN_BOX,wr as GANN_FAN,yr as HIGHLIGHTER,Ji as HORIZONTAL_LINE,qi as HORIZONTAL_RAY,Ro as ICON_ATTRS,Bo as ICON_STROKE,Qo as ICON_SYMBOL_PREFIX,Wo as ICON_VIEWBOX,K as LEVEL_FIELDS,t as LEVEL_NEUTRAL,U as LINE_FIELDS,E as LINE_STYLE_FIELD,$ as LINE_STYLE_OPTIONS,j as LINE_WIDTH_FIELD,cn as LONG_POSITION,sn as MEASURE,Un as NOTE,cs as NUDGE_STEP_PX,hs as NUDGE_STEP_SHIFT_PX,Qi as PARALLEL_CHANNEL,bn as PATH,je as PATTERN_DRAWING_TOOLS,tt as PLATE_TEXT_FIELDS,ar as POLYLINE,Fn as PRICE_LABEL,Qn as PRICE_NOTE,nr as PRICE_RANGE,ji as RAY,Xi as RECTANGLE,wn as ROTATED_RECTANGLE,Q as SHAPE_TEXT_FIELDS,hn as SHORT_POSITION,Y as SHOW_LABELS_FIELD,Xn as SIGNPOST,Pn as SINE_LINE,er as TABLE,yn as TEXT,Z as TEXT_FIELDS,B as TEXT_POSITION_OPTIONS,X as TEXT_VALUE_FIELD,Sn as TIME_CYCLES,Vi as TREND_LINE,lr as TRIANGLE,W as VALIGN_OPTIONS,Yi as VERTICAL_LINE,Ye as anchoredVwapAnalysis,ut as applyDrawingSettings,I as boundsOf,xt as catmullRom,Go as chromeIcon,Xo as chromeIconIds,os as chromeIconSvg,no as clearMemoryClipboard,oo as cloneDrawing,c as cloneLevels,dt as coerceSettingValue,et as composeSettings,Ho as createDrawingLinkGroup,o as cycleColor,wo as decodeClipboardPayload,C as distToEllipse,S as distToLine,L as distToPolyline,A as distToRect,k as distToSegment,xi as drawingSettingsSchema,pi as drawingShortcuts,jo as drawingToolIcon,Eo as drawingToolIconIds,ao as encodeClipboardPayload,N as extendSegment,Ke as fixedRangeVolumeProfileAnalysis,s as formatRatio,l as gannLabel,ai as getDrawingTool,ci as hasDrawingTool,ss as iconSprite,rs as iconSvg,ls as iconUse,ds as keyToDrawingAction,n as levelColor,ui as matchDrawingShortcut,Jr as migrateDrawings,yt as pressureWidth,ft as rdpSimplify,ot as readDrawingSetting,st as readDrawingSettings,F as rectOf,Sr as registerBuiltinDrawingTools,li as registerDrawingTool,hi as registeredDrawingTools,vo as sanitizeDrawing,Lr as sortByZIndex,ro as systemClipboard,as as toolCursor};
|
|
2
2
|
//# sourceMappingURL=openalgo-charts.draw.mjs.map
|