/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import e from"../../../core/Error.js";import t from"../../../geometry/Geometry.js";import{EsriFieldTypes as n,EsriMergePolicyType as i,EsriSplitPolicyType as a,EsriGeometryTypes as o,EsriGraphPropertyRole as r,NamedObjectTypes as l}from"../types.js";import{geometryToWasm as s}from"./queryToWasmEncodedFactories.js";function d(t,n){if(!t.typeName)throw new e("knowledge-graph:no-type-name","You must indicate the entity/relationship named object type to apply edits");if(!("originId"in t)){const e=new n.EntityValue;e.deleteLater(),e.type_name=t.typeName;for(const[i,a]of Object.entries(t.properties))e.set_key_value(i,O(a,n));return t.id&&e.set_id(t.id),e}if("originId"in t&&"destinationId"in t){const e=new n.RelationshipValue;e.deleteLater(),e.type_name=t.typeName;for(const[i,a]of Object.entries(t.properties))e.set_key_value(i,O(a,n));return t.id&&e.set_id(t.id),t.originId&&t.destinationId&&e.set_related_entity_ids(t.originId,t.destinationId),e}throw new e("knowledge-graph:applyEdits-encoding-failure","Could not determine the type of a named graph object passed to the encoder")}function u(e){return{xy_resolution:e.xyResolution,x_false_origin:e.xFalseOrigin,y_false_origin:e.yFalseOrigin,z_resolution:e.zResolution,z_false_origin:e.zFalseOrigin,m_resolution:e.mResolution,m_false_origin:e.mFalseOrigin}}function p(e,t){const n=new t.EntityType;return n.deleteLater(),v(e,t,n)}function y(e,t){const n=new t.RelationshipType;n.deleteLater();for(const i of e.endPoints){const e=new t.EndPoint;e.deleteLater(),e.origin_entity_type=i.originEntityType,e.dest_entity_type=i.destinationEntityType,n.add_end_point(e)}return v(e,t,n)}function c(e,t){const n=new t.EntityType;return n.deleteLater(),b(e,n)}function f(e,t){const n=new t.RelationshipType;return n.deleteLater(),b(e,n)}function m(e,t){const i=new t.GraphProperty;return i.deleteLater(),i.name=e.name,i.alias=e.alias??"",i.field_type={value:n[e.fieldType]},i.geometry_type={value:e.geometryType?o[e.geometryType]:o.esriGeometryNull},i.has_m=e.hasM??!1,i.has_z=e.hasZ??!1,i.nullable=e.nullable??!1,i.editable=e.editable,i.required=e.required,i.default_visibility=e.defaultVisibility,i.system_maintained=e.systemMaintained??!1,i.role={value:r[e.role]},i.default_value=O(e.defaultValue,t),i.domain=e.domain??"",i}function _(e,t){const n=new t.GraphProperty;return n.deleteLater(),e.hasOwnProperty("alias")&&(n.alias=e.alias),e.hasOwnProperty("editable")&&(n.editable=e.editable),e.hasOwnProperty("required")&&(n.required=e.required),e.hasOwnProperty("defaultVisibility")&&(n.default_visibility=e.defaultVisibility),e.hasOwnProperty("defaultValue")&&(n.default_value=O(e.defaultValue,t)),e.hasOwnProperty("domain")&&(n.domain=e.domain),n}function g(e,t){const n=new t.SearchIndexProperties;n.deleteLater();for(const i of e.propertyNames)n.add_search_property_name(i);return n}function w(e,t){const n=new t.FieldIndex;n.deleteLater(),n.name=e.name,n.unique=e.unique,n.ascending=e.ascending,n.description=e.description??"";for(const i of e.fieldNames)n.add_field(i);return n}function h(e,t,o,r){const l=new t.Domain;l.deleteLater(),l.name=e.name;const s=n[o];l.field_type={value:s};let d=s;if(s!==n.esriFieldTypeTimeOnly&&s!==n.esriFieldTypeDateOnly&&s!==n.esriFieldTypeTimestampOffset||(d=n.esriFieldTypeString),r&&(l.merge_policy_type={value:i[r.mergePolicy]},l.split_policy_type={value:a[r.splitPolicy]}),"coded-value"===e.type){const n=e;l.type="codedValue";const i=new t.CodedValueDomain;i.deleteLater();for(const e of n.codedValues){const n=new t.CodedValue;n.deleteLater(),n.name=e.name,n.set_code(e.code,{value:d}),i.add_coded_value(n)}l.coded_value_domain=i}else if("range"===e.type){const n=e;l.type="range";const i=new t.RangeDomain;i.deleteLater(),i.add_value(n.minValue,{value:d}),i.add_value(n.maxValue,{value:d}),l.range_domain=i}else l.type=e.type??"unspecified";return l}function v(e,t,n){n.name=e.name,n.alias=e.alias??"",n.role={value:l[e.role]},n.strict=e.strict;for(const i of e.properties){const e=m(i,t);n.add_property(e)}return n}function b(e,t){return e.hasOwnProperty("alias")&&(t.alias=e.alias),t}function O(e,n){return null==e?null:"object"!=typeof e||e instanceof Date?e:e instanceof t?s(e,n):null}export{h as domainToWasm,p as entityTypeToWasm,w as fieldIndexToWasm,m as graphPropertyToWasm,u as inputQuantizationParametersToWasmFormat,d as namedObjectToWasm,y as relationshipTypeToWasm,c as updateEntityTypeToWasm,_ as updateGraphPropertyToWasm,f as updateRelationshipTypeToWasm,g as updateSearchPropertiesToWasm};
