"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonSchemaValidator = void 0;
const utils_1 = require("@autobe/utils");
const Escaper_1 = require("typia/lib/utils/Escaper");
var JsonSchemaValidator;
(function (JsonSchemaValidator) {
    JsonSchemaValidator.validateSchemas = (props) => {
        authorization(props);
        for (const key of Object.keys(props.schemas))
            JsonSchemaValidator.validateKey({
                errors: props.errors,
                path: props.path,
                key,
            });
    };
    JsonSchemaValidator.validateKey = (props) => {
        const elements = props.key.split(".");
        if (elements.every(Escaper_1.Escaper.variable) === false)
            props.errors.push({
                path: `${props.path}[${JSON.stringify(props.key)}]`,
                expected: "Valid variable name",
                value: props.key,
                description: utils_1.StringUtil.trim `
          JSON schema type name must be a valid variable name.

          Even though JSON schema type name allows dot(.) character, but
          each segment separated by dot(.) must be a valid variable name.

          Current key name ${JSON.stringify(props.key)} is not valid. Change
          it to a valid variable name at the next time.
        `,
            });
        if (props.key === "IPageIRequest")
            props.errors.push({
                path: `${props.path}[${JSON.stringify(props.key)}]`,
                expected: `"IPageIRequest" is a mistake. Use "IPage.IRequest" instead.`,
                value: props.key,
                description: utils_1.StringUtil.trim `
          You've taken a mistake that defines "IPageIRequest" as a type name.
          However, as you've intended to define a pagination request type, 
          the correct type name is "IPage.IRequest" instead of "IPageIRequest".

          Change it to "IPage.IRequest" at the next time.
        `,
            });
        else if (props.key.startsWith("IPage") &&
            props.key.startsWith("IPageI") === false) {
            const expected = `IPage${props.key
                .substring(5)
                .split(".")
                .map((s) => (s.startsWith("I") ? s : `I${s}`))
                .join(".")}`;
            props.errors.push({
                path: `${props.path}[${JSON.stringify(props.key)}]`,
                expected: `Interface name starting with 'I' even after 'IPage': ${JSON.stringify(expected)}`,
                value: props.key,
                description: utils_1.StringUtil.trim `
          JSON schema type name must be an interface name starting with 'I'.
          Even though JSON schema type name allows dot(.) character, but
          each segment separated by dot(.) must be an interface name starting
          with 'I'.

          Even in the case of pagination response, after 'IPage' prefix,
          the remaining part must be an interface name starting with 'I'.
          
          Current key name ${JSON.stringify(props.key)} is not valid. Change
          it to a valid interface name to be  ${JSON.stringify(expected)},
          or change it to another valid interface name at the next time.
        `,
            });
        }
        else if (elements.some((s) => s.startsWith("I") === false) === true) {
            const expected = elements
                .map((s) => (s.startsWith("I") ? s : `I${s}`))
                .join(".");
            props.errors.push({
                path: `${props.path}[${JSON.stringify(props.key)}]`,
                expected: `Interface name starting with 'I': ${JSON.stringify(expected)}`,
                value: props.key,
                description: utils_1.StringUtil.trim `
          JSON schema type name must be an interface name starting with 'I'.
          Even though JSON schema type name allows dot(.) character, but
          each segment separated by dot(.) must be an interface name starting
          with 'I'.

          Current key name ${JSON.stringify(props.key)} is not valid. Change
          it to a valid interface name to be ${JSON.stringify(expected)}, 
          or change it to another valid interface name at the next time.
        `,
            });
        }
    };
    const authorization = (props) => {
        var _a, _b;
        for (const [key, value] of Object.entries(props.schemas)) {
            if (!key.endsWith(".IAuthorized"))
                continue;
            else if (utils_1.AutoBeOpenApiTypeChecker.isObject(value) === false) {
                props.errors.push({
                    path: `${props.path}.${key}`,
                    expected: `AutoBeOpenApi.IJsonSchemaDescriptive<AutoBeOpenApi.IJsonSchema.IObject>`,
                    value: value,
                    description: `${key} must be an object type for authorization responses`,
                });
                continue;
            }
            // Check if token property exists
            (_a = value.properties) !== null && _a !== void 0 ? _a : (value.properties = {});
            value.properties["token"] = {
                $ref: "#/components/schemas/IAuthorizationToken",
                description: "JWT token information for authentication",
            };
            (_b = value.required) !== null && _b !== void 0 ? _b : (value.required = []);
            if (value.required.includes("token") === false)
                value.required.push("token");
        }
    };
})(JsonSchemaValidator || (exports.JsonSchemaValidator = JsonSchemaValidator = {}));
//# sourceMappingURL=JsonSchemaValidator.js.map