import{SpaceTypeMap}from"../common/constant";import{REQUEST_TYPE}from"./model-api";import{createUnityColor}from"./tool";import upperFirst from"lodash/upperFirst";export default function createUnityModelCreator(a){async function b(b){var j,k,l;!1===b.exists&&(b=await c(b));let[m=0,n=0,o=0]=(null===(j=b.props)||void 0===j?void 0:j.position)||[0,0,0],[p=1,q=1,r=1]=(null===(k=b.props)||void 0===k?void 0:k.scale)||[0,0,0],[s=0,t=0,u=0]=(null===(l=b.props)||void 0===l?void 0:l.rotation)||[0,0,0],v={x:m,y:n,z:o},w={x:p,y:q,z:r},A={x:s,y:t,z:u,order:"XYZ"},B=!0,C=!1,D="",E=!1,F=NaN,G={get opacity(){return F},set opacity(a){h(b.modelId,a).then(()=>{F=a})},get modelId(){return b.modelId},get url(){return b.url||""},get position(){return{x:v.x,y:v.y,z:v.z}},set position(a){i(b.modelId,{position:a}).then(()=>{v=a})},get scale(){return{x:w.x,y:w.y,z:w.z}},set scale(a){i(b.modelId,{scale:a}).then(()=>{w=a})},get rotation(){return{x:A.x,y:A.y,z:A.z,order:A.order,set:({x:a,y:c,z:d,order:e="XYZ"})=>{i(b.modelId,{rotation:{x:a,y:c,z:d,order:e}}).then(()=>{A={x:a,y:c,z:d,order:e}})}}},set rotation({x:a,y:c,z:d,order:e="XYZ"}){i(b.modelId,{rotation:{x:a,y:c,z:d,order:e}}).then(()=>{A={x:a,y:c,z:d,order:e}})},get visible(){return B},set visible(a){e(b.modelId,a).then(()=>{B=a})},get color(){return D},set color(a){g(b.modelId,a).then(()=>{D=a})},get disabled(){return C},set disabled(a){d(b.modelId,a).then(()=>{C=a})},get selected(){return E},set selected(a){let c;c="boolean"==typeof a?{active:a}:a,f(b.modelId,c).then(()=>{E=a})},dispose:async function(){return G.removeFromParent()},removeFromParent:async function(){return a.invoke(REQUEST_TYPE.RemoveSceneObject,{ObjectIds:[b.modelId]})},add:async function(){},attach:async function(){},remove:function(a){return a.removeFromParent()},replace:()=>{},getTransform:async function(){return a.invoke(REQUEST_TYPE.GetTransform,{ObjectId:b.modelId}).then(a=>(v={x:a.Position.X,y:a.Position.Y,z:a.Position.Z},w={x:a.Scale.X,y:a.Scale.Y,z:a.Scale.Z},A={x:a.EulerAngles.X,y:a.EulerAngles.Y,z:a.EulerAngles.Z,order:"XYZ"},a))},update:a=>i(b.modelId,a).then(()=>{a.position&&(v=a.position),a.rotation&&(A=a.rotation),a.scale&&(w=a.scale)})};return G}const c=async b=>{var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s;if(!b.url)throw new Error("\u672A\u6307\u5B9A\u81EA\u5B9A\u4E49\u6A21\u578B\u7684\u5730\u5740\uFF01");const t=Object.entries(SpaceTypeMap).reduce((a,[b,c])=>(a[c]=b,a),{}),u=+t[b.type];let v=0;switch(+u){case 1001:v=1;break;case 1002:v=2;break;case 1003:v=3;break;default:v=0}const[w=0,x=0,y=0]=null!==(c=null===(d=b.props)||void 0===d?void 0:d.position)&&void 0!==c?c:[],[z=0,A=0,B=0]=null!==(e=null===(f=b.props)||void 0===f?void 0:f.rotation)&&void 0!==e?e:[],[C=1,D=1,E=1]=null!==(g=null===(h=b.props)||void 0===h?void 0:h.scale)&&void 0!==g?g:[],[F=0,G=0,H=0]=null!==(i=null===(j=b.props)||void 0===j?void 0:j.anchorOffset)&&void 0!==i?i:[];let I=await a.invoke(REQUEST_TYPE.AddCustomObject,{SpaceId:b.parentId,ContentType:Number.isNaN(v)?0:v,ObjectName:b.name,Classes:[b.realType],Content:b.url,Position:{X:w,Y:x,Z:y},Rotation:{X:z,Y:A,Z:B},Scale:{X:C,Y:D,Z:E},AnchorOffset:{X:F,Y:G,Z:H},KeepFaceToCamera:null!==(k=null===(l=b.props)||void 0===l?void 0:l.faceToCamera)&&void 0!==k&&k,FontSize:null===(m=b.props)||void 0===m?void 0:m.fontSize,FontColor:createUnityColor(null!==(n=null===(o=b.props)||void 0===o?void 0:o.fontColor)&&void 0!==n?n:"#66ccff"),AsUIElement:null!==(p=null===(q=b.props)||void 0===q?void 0:q.depthTest)&&void 0!==p&&p,ImageTemplate:null===(r=b.props)||void 0===r?void 0:r.template});const J={...(null!==(s=b.props)&&void 0!==s?s:{})};return J.isCustom=!0,b.props=J,b.modelId=I,b},d=async(b,c)=>{await a.invoke(REQUEST_TYPE.SetInteractive,{ObjectIds:[b],Interactive:c})},e=async(b,c)=>{await a.invoke(REQUEST_TYPE.SetVisibility,{ObjectIds:[b],Visible:c})},f=async(b,c)=>{let d={};for(let a in c){let b=upperFirst(a);d[b]="Color"===b?createUnityColor(c[a]):c[a]}return await a.invoke(REQUEST_TYPE.HightlightTargetObject,{...d,ObjectIds:[b]}),c},g=async(b,c)=>{b.includes("Room")?await a.invoke(REQUEST_TYPE.SetRoomApperance,{RoomIds:[b],Color:createUnityColor(c)}):await a.invoke(REQUEST_TYPE.SetObjectsViewMode,{ObjectIds:[b],ViewMode:"Transparent",Setting:{Color:createUnityColor(c)}})},h=async(b,c)=>{1===c?await a.invoke(REQUEST_TYPE.SetObjectsViewMode,{ObjectIds:[b],ViewMode:"Normal"}):await a.invoke(REQUEST_TYPE.SetObjectsViewMode,{ObjectIds:[b],ViewMode:"Transparent",Setting:{Color:{R:1,G:1,B:1,A:c}}})},i=async(b,{position:c,rotation:d,scale:e,duration:f,moveSpeed:g})=>{let h={ObjectId:b};c&&(h.Position={X:c.x,Y:c.y,Z:c.z}),e&&(h.Scale={X:e.x,Y:e.y,Z:e.z}),d&&(h.EulerAngles={X:d.x,Y:d.y,Z:d.z}),f&&(h.Duration=f),g&&(h.MoveSpeed=g),await a.invoke(REQUEST_TYPE.SetTransform,h)};return async function d(a,c){c||(c=new Map);for(let e of a){let a=await b(e);c.set(e.modelId,a),e.children&&0<e.children.length&&(await d(e.children,c))}return c}}