"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.rules = void 0;
const url_1 = require("url");
const lodash_omit_1 = __importDefault(require("lodash.omit"));
const json_schema_merge_allof_1 = __importDefault(require("json-schema-merge-allof"));
exports.rules = new Map();
/*
// Normalize id
rules.set('$id to id', schema => {
  if (schema.$id) {
    schema.id = schema.$id
  }
})
*/
// Add jsdoc of default to description
exports.rules.set('default to jsdoc', (schema) => {
    if (typeof schema.default !== 'undefined') {
        const jsdoc = `@default ${JSON.stringify(schema.default, null, 2)}`;
        if (schema.description) {
            schema.description += '\n' + jsdoc;
        }
        else {
            schema.description = jsdoc;
        }
    }
});
// Not expressible is TS, just causes issues
exports.rules.set('ingore patternProperties', (schema) => {
    if (schema.patternProperties) {
        delete schema.patternProperties;
    }
});
exports.rules.set('$ref to absolute path', (schema, { $id }) => {
    if (schema.$ref) {
        schema.$ref = new url_1.URL(schema.$ref, $id).toString();
    }
});
// Normalize keywords adjacent to boolean operators
exports.rules.set('Merge base schema into boolean operator subschemas', (schema) => {
    var _a;
    function isObj(schema) {
        return !!schema.properties;
    }
    const booleans = ['allOf', 'anyOf', 'oneOf'];
    if (isObj(schema)) {
        const adjacent = lodash_omit_1.default(schema, booleans.concat(['$id', 'id', '$schema']));
        for (const operator of booleans) {
            if (schema[operator]) {
                schema[operator] = ((_a = schema[operator]) !== null && _a !== void 0 ? _a : []).map((subschema) => json_schema_merge_allof_1.default({ allOf: [subschema, adjacent] }));
            }
        }
    }
});
//# sourceMappingURL=normalize.js.map