/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__decorate as e}from"tslib";import{watch as t,syncAndInitial as i,sync as s}from"../../../core/reactiveUtils.js";import{property as r,subclass as a}from"../../../core/accessorSupport/decorators.js";import{PluginBase as n}from"./PluginBase.js";import{createSquare as o,createCircle as l,createEllipse as d,createRectangle as p}from"../support/createUtils.js";import{knownFieldDisplayOrders as u}from"../support/types.js";const h=Symbol("own-shape"),c=Symbol("shape-area-measurement"),m=Symbol("shape-size-measurement-x"),f=Symbol("shape-size-measurement-y"),y=Symbol("shape-area-field"),_=Symbol("shape-x-size-field"),g=Symbol("shape-y-size-field"),S=Symbol("shape-radius-field");let v=class extends n{constructor(){super(...arguments),this.shapeType="rectangle",this.defaultCentered=!1,this.defaultUniform=!1}get _globalModifierState(){return this._context?.getGlobalState()}get effectiveUniform(){return this.defaultUniform!==!!this._globalModifierState?.preserveAspectRatio}get effectiveCentered(){return this.defaultCentered!==this._globalModifierState?.centered}set shapeResult(e){this._set("shapeResult",e),e?"center"in e?(this._radiusField?.show(),this._xSizeField?.hide(),this._ySizeField?.hide(),this._areaField?.show()):"midpoints"in e&&e.midpoints&&(this._radiusField?.hide(),this._xSizeField?.show(),this._ySizeField?.show(),this._areaField?.show()):(this._xSizeField?.hide(),this._ySizeField?.hide(),this._radiusField?.hide(),this._areaField?.hide());const t=e?.geometry,i=this._session;if(i&&t){const e=t.curveRings??t.rings,s=e?.at(0);s&&i.appendOrReplacePart(h,s)}}start(e){super.start(e),this._interactiveHandle??=e.createInteractiveHandle({defaultCaptureMode:"capture-when-engaged",selfSnappingEnabled:!1},{disableDefaultXYFields:!0}),this.addHandles([t(()=>[this._interactiveHandle?.outputMapPosition,this.effectiveCentered,this.effectiveUniform],([e,t,i])=>{if(!e||this._isComplete)return;const s=this._interactiveHandle?.outputMapPositionOfDragStart??e,r=s===e?null:e;this.shapeResult=this._generateShape(s,r,t,i)},i),t(()=>this._interactiveHandle?.dragging,(t,i)=>{t&&e.setGlobalState("drawAtFixedElevation",!0),!t&&i&&"grabbable-object"!==this._interactiveHandle?.captureMode&&e.requestComplete()},s)],this._ownHandlesKey),this._areaField??=this._context?.createMeasuredField({id:y,preset:"area",getMeasurementInput:()=>this.shapeResult?.geometry,measure:async(e,t)=>{if(!e)return null;const i=t.inputUnitInfos.area.unit;return this._context?.measureArea(e,c,"tooltip",i)},fieldProperties:{displayOrder:u.ShapeMeasurements}});const r=Symbol("Size-group");this._xSizeField??=this._context?.createMeasuredField({id:_,preset:"generic-length",getMeasurementInput:()=>this.shapeResult,measure:async(e,t)=>e&&"midpoints"in e&&e.midpoints?this._context?.measureDistance([e.midpoints?.left,e.midpoints?.right],m,"tooltip",t.inputUnitInfos.length.unit):null,fieldProperties:{displayOrder:u.Middle+1,displayGroup:r,title:e=>e.messages.sketch.size,readOnly:!0}}),this._ySizeField??=this._context?.createMeasuredField({id:g,preset:"generic-length",getMeasurementInput:()=>this.shapeResult,measure:async(e,t)=>e&&"midpoints"in e&&e.midpoints?this._context?.measureDistance([e.midpoints?.bottom,e.midpoints?.top],f,"tooltip",t.inputUnitInfos.length.unit):null,fieldProperties:{displayOrder:u.Middle+2,displayGroup:r,title:e=>e.messages.sketch.size,readOnly:!0}}),this._radiusField??=this._context?.createMeasuredField({id:S,preset:"radius",getMeasurementInput:()=>this.shapeResult,measure:async(e,t)=>{if(e&&"center"in e&&null!=e.center)return this._context?.measureDistance([e.center,e.edge],f,"tooltip",t.inputUnitInfos.length.unit)},fieldProperties:{displayOrder:u.Middle+1,readOnly:!0}})}_generateShape(e,t,i,s){const{shapeType:r}=this,{resolution:a}=this._context?.getViewInformation()??{},n=this._context?.getViewAlignedSurfaceCoordinateSystem(e);if(!n||null==a)return null;const u=[e.x,e.y],h=t&&[t.x,t.y];if(!h){const e=[[u,n.makeMapPoint(u[0]+48*a,u[1])],n,!0];return"rectangle"===r?o(...e):l(...e,void 0)}const c=[[u,h],n,i];return s?"circle"===r?l(...c,void 0):o(...c):"circle"===r?d(...c):p(...c)}};e([r()],v.prototype,"_interactiveHandle",void 0),e([r()],v.prototype,"_areaField",void 0),e([r()],v.prototype,"_xSizeField",void 0),e([r()],v.prototype,"_ySizeField",void 0),e([r()],v.prototype,"_radiusField",void 0),e([r()],v.prototype,"_globalModifierState",null),e([r()],v.prototype,"shapeType",void 0),e([r()],v.prototype,"defaultCentered",void 0),e([r()],v.prototype,"defaultUniform",void 0),e([r()],v.prototype,"effectiveUniform",null),e([r()],v.prototype,"effectiveCentered",null),e([r()],v.prototype,"shapeResult",null),v=e([a("esri.views.draw.plugins.ShapePlugin")],v);export{v as ShapePlugin};
