import { validateString } from "./validateString.mjs";
import { getOrganizationById } from "../../services/organization.service.mjs";

//#region src/utils/validation/validateTag.ts
const defaultFieldsToCheck = ["name"];
const KEY_MIN_LENGTH = 4;
const KEY_MAX_LENGTH = 20;
const NAME_MIN_LENGTH = 4;
const NAME_MAX_LENGTH = 50;
/**
* Validates an tag object.
* @param tag The tag object to validate.
* @returns An object containing the validation errors for each field.
*/
const validateTag = async (tag, fieldsToCheck = defaultFieldsToCheck) => {
	const errors = {};
	const fieldsToValidate = new Set(fieldsToCheck);
	for (const field of fieldsToValidate) {
		const value = tag[field];
		errors[field] = [];
		if (field === "key") {
			const nameErrors = validateString(value, "Key", KEY_MIN_LENGTH, KEY_MAX_LENGTH);
			if (nameErrors.length > 0) errors[field] = nameErrors;
		}
		if (field === "name") {
			const nameErrors = validateString(value, "Name", NAME_MIN_LENGTH, NAME_MAX_LENGTH);
			if (nameErrors.length > 0) errors[field] = nameErrors;
		}
		if (field === "organizationId") {
			const organizationErrors = [];
			if (typeof value !== "string") organizationErrors.push("Organization id must be a string");
			if (!value) organizationErrors.push("Organization id is required");
			if (!await getOrganizationById(field)) organizationErrors.push("Organization not found");
			if (organizationErrors.length > 0) errors[field] = organizationErrors;
		}
		if (errors[field].length === 0) delete errors[field];
	}
	return errors;
};

//#endregion
export { KEY_MAX_LENGTH, KEY_MIN_LENGTH, NAME_MAX_LENGTH, NAME_MIN_LENGTH, validateTag };
//# sourceMappingURL=validateTag.mjs.map