import is from "@sindresorhus/is";
//#region src/lib/utilities/codemeta-helpers.ts
/**
* Helpers for building codemeta JSON-LD objects.
*
* Provides type-safe constructors for Person/Organization and SoftwareApplication
* dependency nodes, plus deduplication and license URL normalization.
*/
/**
* Build a codemeta JSON-LD Person or Organization from flexible inputs.
* Returns undefined if no identifying information (name, givenName+familyName, or email) is present.
*
* Works with person shapes from any metascope source — the caller maps
* source-specific field names into this common parameter object.
*/
function toPersonOrOrgLd(options) {
	const hasName = is.nonEmptyStringAndNotWhitespace(options.name);
	const hasGivenName = is.nonEmptyStringAndNotWhitespace(options.givenName);
	const hasFamilyName = is.nonEmptyStringAndNotWhitespace(options.familyName);
	const hasEmail = is.nonEmptyStringAndNotWhitespace(options.email);
	if (!hasName && !hasGivenName && !hasFamilyName && !hasEmail) return void 0;
	const person = { "@type": options.type ?? "Person" };
	if (is.nonEmptyStringAndNotWhitespace(options.id)) person["@id"] = options.id;
	if (hasName) person.name = options.name;
	if (hasGivenName) person.givenName = options.givenName;
	if (hasFamilyName) person.familyName = options.familyName;
	if (hasEmail) person.email = options.email;
	if (is.nonEmptyStringAndNotWhitespace(options.url)) person.url = options.url;
	if (is.nonEmptyStringAndNotWhitespace(options.affiliation)) person.affiliation = {
		"@type": "Organization",
		name: options.affiliation
	};
	return person;
}
/**
* Deduplicate persons by name (case-insensitive, trimmed).
* Keeps the first occurrence, so callers should place higher-priority sources first.
* Returns undefined if the result is empty.
*/
function deduplicatePersonsOrOrgs(persons) {
	const seen = /* @__PURE__ */ new Map();
	for (const person of persons) {
		const key = (person.name ?? ([person.givenName, person.familyName].filter(Boolean).join(" ") || void 0) ?? person.email ?? "").toLowerCase().trim();
		if (key.length > 0 && !seen.has(key)) seen.set(key, person);
	}
	const result = [...seen.values()];
	return result.length > 0 ? result : void 0;
}
/**
* Build a codemeta JSON-LD SoftwareApplication dependency node.
*/
function toDependencyLd(name, version, identifier, runtimePlatform) {
	const dep = {
		"@type": "SoftwareApplication",
		name
	};
	if (is.nonEmptyStringAndNotWhitespace(version)) dep.version = version;
	if (is.nonEmptyStringAndNotWhitespace(identifier)) dep.identifier = identifier;
	if (is.nonEmptyStringAndNotWhitespace(runtimePlatform)) dep.runtimePlatform = runtimePlatform;
	return dep;
}
/**
* Deduplicate dependencies by name (case-insensitive).
* Keeps the first occurrence. Returns undefined if the result is empty.
*/
function deduplicateDependencies(deps) {
	const seen = /* @__PURE__ */ new Map();
	for (const dep of deps) {
		const key = dep.name.toLowerCase().trim();
		if (key.length > 0 && !seen.has(key)) seen.set(key, dep);
	}
	const result = [...seen.values()];
	return result.length > 0 ? result : void 0;
}
/**
* Normalize a license identifier to an SPDX URL.
* Handles bare SPDX IDs ("MIT") and existing SPDX URLs.
*/
function toSpdxLicenseUrl(spdxId) {
	return `https://spdx.org/licenses/${spdxId.replace("https://spdx.org/licenses/", "").replace("http://spdx.org/licenses/", "")}`;
}
//#endregion
export { deduplicateDependencies, deduplicatePersonsOrOrgs, toDependencyLd, toPersonOrOrgLd, toSpdxLicenseUrl };
