'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _index = require('../index.js');

var _index2 = _interopRequireDefault(_index);

var _xlink = require('../format/xlink.js');

var _xlink2 = _interopRequireDefault(_xlink);

var _xml = require('../format/xml.js');

var _xml2 = _interopRequireDefault(_xml);

var _xsd = require('../format/xsd.js');

var _xsd2 = _interopRequireDefault(_xsd);

var _xml3 = require('../xml.js');

var _xml4 = _interopRequireDefault(_xml3);

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : { default: obj };
}

/**
 * @constructor
 * @extends {ol.format.XML}
 */
var _ol_format_OWS_ = function () {
    _xml2.default.call(this);
};

_index2.default.inherits(_ol_format_OWS_, _xml2.default);

/**
 * @inheritDoc
 */
_ol_format_OWS_.prototype.readFromDocument = function (doc) {
    for (var n = doc.firstChild; n; n = n.nextSibling) {
        if (n.nodeType == Node.ELEMENT_NODE) {
            return this.readFromNode(n);
        }
    }
    return null;
};

/**
 * @inheritDoc
 */
_ol_format_OWS_.prototype.readFromNode = function (node) {
    var owsObject = _xml4.default.pushParseAndPop({}, _ol_format_OWS_.PARSERS_, node, []);
    return owsObject ? owsObject : null;
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @private
 * @return {Object|undefined} The address.
 */
_ol_format_OWS_.readAddress_ = function (node, objectStack) {
    return _xml4.default.pushParseAndPop({}, _ol_format_OWS_.ADDRESS_PARSERS_, node, objectStack);
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @private
 * @return {Object|undefined} The values.
 */
_ol_format_OWS_.readAllowedValues_ = function (node, objectStack) {
    return _xml4.default.pushParseAndPop({}, _ol_format_OWS_.ALLOWED_VALUES_PARSERS_, node, objectStack);
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @private
 * @return {Object|undefined} The constraint.
 */
_ol_format_OWS_.readConstraint_ = function (node, objectStack) {
    var name = node.getAttribute('name');
    if (!name) {
        return undefined;
    }
    return _xml4.default.pushParseAndPop({ 'name': name }, _ol_format_OWS_.CONSTRAINT_PARSERS_, node, objectStack);
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @private
 * @return {Object|undefined} The contact info.
 */
_ol_format_OWS_.readContactInfo_ = function (node, objectStack) {
    return _xml4.default.pushParseAndPop({}, _ol_format_OWS_.CONTACT_INFO_PARSERS_, node, objectStack);
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @private
 * @return {Object|undefined} The DCP.
 */
_ol_format_OWS_.readDcp_ = function (node, objectStack) {
    return _xml4.default.pushParseAndPop({}, _ol_format_OWS_.DCP_PARSERS_, node, objectStack);
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @private
 * @return {Object|undefined} The GET object.
 */
_ol_format_OWS_.readGet_ = function (node, objectStack) {
    var href = _xlink2.default.readHref(node);
    if (!href) {
        return undefined;
    }
    return _xml4.default.pushParseAndPop({ 'href': href }, _ol_format_OWS_.REQUEST_METHOD_PARSERS_, node, objectStack);
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @private
 * @return {Object|undefined} The HTTP object.
 */
_ol_format_OWS_.readHttp_ = function (node, objectStack) {
    return _xml4.default.pushParseAndPop({}, _ol_format_OWS_.HTTP_PARSERS_, node, objectStack);
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @private
 * @return {Object|undefined} The operation.
 */
_ol_format_OWS_.readOperation_ = function (node, objectStack) {
    var name = node.getAttribute('name');
    var value = _xml4.default.pushParseAndPop({}, _ol_format_OWS_.OPERATION_PARSERS_, node, objectStack);
    if (!value) {
        return undefined;
    }
    var object = /** @type {Object} */
    objectStack[objectStack.length - 1];
    object[name] = value;
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @private
 * @return {Object|undefined} The operations metadata.
 */
_ol_format_OWS_.readOperationsMetadata_ = function (node, objectStack) {
    return _xml4.default.pushParseAndPop({}, _ol_format_OWS_.OPERATIONS_METADATA_PARSERS_, node, objectStack);
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @private
 * @return {Object|undefined} The phone.
 */
_ol_format_OWS_.readPhone_ = function (node, objectStack) {
    return _xml4.default.pushParseAndPop({}, _ol_format_OWS_.PHONE_PARSERS_, node, objectStack);
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @private
 * @return {Object|undefined} The service identification.
 */
_ol_format_OWS_.readServiceIdentification_ = function (node, objectStack) {
    return _xml4.default.pushParseAndPop({}, _ol_format_OWS_.SERVICE_IDENTIFICATION_PARSERS_, node, objectStack);
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @private
 * @return {Object|undefined} The service contact.
 */
_ol_format_OWS_.readServiceContact_ = function (node, objectStack) {
    return _xml4.default.pushParseAndPop({}, _ol_format_OWS_.SERVICE_CONTACT_PARSERS_, node, objectStack);
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @private
 * @return {Object|undefined} The service provider.
 */
_ol_format_OWS_.readServiceProvider_ = function (node, objectStack) {
    return _xml4.default.pushParseAndPop({}, _ol_format_OWS_.SERVICE_PROVIDER_PARSERS_, node, objectStack);
};

/**
 * @param {Node} node Node.
 * @param {Array.<*>} objectStack Object stack.
 * @private
 * @return {string|undefined} The value.
 */
_ol_format_OWS_.readValue_ = function (node, objectStack) {
    return _xsd2.default.readString(node);
};

/**
 * @const
 * @type {Array.<string>}
 * @private
 */
_ol_format_OWS_.NAMESPACE_URIS_ = [null, 'http://www.opengis.net/ows/1.1'];

/**
 * @const
 * @type {Object.<string, Object.<string, ol.XmlParser>>}
 * @private
 */
_ol_format_OWS_.PARSERS_ = _xml4.default.makeStructureNS(_ol_format_OWS_.NAMESPACE_URIS_, {
    'ServiceIdentification': _xml4.default.makeObjectPropertySetter(_ol_format_OWS_.readServiceIdentification_),
    'ServiceProvider': _xml4.default.makeObjectPropertySetter(_ol_format_OWS_.readServiceProvider_),
    'OperationsMetadata': _xml4.default.makeObjectPropertySetter(_ol_format_OWS_.readOperationsMetadata_)
});

/**
 * @const
 * @type {Object.<string, Object.<string, ol.XmlParser>>}
 * @private
 */
_ol_format_OWS_.ADDRESS_PARSERS_ = _xml4.default.makeStructureNS(_ol_format_OWS_.NAMESPACE_URIS_, {
    'DeliveryPoint': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString),
    'City': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString),
    'AdministrativeArea': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString),
    'PostalCode': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString),
    'Country': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString),
    'ElectronicMailAddress': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString)
});

/**
 * @const
 * @type {Object.<string, Object.<string, ol.XmlParser>>}
 * @private
 */
_ol_format_OWS_.ALLOWED_VALUES_PARSERS_ = _xml4.default.makeStructureNS(_ol_format_OWS_.NAMESPACE_URIS_, {
    'Value': _xml4.default.makeObjectPropertyPusher(_ol_format_OWS_.readValue_)
});

/**
 * @const
 * @type {Object.<string, Object.<string, ol.XmlParser>>}
 * @private
 */
_ol_format_OWS_.CONSTRAINT_PARSERS_ = _xml4.default.makeStructureNS(_ol_format_OWS_.NAMESPACE_URIS_, {
    'AllowedValues': _xml4.default.makeObjectPropertySetter(_ol_format_OWS_.readAllowedValues_)
});

/**
 * @const
 * @type {Object.<string, Object.<string, ol.XmlParser>>}
 * @private
 */
_ol_format_OWS_.CONTACT_INFO_PARSERS_ = _xml4.default.makeStructureNS(_ol_format_OWS_.NAMESPACE_URIS_, {
    'Phone': _xml4.default.makeObjectPropertySetter(_ol_format_OWS_.readPhone_),
    'Address': _xml4.default.makeObjectPropertySetter(_ol_format_OWS_.readAddress_)
});

/**
 * @const
 * @type {Object.<string, Object.<string, ol.XmlParser>>}
 * @private
 */
_ol_format_OWS_.DCP_PARSERS_ = _xml4.default.makeStructureNS(_ol_format_OWS_.NAMESPACE_URIS_, {
    'HTTP': _xml4.default.makeObjectPropertySetter(_ol_format_OWS_.readHttp_)
});

/**
 * @const
 * @type {Object.<string, Object.<string, ol.XmlParser>>}
 * @private
 */
_ol_format_OWS_.HTTP_PARSERS_ = _xml4.default.makeStructureNS(_ol_format_OWS_.NAMESPACE_URIS_, {
    'Get': _xml4.default.makeObjectPropertyPusher(_ol_format_OWS_.readGet_),
    'Post': undefined // TODO
});

/**
 * @const
 * @type {Object.<string, Object.<string, ol.XmlParser>>}
 * @private
 */
_ol_format_OWS_.OPERATION_PARSERS_ = _xml4.default.makeStructureNS(_ol_format_OWS_.NAMESPACE_URIS_, {
    'DCP': _xml4.default.makeObjectPropertySetter(_ol_format_OWS_.readDcp_)
});

/**
 * @const
 * @type {Object.<string, Object.<string, ol.XmlParser>>}
 * @private
 */
_ol_format_OWS_.OPERATIONS_METADATA_PARSERS_ = _xml4.default.makeStructureNS(_ol_format_OWS_.NAMESPACE_URIS_, {
    'Operation': _ol_format_OWS_.readOperation_
});

/**
 * @const
 * @type {Object.<string, Object.<string, ol.XmlParser>>}
 * @private
 */
_ol_format_OWS_.PHONE_PARSERS_ = _xml4.default.makeStructureNS(_ol_format_OWS_.NAMESPACE_URIS_, {
    'Voice': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString),
    'Facsimile': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString)
});

/**
 * @const
 * @type {Object.<string, Object.<string, ol.XmlParser>>}
 * @private
 */
_ol_format_OWS_.REQUEST_METHOD_PARSERS_ = _xml4.default.makeStructureNS(_ol_format_OWS_.NAMESPACE_URIS_, {
    'Constraint': _xml4.default.makeObjectPropertyPusher(_ol_format_OWS_.readConstraint_)
});

/**
 * @const
 * @type {Object.<string, Object.<string, ol.XmlParser>>}
 * @private
 */
_ol_format_OWS_.SERVICE_CONTACT_PARSERS_ = _xml4.default.makeStructureNS(_ol_format_OWS_.NAMESPACE_URIS_, {
    'IndividualName': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString),
    'PositionName': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString),
    'ContactInfo': _xml4.default.makeObjectPropertySetter(_ol_format_OWS_.readContactInfo_)
});

/**
 * @const
 * @type {Object.<string, Object.<string, ol.XmlParser>>}
 * @private
 */
_ol_format_OWS_.SERVICE_IDENTIFICATION_PARSERS_ = _xml4.default.makeStructureNS(_ol_format_OWS_.NAMESPACE_URIS_, {
    'Abstract': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString),
    'AccessConstraints': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString),
    'Fees': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString),
    'Title': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString),
    'ServiceTypeVersion': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString),
    'ServiceType': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString)
});

/**
 * @const
 * @type {Object.<string, Object.<string, ol.XmlParser>>}
 * @private
 */
_ol_format_OWS_.SERVICE_PROVIDER_PARSERS_ = _xml4.default.makeStructureNS(_ol_format_OWS_.NAMESPACE_URIS_, {
    'ProviderName': _xml4.default.makeObjectPropertySetter(_xsd2.default.readString),
    'ProviderSite': _xml4.default.makeObjectPropertySetter(_xlink2.default.readHref),
    'ServiceContact': _xml4.default.makeObjectPropertySetter(_ol_format_OWS_.readServiceContact_)
});
exports.default = _ol_format_OWS_;
module.exports = exports['default'];