const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.cjs');
const require_utility$1 = require('./utility.cjs');
const require_xpath$1 = require('../../../xpath/xpath.cjs');
const require_api$1 = require('./api.cjs');
const require_index = require('../../../camelcase/index.cjs');

//#region ../../node_modules/samlify/build/src/extractor.js
var require_extractor = /* @__PURE__ */ require_rolldown_runtime.__commonJS({ "../../node_modules/samlify/build/src/extractor.js": ((exports) => {
	var __assign = exports && exports.__assign || function() {
		__assign = Object.assign || function(t) {
			for (var s, i = 1, n = arguments.length; i < n; i++) {
				s = arguments[i];
				for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
			}
			return t;
		};
		return __assign.apply(this, arguments);
	};
	var __importDefault = exports && exports.__importDefault || function(mod) {
		return mod && mod.__esModule ? mod : { "default": mod };
	};
	Object.defineProperty(exports, "__esModule", { value: true });
	exports.extract = exports.logoutResponseFields = exports.logoutRequestFields = exports.loginResponseFields = exports.logoutResponseStatusFields = exports.loginResponseStatusFields = exports.loginRequestFields = void 0;
	var xpath_1 = require_xpath$1.default;
	var utility_1 = require_utility$1.default;
	var api_1 = require_api$1.default;
	var camelcase_1 = __importDefault(require_index.default);
	function buildAbsoluteXPath(paths) {
		return paths.reduce(function(currentPath, name) {
			var appendedPath = currentPath;
			var isWildcard = name.startsWith("~");
			if (isWildcard) {
				var pathName = name.replace("~", "");
				appendedPath = currentPath + "/*[contains(local-name(), '".concat(pathName, "')]");
			}
			if (!isWildcard) appendedPath = currentPath + "/*[local-name(.)='".concat(name, "']");
			return appendedPath;
		}, "");
	}
	function buildAttributeXPath(attributes) {
		if (attributes.length === 0) return "/text()";
		if (attributes.length === 1) return "/@".concat(attributes[0]);
		var filters = attributes.map(function(attribute) {
			return "name()='".concat(attribute, "'");
		}).join(" or ");
		return "/@*[".concat(filters, "]");
	}
	exports.loginRequestFields = [
		{
			key: "request",
			localPath: ["AuthnRequest"],
			attributes: [
				"ID",
				"IssueInstant",
				"Destination",
				"AssertionConsumerServiceURL"
			]
		},
		{
			key: "issuer",
			localPath: ["AuthnRequest", "Issuer"],
			attributes: []
		},
		{
			key: "nameIDPolicy",
			localPath: ["AuthnRequest", "NameIDPolicy"],
			attributes: ["Format", "AllowCreate"]
		},
		{
			key: "authnContextClassRef",
			localPath: ["AuthnRequest", "AuthnContextClassRef"],
			attributes: []
		},
		{
			key: "signature",
			localPath: ["AuthnRequest", "Signature"],
			attributes: [],
			context: true
		}
	];
	exports.loginResponseStatusFields = [{
		key: "top",
		localPath: [
			"Response",
			"Status",
			"StatusCode"
		],
		attributes: ["Value"]
	}, {
		key: "second",
		localPath: [
			"Response",
			"Status",
			"StatusCode",
			"StatusCode"
		],
		attributes: ["Value"]
	}];
	exports.logoutResponseStatusFields = [{
		key: "top",
		localPath: [
			"LogoutResponse",
			"Status",
			"StatusCode"
		],
		attributes: ["Value"]
	}, {
		key: "second",
		localPath: [
			"LogoutResponse",
			"Status",
			"StatusCode",
			"StatusCode"
		],
		attributes: ["Value"]
	}];
	var loginResponseFields = function(assertion) {
		return [
			{
				key: "conditions",
				localPath: ["Assertion", "Conditions"],
				attributes: ["NotBefore", "NotOnOrAfter"],
				shortcut: assertion
			},
			{
				key: "response",
				localPath: ["Response"],
				attributes: [
					"ID",
					"IssueInstant",
					"Destination",
					"InResponseTo"
				]
			},
			{
				key: "audience",
				localPath: [
					"Assertion",
					"Conditions",
					"AudienceRestriction",
					"Audience"
				],
				attributes: [],
				shortcut: assertion
			},
			{
				key: "issuer",
				localPath: ["Assertion", "Issuer"],
				attributes: [],
				shortcut: assertion
			},
			{
				key: "nameID",
				localPath: [
					"Assertion",
					"Subject",
					"NameID"
				],
				attributes: [],
				shortcut: assertion
			},
			{
				key: "sessionIndex",
				localPath: ["Assertion", "AuthnStatement"],
				attributes: [
					"AuthnInstant",
					"SessionNotOnOrAfter",
					"SessionIndex"
				],
				shortcut: assertion
			},
			{
				key: "attributes",
				localPath: [
					"Assertion",
					"AttributeStatement",
					"Attribute"
				],
				index: ["Name"],
				attributePath: ["AttributeValue"],
				attributes: [],
				shortcut: assertion
			}
		];
	};
	exports.loginResponseFields = loginResponseFields;
	exports.logoutRequestFields = [
		{
			key: "request",
			localPath: ["LogoutRequest"],
			attributes: [
				"ID",
				"IssueInstant",
				"Destination"
			]
		},
		{
			key: "issuer",
			localPath: ["LogoutRequest", "Issuer"],
			attributes: []
		},
		{
			key: "nameID",
			localPath: ["LogoutRequest", "NameID"],
			attributes: []
		},
		{
			key: "sessionIndex",
			localPath: ["LogoutRequest", "SessionIndex"],
			attributes: []
		},
		{
			key: "signature",
			localPath: ["LogoutRequest", "Signature"],
			attributes: [],
			context: true
		}
	];
	exports.logoutResponseFields = [
		{
			key: "response",
			localPath: ["LogoutResponse"],
			attributes: [
				"ID",
				"Destination",
				"InResponseTo"
			]
		},
		{
			key: "issuer",
			localPath: ["LogoutResponse", "Issuer"],
			attributes: []
		},
		{
			key: "signature",
			localPath: ["LogoutResponse", "Signature"],
			attributes: [],
			context: true
		}
	];
	function extract(context, fields) {
		var dom = (0, api_1.getContext)().dom;
		var rootDoc = dom.parseFromString(context);
		return fields.reduce(function(result, field) {
			var _a, _b, _c, _d, _e, _f;
			var key = field.key;
			var localPath = field.localPath;
			var attributes = field.attributes;
			var isEntire = field.context;
			var shortcut = field.shortcut;
			var index = field.index;
			var attributePath = field.attributePath;
			var targetDoc = rootDoc;
			if (shortcut) targetDoc = dom.parseFromString(shortcut);
			if (localPath.every(function(path) {
				return Array.isArray(path);
			})) {
				var multiXPaths = localPath.map(function(path) {
					return "".concat(buildAbsoluteXPath(path), "/text()");
				}).join(" | ");
				return __assign(__assign({}, result), (_a = {}, _a[key] = (0, utility_1.uniq)((0, xpath_1.select)(multiXPaths, targetDoc).map(function(n) {
					return n.nodeValue;
				}).filter(utility_1.notEmpty)), _a));
			}
			var baseXPath = buildAbsoluteXPath(localPath);
			var attributeXPath = buildAttributeXPath(attributes);
			if (index && attributePath) {
				var indexPath = buildAttributeXPath(index);
				var fullLocalXPath = "".concat(baseXPath).concat(indexPath);
				var parentNodes = (0, xpath_1.select)(baseXPath, targetDoc);
				var parentAttributes = (0, xpath_1.select)(fullLocalXPath, targetDoc).map(function(n) {
					return n.value;
				});
				var childXPath = buildAbsoluteXPath([(0, utility_1.last)(localPath)].concat(attributePath));
				var childAttributeXPath = buildAttributeXPath(attributes);
				var fullChildXPath_1 = "".concat(childXPath).concat(childAttributeXPath);
				var childAttributes = parentNodes.map(function(node$1) {
					var nodeDoc = dom.parseFromString(node$1.toString());
					if (attributes.length === 0) {
						var childValues = (0, xpath_1.select)(fullChildXPath_1, nodeDoc).map(function(n) {
							return n.nodeValue;
						});
						if (childValues.length === 1) return childValues[0];
						return childValues;
					}
					if (attributes.length > 0) {
						var childValues = (0, xpath_1.select)(fullChildXPath_1, nodeDoc).map(function(n) {
							return n.value;
						});
						if (childValues.length === 1) return childValues[0];
						return childValues;
					}
					return null;
				});
				var obj = (0, utility_1.zipObject)(parentAttributes, childAttributes, false);
				return __assign(__assign({}, result), (_b = {}, _b[key] = obj, _b));
			}
			if (isEntire) {
				var node = (0, xpath_1.select)(baseXPath, targetDoc);
				var value = null;
				if (node.length === 1) value = node[0].toString();
				if (node.length > 1) value = node.map(function(n) {
					return n.toString();
				});
				return __assign(__assign({}, result), (_c = {}, _c[key] = value, _c));
			}
			if (attributes.length > 1) {
				var baseNode = (0, xpath_1.select)(baseXPath, targetDoc).map(function(n) {
					return n.toString();
				});
				var childXPath_1 = "".concat(buildAbsoluteXPath([(0, utility_1.last)(localPath)])).concat(attributeXPath);
				var attributeValues = baseNode.map(function(node$1) {
					var nodeDoc = dom.parseFromString(node$1);
					return (0, xpath_1.select)(childXPath_1, nodeDoc).reduce(function(r, n) {
						r[(0, camelcase_1.default)(n.name, { locale: "en-us" })] = n.value;
						return r;
					}, {});
				});
				return __assign(__assign({}, result), (_d = {}, _d[key] = attributeValues.length === 1 ? attributeValues[0] : attributeValues, _d));
			}
			if (attributes.length === 1) {
				var fullPath = "".concat(baseXPath).concat(attributeXPath);
				var attributeValues = (0, xpath_1.select)(fullPath, targetDoc).map(function(n) {
					return n.value;
				});
				return __assign(__assign({}, result), (_e = {}, _e[key] = attributeValues[0], _e));
			}
			if (attributes.length === 0) {
				var attributeValue = null;
				var node = (0, xpath_1.select)(baseXPath, targetDoc);
				if (node.length === 1) {
					var fullPath = "string(".concat(baseXPath).concat(attributeXPath, ")");
					attributeValue = (0, xpath_1.select)(fullPath, targetDoc);
				}
				if (node.length > 1) attributeValue = node.filter(function(n) {
					return n.firstChild;
				}).map(function(n) {
					return n.firstChild.nodeValue;
				});
				return __assign(__assign({}, result), (_f = {}, _f[key] = attributeValue, _f));
			}
			return result;
		}, {});
	}
	exports.extract = extract;
}) });

//#endregion
Object.defineProperty(exports, 'default', {
  enumerable: true,
  get: function () {
    return require_extractor();
  }
});
//# sourceMappingURL=extractor.cjs.map