/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../../core/maybe","../../../../../core/screenUtils","../definitions","./CollisionGrid","./visualVariableSimpleUtils"],(function(e,t,i,n,o,r){"use strict";const s=254,l=255,a=0;function u(e,i){const n=[];e.forEachTile((e=>n.push(e))),n.sort(((e,t)=>e.instanceId-t.instanceId)),n.forEach((e=>{t.isSome(e.labelMetrics)&&e.isReady&&i(e,e.labelMetrics.getCursor())}))}function c(e){return e.layer&&("feature"===e.layer.type||"csv"===e.layer.type||"geojson"===e.layer.type||"ogc-feature"===e.layer.type||"stream"===e.layer.type||"subtype-group"===e.layer.type||"wfs"===e.layer.type)}function d(e){return t=>i.pt2px(r.getSizeForValueSimple(t,e))}function f(e){const t="visualVariables"in e&&e.visualVariables;if(!t)return null;for(const i of t)if("size"===i.type)return d(i);return null}function b(e){for(const t of e){const e="featureReduction"in t&&t.featureReduction&&"labelingInfo"in t.featureReduction&&t.featureReduction,i=[...t.labelingInfo||[],...(null==e?void 0:e.labelingInfo)||[]];if(!t.labelsVisible||!i.length)continue;if(i.some((e=>"none"===e.deconflictionStrategy)))return!0}return!1}function y(e,i){if(!c(i))return;const n="subtype-group"===i.layer.type?i.layer.sublayers.items:[i.layer],o=i.layer.geometryType,r=!b(n),s={};if("subtype-group"!==i.layer.type){var l;if("heatmap"===(null==(l=i.tileRenderer)?void 0:l.type))return;const e=f(i.layer.renderer);s[0]=e}const a=i.tileRenderer;if(t.isNone(a))return;const u=i.layer.visible&&!i.suspended;e.push({tileRenderer:a,vvEvaluators:s,deconflictionEnabled:r,geometryType:o,visible:u})}let p=function(){function e(){}var i=e.prototype;return i.run=function(e,t,i){const n=[];for(let o=e.length-1;o>=0;o--){y(n,e[o])}this._transformMetrics(n,t),this._runCollision(n,t,i)},i._runCollision=function(e,t,i){const[n,r]=t.state.size,s=new o.CollisionBitsetGrid(n*t.pixelRatio,r*t.pixelRatio);for(const{tileRenderer:o,deconflictionEnabled:c,visible:d}of e){const e=o.featuresView.attributeView;c?d?(this._prepare(o),this._collideVisible(s,o,i),this._collideInvisible(s,o)):u(o,((t,i)=>{for(;i.nextId();)e.setLabelMinZoom(i.id,l)})):u(o,((t,i)=>{for(;i.nextId();)e.setLabelMinZoom(i.id,a)}))}},i._isFiltered=function(e,i,o){const r=i.getFilterFlags(e),s=!o.hasFilter||!!(r&n.FILTER_FLAG_0),l=t.isNone(o.featureEffect)||o.featureEffect.excludedLabelsVisible||!!(r&n.EFFECT_FLAG_0);return!(s&&l)},i._prepare=function(e){const t=e.featuresView.attributeView,i=new Set;u(e,((n,o)=>{for(;o.nextId();){if(i.has(o.id))continue;if(i.add(o.id),this._isFiltered(o.id,t,e.layerView)){t.setLabelMinZoom(o.id,s);continue}t.getLabelMinZoom(o.id)!==a?t.setLabelMinZoom(o.id,l):t.setLabelMinZoom(o.id,a)}}))},i._collideVisible=function(e,t,i){const n=t.featuresView.attributeView,r=new Set;u(t,((t,l)=>{for(;l.nextId();)if(!r.has(l.id))if(t.key.level===i){if(0===n.getLabelMinZoom(l.id)){switch(e.insertMetrics(l)){case o.OUTSIDE_EXTENT:break;case o.HAS_COLLISION:n.setLabelMinZoom(l.id,s),r.add(l.id);break;case o.NONE:n.setLabelMinZoom(l.id,a),r.add(l.id)}}}else n.setLabelMinZoom(l.id,s)}))},i._collideInvisible=function(e,t){const i=t.featuresView.attributeView,n=new Set;u(t,((t,r)=>{for(;r.nextId();)if(!n.has(r.id)&&i.getLabelMinZoom(r.id)===l){switch(e.insertMetrics(r)){case o.OUTSIDE_EXTENT:break;case o.HAS_COLLISION:i.setLabelMinZoom(r.id,l),n.add(r.id);break;case o.NONE:i.setLabelMinZoom(r.id,a),n.add(r.id)}}}))},i._transformMetrics=function(e,i){for(const{tileRenderer:n,geometryType:o,vvEvaluators:r}of e)u(n,((e,s)=>{const l=n.featuresView.attributeView,a=e.transforms.labelMat2d;a[4]=Math.round(a[4]),a[5]=Math.round(a[5]);const u=a[4],c=a[5],d="polyline"===o;for(;s.next();){const e=s.boundsCount,n=s.anchorX,o=s.anchorY;let f=s.size;const b=r[0];if(t.isSome(b)){const e=b(l.getVVSize(s.id));f=isNaN(e)||null==e||e===1/0?f:e}const y=s.directionX*(f/2),p=s.directionY*(f/2);for(let t=0;t<e;t++){let e=n,r=s.anchorY;if(d){let n=e+s.boundsX(t)+y,o=r+s.boundsY(t)+p;i.state.rotation?(n=a[0]*n+a[2]*o+a[4],o=a[1]*n+a[3]*o+a[5]):(n+=u,o+=c),s.setBoundsComputedAnchorX(t,Math.floor(n)),s.setBoundsComputedAnchorY(t,Math.floor(o))}else{i.state.rotation?(e=a[0]*n+a[2]*o+a[4],r=a[1]*n+a[3]*o+a[5]):(e+=u,r+=c);const l=e+s.boundsX(t)+y,d=r+s.boundsY(t)+p;s.setBoundsComputedAnchorX(t,l),s.setBoundsComputedAnchorY(t,d)}}}}))},e}();e.CollisionEngine=p,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
