/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
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";import p from"./GeoreferenceBase.js";const l=100,y=.5;async function m(e,n){e.loaded||await e.load();const{contentWidth:a,contentHeight:c}=e,{spatialReference:p}=n,m=r(p);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(y),{center:w,width:P,height:d}=u,x=a/c,g=f||x>1,h=g?l:l*x,R=g?l/x:l,j=g?P:d*x,v=g?P/x:d,b=w.x-j/2,G=w.x+j/2,O=w.y+v/2,k=w.y-v/2;return new s({controlPoints:[new i({sourcePoint:{x:h,y:c-R},mapPoint:new o({x:b,y:k,spatialReference:p})}),new i({sourcePoint:{x:h,y:R},mapPoint:new o({x:b,y:O,spatialReference:p})}),new i({sourcePoint:{x:a-h,y:R},mapPoint:new o({x:G,y:O,spatialReference:p})}),new i({sourcePoint:{x:a-h,y:c-R},mapPoint:new o({x:G,y:k,spatialReference:p})})],width:a,height:c})}function f(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 u(e){return"media"===e?.type}function w(e,t){const o=n(t);return u(e)&&!!e.portalItem&&null!=o&&o>3}function P(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 d(e,n,t){return{enabled:!w(t?.layer,t?.origin),ignoreOrigin:!0}}const x={json:{name:"url",type:String,write:{overridePolicy:d}}},g={readOnly:!0,json:{read:!1,write:{target:"mediaType",overridePolicy:d}}},h={types:{key:"type",base:p,typeMap:{"control-points":s,corners:a,"extent-and-rotation":c}},json:{types:{key:"type",base:p,typeMap:{"control-points":s}},write:{overridePolicy:()=>({enabled:!0,ignoreOrigin:!0})}}};export{m as createDefaultControlPointsGeoreference,f as createLocalModeControlPointsGeoreference,w as isWritingLayerFromItemToWebDocument,x as mediaElementUrlProperty,h as mediaGeoreferenceProperty,g as mediaTypeProperty,P as toControlPointsGeoreference};
