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

//#region src/utils/validation/validateProject.ts
const defaultFieldsToCheck = [
	"name",
	"membersIds",
	"adminsIds",
	"organizationId"
];
const NAME_MIN_LENGTH = 4;
const NAME_MAX_LENGTH = 100;
const MEMBERS_MIN_LENGTH = 1;
/**
* Validates an project object.
* @param project The project object to validate.
* @returns An object containing the validation errors for each field.
*/
const validateProject = async (project, fieldsToCheck = defaultFieldsToCheck) => {
	const errors = {};
	const fieldsToValidate = new Set(fieldsToCheck);
	const projectJson = JSON.parse(JSON.stringify(project));
	for (const field of fieldsToValidate) {
		const value = projectJson[field];
		errors[field] = [];
		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 organization = await getOrganizationById(field);
			const organizationErrors = [];
			if (typeof value !== "string") organizationErrors.push("Organization id must be a string");
			if (!value) organizationErrors.push("Organization id is required");
			if (!organization) organizationErrors.push("Organization not found");
			if (organizationErrors.length > 0) errors[field] = organizationErrors;
		}
		if (field === "membersIds" || field === "adminsIds") if (!project.organizationId) errors[field] = ["Organization id is required to validate project members"];
		else {
			const organization = await getOrganizationById(project.organizationId);
			const membersErrors = validateArray(value, "Members", "string", (item) => (organization?.membersIds).includes(item), MEMBERS_MIN_LENGTH);
			if (membersErrors.length > 0) errors[field] = membersErrors;
		}
		if (errors[field].length === 0) delete errors[field];
	}
	return errors;
};

//#endregion
export { MEMBERS_MIN_LENGTH, NAME_MAX_LENGTH, NAME_MIN_LENGTH, validateProject };
//# sourceMappingURL=validateProject.mjs.map