var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { BaseEntity, ValidationResult } from "@memberjunction/core";
import { MJTemplateEntity } from "../generated/entity_subclasses.js";
import { RegisterClass, UUIDsEqual } from "@memberjunction/global";
let MJTemplateEntityExtended = class MJTemplateEntityExtended extends MJTemplateEntity {
    constructor() {
        super(...arguments);
        this._Content = [];
        this._Params = [];
    }
    get Content() {
        return this._Content;
    }
    set Content(value) {
        this._Content = value;
    }
    get Params() {
        return this._Params;
    }
    set Params(value) {
        this._Params = value;
    }
    /**
     * Returns all content for a given type for the template
     * @param type
     * @returns
     */
    GetContentByType(type) {
        return this.Content.filter(c => c.Type.trim().toLowerCase() === type.trim().toLowerCase());
    }
    /**
     * Returns the highest priority content for the template
     * @param type If provided, returns the highest priority content of the specified type
     * @returns
     */
    GetHighestPriorityContent(type) {
        if (type) {
            return this.Content.filter(c => c.Type.trim().toLowerCase() === type.trim().toLowerCase())
                .sort((a, b) => a.Priority - b.Priority)[0];
        }
        else {
            return this.Content.sort((a, b) => a.Priority - b.Priority)[0];
        }
    }
    /**
     * Returns all parameters that apply to a specific template content.
     * This includes both global parameters (where TemplateContentID is NULL)
     * and content-specific parameters for the given contentId.
     *
     * @param contentId - The ID of the template content. If not provided, returns only global parameters.
     * @returns Array of MJTemplateParamEntity objects that apply to the specified content
     *
     * @example
     * // Get all parameters for a specific content
     * const params = template.GetParametersForContent('content-uuid');
     *
     * @example
     * // Get only global parameters (that apply to all contents)
     * const globalParams = template.GetParametersForContent();
     */
    GetParametersForContent(contentId) {
        if (!contentId) {
            // Return only global parameters (TemplateContentID is null)
            return this.Params.filter(p => !p.TemplateContentID);
        }
        // Return both global parameters and content-specific parameters
        return this.Params.filter(p => !p.TemplateContentID || // Global param (applies to all contents)
            UUIDsEqual(p.TemplateContentID, contentId) // Content-specific param
        );
    }
    /**
     * This method is different from the Validate() method which validates the state of the Template itself.
     * This method validates the data object provided meets the requirements for the template's parameter definitions.
     *
     * @param data - the data object to validate against the template's parameter definitions
     * @param contentId - Optional: The ID of the template content to validate against.
     *                    If provided, validates against parameters specific to that content.
     *                    If not provided, validates against all parameters.
     * @returns ValidationResult with success status and any validation errors
     *
     * @example
     * // Validate against all parameters
     * const result = template.ValidateTemplateInput(inputData);
     *
     * @example
     * // Validate against parameters for a specific content
     * const result = template.ValidateTemplateInput(inputData, 'content-uuid');
     */
    ValidateTemplateInput(data, contentId) {
        const result = new ValidationResult();
        // Get the relevant parameters based on contentId
        const paramsToValidate = contentId ?
            this.GetParametersForContent(contentId) :
            this.Params;
        paramsToValidate.forEach((p) => {
            if (p.IsRequired) {
                if (!data ||
                    data[p.Name] === undefined ||
                    data[p.Name] === null ||
                    (typeof data[p.Name] === 'string' && data[p.Name].toString().trim() === ''))
                    result.Errors.push({
                        Source: p.Name,
                        Message: `Parameter ${p.Name} is required.`,
                        Value: data[p.Name],
                        Type: 'Failure'
                    });
            }
        });
        // now set result's top level success flag based on the existence of ANY failure record within the errors collection
        result.Success = result.Errors.some(e => e.Type === 'Failure') ? false : true;
        return result;
    }
};
MJTemplateEntityExtended = __decorate([
    RegisterClass(BaseEntity, 'MJ: Templates')
], MJTemplateEntityExtended);
export { MJTemplateEntityExtended };
//# sourceMappingURL=MJTemplateEntityExtended.js.map