/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{createScreenPoint as e}from"../../core/screenUtils.js";import{nameToId as n}from"../../core/accessorSupport/PropertyOrigin.js";import t from"../../geometry/Extent.js";import o from"../../geometry/Point.js";import{getInfo as r}from"../../geometry/support/spatialReferenceUtils.js";import i from"./ControlPoint.js";import s from"./ControlPointsGeoreference.js";import a from"./CornersGeoreference.js";import c from"./ExtentAndRotationGeoreference.js";const p=100,l=.5;async function y(e,n){e.loaded||await e.load();const{contentWidth:a,contentHeight:c}=e,{spatialReference:y}=n,m=r(y);let f=!1;if(m){const e=m.valid[1]-m.valid[0];n.width>e&&(n=new t({xmin:m.valid[0],ymin:n.ymin,xmax:m.valid[1],ymax:n.ymax,spatialReference:n.spatialReference}),f=!0)}const u=n.expand(l),{center:w,width:P,height:d}=u,x=a/c,g=f||x>1,h=g?p:p*x,R=g?p/x:p,j=g?P:d*x,v=g?P/x:d,b=w.x-j/2,O=w.x+j/2,k=w.y+v/2,C=w.y-v/2;return new s({controlPoints:[new i({sourcePoint:{x:h,y:c-R},mapPoint:new o({x:b,y:C,spatialReference:y})}),new i({sourcePoint:{x:h,y:R},mapPoint:new o({x:b,y:k,spatialReference:y})}),new i({sourcePoint:{x:a-h,y:R},mapPoint:new o({x:O,y:k,spatialReference:y})}),new i({sourcePoint:{x:a-h,y:c-R},mapPoint:new o({x:O,y:C,spatialReference:y})})],width:a,height:c})}function m(e){const n=e.georeference;if("control-points"!==n.type)return null;const t={wkid:0},{width:r,height:i}=n,a=n.controlPoints;return new s({controlPoints:a.map(e=>{const{x:n,y:r}=e.sourcePoint;return{sourcePoint:{x:n,y:r},mapPoint:new o({x:n,y:i-r,spatialReference:t})}}),width:r,height:i})}function f(e){return"media"===e?.type}function u(e,t){const o=n(t);return f(e)&&!!e.portalItem&&null!=o&&o>3}function w(n,t,r){if(!n||"control-points"===n.type)return n;const{coords:a}=n;if(5!==a?.rings[0]?.length)return null;const[c,p,l,y]=a.rings[0],{spatialReference:m}=a;return new s({controlPoints:[new i({mapPoint:new o({x:c[0],y:c[1],spatialReference:m}),sourcePoint:e(0,r)}),new i({mapPoint:new o({x:p[0],y:p[1],spatialReference:m}),sourcePoint:e(0,0)}),new i({mapPoint:new o({x:l[0],y:l[1],spatialReference:m}),sourcePoint:e(t,0)}),new i({mapPoint:new o({x:y[0],y:y[1],spatialReference:m}),sourcePoint:e(t,r)})],width:t,height:r})}function P(e,n,t){return{enabled:!u(t?.layer,t?.origin),ignoreOrigin:!0}}const d={json:{name:"url",type:String,write:{overridePolicy:P}}},x={readOnly:!0,json:{read:!1,write:{target:"mediaType",overridePolicy:P}}},g={types:{key:"type",base:null,typeMap:{"control-points":s,corners:a,"extent-and-rotation":c}},json:{types:{key:"type",base:null,typeMap:{"control-points":s}},write:{overridePolicy:()=>({enabled:!0,ignoreOrigin:!0})}}};export{y as createDefaultControlPointsGeoreference,m as createLocalModeControlPointsGeoreference,u as isWritingLayerFromItemToWebDocument,d as mediaElementUrlProperty,g as mediaGeoreferenceProperty,x as mediaTypeProperty,w as toControlPointsGeoreference};
