var __extends=this&&this.__extends||function(){var o=function(e,t){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)};return function(e,t){function r(){this.constructor=e}o(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}}();import{extend}from"../array.js";import{createOrUpdate}from"../extent.js";import{transformExtentWithOptions,transformGeometryWithOptions}from"./Feature.js";import GMLBase,{GMLNS}from"./GMLBase.js";import{readNonNegativeIntegerString,writeStringTextNode}from"./xsd.js";import GeometryLayout from"../geom/GeometryLayout.js";import LineString from"../geom/LineString.js";import MultiLineString from"../geom/MultiLineString.js";import MultiPolygon from"../geom/MultiPolygon.js";import Polygon from"../geom/Polygon.js";import{assign}from"../obj.js";import{get as getProjection}from"../proj.js";import{createElementNS,getAllTextContent,makeArrayPusher,makeChildAppender,makeReplacer,makeSimpleNodeFactory,OBJECT_PROPERTY_NODE_FACTORY,parseNode,pushParseAndPop,pushSerializeAndPop,XML_SCHEMA_INSTANCE_URI}from"../xml.js";var schemaLocation=GMLNS+" http://schemas.opengis.net/gml/3.1.1/profiles/gmlsfProfile/1.0.0/gmlsf.xsd",MULTIGEOMETRY_TO_MEMBER_NODENAME={MultiLineString:"lineStringMember",MultiCurve:"curveMember",MultiPolygon:"polygonMember",MultiSurface:"surfaceMember"},GML3=function(r){function e(e){var t=this,e=e||{};return(t=r.call(this,e)||this).surface_=void 0!==e.surface&&e.surface,t.curve_=void 0!==e.curve&&e.curve,t.multiCurve_=void 0===e.multiCurve||e.multiCurve,t.multiSurface_=void 0===e.multiSurface||e.multiSurface,t.schemaLocation=e.schemaLocation||schemaLocation,t.hasZ=void 0!==e.hasZ&&e.hasZ,t}return __extends(e,r),e.prototype.readMultiCurve_=function(e,t){e=pushParseAndPop([],this.MULTICURVE_PARSERS_,e,t,this);if(e)return new MultiLineString(e)},e.prototype.readMultiSurface_=function(e,t){e=pushParseAndPop([],this.MULTISURFACE_PARSERS_,e,t,this);if(e)return new MultiPolygon(e)},e.prototype.curveMemberParser_=function(e,t){parseNode(this.CURVEMEMBER_PARSERS_,e,t,this)},e.prototype.surfaceMemberParser_=function(e,t){parseNode(this.SURFACEMEMBER_PARSERS_,e,t,this)},e.prototype.readPatch_=function(e,t){return pushParseAndPop([null],this.PATCHES_PARSERS_,e,t,this)},e.prototype.readSegment_=function(e,t){return pushParseAndPop([null],this.SEGMENTS_PARSERS_,e,t,this)},e.prototype.readPolygonPatch_=function(e,t){return pushParseAndPop([null],this.FLAT_LINEAR_RINGS_PARSERS,e,t,this)},e.prototype.readLineStringSegment_=function(e,t){return pushParseAndPop([null],this.GEOMETRY_FLAT_COORDINATES_PARSERS,e,t,this)},e.prototype.interiorParser_=function(e,t){e=pushParseAndPop(void 0,this.RING_PARSERS,e,t,this);e&&t[t.length-1].push(e)},e.prototype.exteriorParser_=function(e,t){e=pushParseAndPop(void 0,this.RING_PARSERS,e,t,this);e&&(t[t.length-1][0]=e)},e.prototype.readSurface_=function(e,t){var r=pushParseAndPop([null],this.SURFACE_PARSERS_,e,t,this);if(r&&r[0]){for(var o=r[0],i=[o.length],n=void 0,n=1,a=r.length;n<a;++n)extend(o,r[n]),i.push(o.length);return new Polygon(o,GeometryLayout.XYZ,i)}},e.prototype.readCurve_=function(e,t){e=pushParseAndPop([null],this.CURVE_PARSERS_,e,t,this);if(e)return new LineString(e,GeometryLayout.XYZ)},e.prototype.readEnvelope_=function(e,t){e=pushParseAndPop([null],this.ENVELOPE_PARSERS_,e,t,this);return createOrUpdate(e[1][0],e[1][1],e[2][0],e[2][1])},e.prototype.readFlatPos_=function(e,t){for(var r,o=getAllTextContent(e,!1),i=/^\s*([+\-]?\d*\.?\d+(?:[eE][+\-]?\d+)?)\s*/,n=[];r=i.exec(o);)n.push(parseFloat(r[1])),o=o.substr(r[0].length);if(""===o){e=t[0].srsName,t="enu";if("neu"===(t=e?getProjection(e).getAxisOrientation():t))for(var a=void 0,a=0,p=n.length;a<p;a+=3){var s=n[a],u=n[a+1];n[a]=u,n[a+1]=s}e=n.length;if(2==e&&n.push(0),0!==e)return n}},e.prototype.readFlatPosList_=function(e,t){for(var r,o,i,n=getAllTextContent(e,!1).replace(/^\s*|\s*$/g,""),t=t[0],a=t.srsName,t=t.srsDimension,p="enu",s=(a&&(p=getProjection(a).getAxisOrientation()),n.split(/\s+/)),u=2,l=(e.getAttribute("srsDimension")?u=readNonNegativeIntegerString(e.getAttribute("srsDimension")):e.getAttribute("dimension")?u=readNonNegativeIntegerString(e.getAttribute("dimension")):e.parentNode.getAttribute("srsDimension")?u=readNonNegativeIntegerString(e.parentNode.getAttribute("srsDimension")):t&&(u=readNonNegativeIntegerString(t)),[]),m=0,h=s.length;m<h;m+=u)r=parseFloat(s[m]),o=parseFloat(s[m+1]),i=3===u?parseFloat(s[m+2]):0,"en"===p.substr(0,2)?l.push(r,o,i):l.push(o,r,i);return l},e.prototype.writePos_=function(e,t,r){var r=r[r.length-1],o=r.hasZ,r=(e.setAttribute("srsDimension",o?"3":"2"),r.srsName),i="enu",r=(r&&(i=getProjection(r).getAxisOrientation()),t.getCoordinates()),t="en"===i.substr(0,2)?r[0]+" "+r[1]:r[1]+" "+r[0];o&&(t+=" "+(r[2]||0)),writeStringTextNode(e,t)},e.prototype.getCoords_=function(e,t,r){var o="enu",t="en"===(o=t?getProjection(t).getAxisOrientation():o).substr(0,2)?e[0]+" "+e[1]:e[1]+" "+e[0];return r&&(t+=" "+(e[2]||0)),t},e.prototype.writePosList_=function(e,t,r){for(var o,r=r[r.length-1],i=r.hasZ,n=(e.setAttribute("srsDimension",i?"3":"2"),r.srsName),a=t.getCoordinates(),p=a.length,s=new Array(p),u=0;u<p;++u)o=a[u],s[u]=this.getCoords_(o,n,i);writeStringTextNode(e,s.join(" "))},e.prototype.writePoint_=function(e,t,r){var o=r[r.length-1].srsName,o=(o&&e.setAttribute("srsName",o),createElementNS(e.namespaceURI,"pos"));e.appendChild(o),this.writePos_(o,t,r)},e.prototype.writeEnvelope=function(e,t,r){var o=r[r.length-1].srsName,o=(o&&e.setAttribute("srsName",o),[t[0]+" "+t[1],t[2]+" "+t[3]]);pushSerializeAndPop({node:e},this.ENVELOPE_SERIALIZERS_,OBJECT_PROPERTY_NODE_FACTORY,o,r,["lowerCorner","upperCorner"],this)},e.prototype.writeLinearRing_=function(e,t,r){var o=r[r.length-1].srsName,o=(o&&e.setAttribute("srsName",o),createElementNS(e.namespaceURI,"posList"));e.appendChild(o),this.writePosList_(o,t,r)},e.prototype.RING_NODE_FACTORY_=function(e,t,r){var t=t[t.length-1],o=t.node,i=t.exteriorWritten;return void 0===i&&(t.exteriorWritten=!0),createElementNS(o.namespaceURI,void 0!==i?"interior":"exterior")},e.prototype.writeSurfaceOrPolygon_=function(e,t,r){var o,i=r[r.length-1],n=i.hasZ,i=i.srsName;"PolygonPatch"!==e.nodeName&&i&&e.setAttribute("srsName",i),"Polygon"===e.nodeName||"PolygonPatch"===e.nodeName?(o=t.getLinearRings(),pushSerializeAndPop({node:e,hasZ:n,srsName:i},this.RING_SERIALIZERS_,this.RING_NODE_FACTORY_,o,r,void 0,this)):"Surface"===e.nodeName&&(n=createElementNS(e.namespaceURI,"patches"),e.appendChild(n),this.writeSurfacePatches_(n,t,r))},e.prototype.writeCurveOrLineString_=function(e,t,r){var o=r[r.length-1].srsName;"LineStringSegment"!==e.nodeName&&o&&e.setAttribute("srsName",o),"LineString"===e.nodeName||"LineStringSegment"===e.nodeName?(o=createElementNS(e.namespaceURI,"posList"),e.appendChild(o),this.writePosList_(o,t,r)):"Curve"===e.nodeName&&(o=createElementNS(e.namespaceURI,"segments"),e.appendChild(o),this.writeCurveSegments_(o,t,r))},e.prototype.writeMultiSurfaceOrPolygon_=function(e,t,r){var o=r[r.length-1],i=o.hasZ,n=o.srsName,o=o.surface,t=(n&&e.setAttribute("srsName",n),t.getPolygons());pushSerializeAndPop({node:e,hasZ:i,srsName:n,surface:o},this.SURFACEORPOLYGONMEMBER_SERIALIZERS_,this.MULTIGEOMETRY_MEMBER_NODE_FACTORY_,t,r,void 0,this)},e.prototype.writeMultiPoint_=function(e,t,r){var o=r[r.length-1],i=o.srsName,o=o.hasZ,t=(i&&e.setAttribute("srsName",i),t.getPoints());pushSerializeAndPop({node:e,hasZ:o,srsName:i},this.POINTMEMBER_SERIALIZERS_,makeSimpleNodeFactory("pointMember"),t,r,void 0,this)},e.prototype.writeMultiCurveOrLineString_=function(e,t,r){var o=r[r.length-1],i=o.hasZ,n=o.srsName,o=o.curve,t=(n&&e.setAttribute("srsName",n),t.getLineStrings());pushSerializeAndPop({node:e,hasZ:i,srsName:n,curve:o},this.LINESTRINGORCURVEMEMBER_SERIALIZERS_,this.MULTIGEOMETRY_MEMBER_NODE_FACTORY_,t,r,void 0,this)},e.prototype.writeRing_=function(e,t,r){var o=createElementNS(e.namespaceURI,"LinearRing");e.appendChild(o),this.writeLinearRing_(o,t,r)},e.prototype.writeSurfaceOrPolygonMember_=function(e,t,r){var o=this.GEOMETRY_NODE_FACTORY_(t,r);o&&(e.appendChild(o),this.writeSurfaceOrPolygon_(o,t,r))},e.prototype.writePointMember_=function(e,t,r){var o=createElementNS(e.namespaceURI,"Point");e.appendChild(o),this.writePoint_(o,t,r)},e.prototype.writeLineStringOrCurveMember_=function(e,t,r){var o=this.GEOMETRY_NODE_FACTORY_(t,r);o&&(e.appendChild(o),this.writeCurveOrLineString_(o,t,r))},e.prototype.writeSurfacePatches_=function(e,t,r){var o=createElementNS(e.namespaceURI,"PolygonPatch");e.appendChild(o),this.writeSurfaceOrPolygon_(o,t,r)},e.prototype.writeCurveSegments_=function(e,t,r){var o=createElementNS(e.namespaceURI,"LineStringSegment");e.appendChild(o),this.writeCurveOrLineString_(o,t,r)},e.prototype.writeGeometryElement=function(e,t,r){var o=r[r.length-1],i=assign({},o);i.node=e,e=Array.isArray(t)?transformExtentWithOptions(t,o):transformGeometryWithOptions(t,!0,o),pushSerializeAndPop(i,this.GEOMETRY_SERIALIZERS_,this.GEOMETRY_NODE_FACTORY_,[e],r,void 0,this)},e.prototype.writeFeatureElement=function(e,t,r){var o,i=t.getId(),n=(i&&e.setAttribute("fid",i),r[r.length-1]),a=n.featureNS,p=t.getGeometryName(),s=(n.serializers||(n.serializers={},n.serializers[a]={}),t.getProperties()),u=[],l=[];for(o in s){var m=s[o];null!==m&&(u.push(o),l.push(m),o==p||"function"==typeof m.getSimplifiedGeometry?o in n.serializers[a]||(n.serializers[a][o]=makeChildAppender(this.writeGeometryElement,this)):o in n.serializers[a]||(n.serializers[a][o]=makeChildAppender(writeStringTextNode)))}i=assign({},n);i.node=e,pushSerializeAndPop(i,n.serializers,makeSimpleNodeFactory(void 0,a),l,r,u)},e.prototype.writeFeatureMembers_=function(e,t,r){var o=r[r.length-1],i=o.featureType,n=o.featureNS,a={},o=(a[n]={},a[n][i]=makeChildAppender(this.writeFeatureElement,this),assign({},o));o.node=e,pushSerializeAndPop(o,a,makeSimpleNodeFactory(i,n),t,r)},e.prototype.MULTIGEOMETRY_MEMBER_NODE_FACTORY_=function(e,t,r){t=t[t.length-1].node;return createElementNS(this.namespace,MULTIGEOMETRY_TO_MEMBER_NODENAME[t.nodeName])},e.prototype.GEOMETRY_NODE_FACTORY_=function(e,t,r){var o,t=t[t.length-1],i=t.multiSurface,n=t.surface,a=t.curve,t=t.multiCurve;return Array.isArray(e)?o="Envelope":"MultiPolygon"===(o=e.getType())&&!0===i?o="MultiSurface":"Polygon"===o&&!0===n?o="Surface":"LineString"===o&&!0===a?o="Curve":"MultiLineString"===o&&!0===t&&(o="MultiCurve"),createElementNS(this.namespace,o)},e.prototype.writeGeometryNode=function(e,t){t=this.adaptOptions(t);var r=createElementNS(this.namespace,"geom"),o={node:r,hasZ:this.hasZ,srsName:this.srsName,curve:this.curve_,surface:this.surface_,multiSurface:this.multiSurface_,multiCurve:this.multiCurve_};return t&&assign(o,t),this.writeGeometryElement(r,e,[o]),r},e.prototype.writeFeaturesNode=function(e,t){t=this.adaptOptions(t);var r=createElementNS(this.namespace,"featureMembers"),o=(r.setAttributeNS(XML_SCHEMA_INSTANCE_URI,"xsi:schemaLocation",this.schemaLocation),{srsName:this.srsName,hasZ:this.hasZ,curve:this.curve_,surface:this.surface_,multiSurface:this.multiSurface_,multiCurve:this.multiCurve_,featureNS:this.featureNS,featureType:this.featureType});return t&&assign(o,t),this.writeFeatureMembers_(r,e,[o]),r},e}(GMLBase);GML3.prototype.GEOMETRY_FLAT_COORDINATES_PARSERS={"http://www.opengis.net/gml":{pos:makeReplacer(GML3.prototype.readFlatPos_),posList:makeReplacer(GML3.prototype.readFlatPosList_)}},GML3.prototype.FLAT_LINEAR_RINGS_PARSERS={"http://www.opengis.net/gml":{interior:GML3.prototype.interiorParser_,exterior:GML3.prototype.exteriorParser_}},GML3.prototype.GEOMETRY_PARSERS={"http://www.opengis.net/gml":{Point:makeReplacer(GMLBase.prototype.readPoint),MultiPoint:makeReplacer(GMLBase.prototype.readMultiPoint),LineString:makeReplacer(GMLBase.prototype.readLineString),MultiLineString:makeReplacer(GMLBase.prototype.readMultiLineString),LinearRing:makeReplacer(GMLBase.prototype.readLinearRing),Polygon:makeReplacer(GMLBase.prototype.readPolygon),MultiPolygon:makeReplacer(GMLBase.prototype.readMultiPolygon),Surface:makeReplacer(GML3.prototype.readSurface_),MultiSurface:makeReplacer(GML3.prototype.readMultiSurface_),Curve:makeReplacer(GML3.prototype.readCurve_),MultiCurve:makeReplacer(GML3.prototype.readMultiCurve_),Envelope:makeReplacer(GML3.prototype.readEnvelope_)}},GML3.prototype.MULTICURVE_PARSERS_={"http://www.opengis.net/gml":{curveMember:makeArrayPusher(GML3.prototype.curveMemberParser_),curveMembers:makeArrayPusher(GML3.prototype.curveMemberParser_)}},GML3.prototype.MULTISURFACE_PARSERS_={"http://www.opengis.net/gml":{surfaceMember:makeArrayPusher(GML3.prototype.surfaceMemberParser_),surfaceMembers:makeArrayPusher(GML3.prototype.surfaceMemberParser_)}},GML3.prototype.CURVEMEMBER_PARSERS_={"http://www.opengis.net/gml":{LineString:makeArrayPusher(GMLBase.prototype.readLineString),Curve:makeArrayPusher(GML3.prototype.readCurve_)}},GML3.prototype.SURFACEMEMBER_PARSERS_={"http://www.opengis.net/gml":{Polygon:makeArrayPusher(GMLBase.prototype.readPolygon),Surface:makeArrayPusher(GML3.prototype.readSurface_)}},GML3.prototype.SURFACE_PARSERS_={"http://www.opengis.net/gml":{patches:makeReplacer(GML3.prototype.readPatch_)}},GML3.prototype.CURVE_PARSERS_={"http://www.opengis.net/gml":{segments:makeReplacer(GML3.prototype.readSegment_)}},GML3.prototype.ENVELOPE_PARSERS_={"http://www.opengis.net/gml":{lowerCorner:makeArrayPusher(GML3.prototype.readFlatPosList_),upperCorner:makeArrayPusher(GML3.prototype.readFlatPosList_)}},GML3.prototype.PATCHES_PARSERS_={"http://www.opengis.net/gml":{PolygonPatch:makeReplacer(GML3.prototype.readPolygonPatch_)}},GML3.prototype.SEGMENTS_PARSERS_={"http://www.opengis.net/gml":{LineStringSegment:makeReplacer(GML3.prototype.readLineStringSegment_)}},GML3.prototype.writeFeatures,GML3.prototype.RING_SERIALIZERS_={"http://www.opengis.net/gml":{exterior:makeChildAppender(GML3.prototype.writeRing_),interior:makeChildAppender(GML3.prototype.writeRing_)}},GML3.prototype.ENVELOPE_SERIALIZERS_={"http://www.opengis.net/gml":{lowerCorner:makeChildAppender(writeStringTextNode),upperCorner:makeChildAppender(writeStringTextNode)}},GML3.prototype.SURFACEORPOLYGONMEMBER_SERIALIZERS_={"http://www.opengis.net/gml":{surfaceMember:makeChildAppender(GML3.prototype.writeSurfaceOrPolygonMember_),polygonMember:makeChildAppender(GML3.prototype.writeSurfaceOrPolygonMember_)}},GML3.prototype.POINTMEMBER_SERIALIZERS_={"http://www.opengis.net/gml":{pointMember:makeChildAppender(GML3.prototype.writePointMember_)}},GML3.prototype.LINESTRINGORCURVEMEMBER_SERIALIZERS_={"http://www.opengis.net/gml":{lineStringMember:makeChildAppender(GML3.prototype.writeLineStringOrCurveMember_),curveMember:makeChildAppender(GML3.prototype.writeLineStringOrCurveMember_)}},GML3.prototype.GEOMETRY_SERIALIZERS_={"http://www.opengis.net/gml":{Curve:makeChildAppender(GML3.prototype.writeCurveOrLineString_),MultiCurve:makeChildAppender(GML3.prototype.writeMultiCurveOrLineString_),Point:makeChildAppender(GML3.prototype.writePoint_),MultiPoint:makeChildAppender(GML3.prototype.writeMultiPoint_),LineString:makeChildAppender(GML3.prototype.writeCurveOrLineString_),MultiLineString:makeChildAppender(GML3.prototype.writeMultiCurveOrLineString_),LinearRing:makeChildAppender(GML3.prototype.writeLinearRing_),Polygon:makeChildAppender(GML3.prototype.writeSurfaceOrPolygon_),MultiPolygon:makeChildAppender(GML3.prototype.writeMultiSurfaceOrPolygon_),Surface:makeChildAppender(GML3.prototype.writeSurfaceOrPolygon_),MultiSurface:makeChildAppender(GML3.prototype.writeMultiSurfaceOrPolygon_),Envelope:makeChildAppender(GML3.prototype.writeEnvelope)}};export default GML3;