/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
import { Info, Metadata, OperationGroup, Schemas, Security } from "@autorest/codemodel";
import { enableSourceTracking } from "@azure-tools/codegen";
export class CodeModel extends Metadata {
    constructor(title, sourceTracking = false, objectInitializer) {
        super();
        // if we are enabling source tracking, then we have to use a proxied version of this
        const $this = sourceTracking ? enableSourceTracking(this) : this;
        $this.info = new Info(title);
        $this.schemas = new Schemas();
        $this.operationGroups = [];
        $this.security = new Security(false);
        $this.clients = [];
        this.applyTo($this, objectInitializer);
    }
    get globals() {
        return this.globalParameters || (this.globalParameters = []);
    }
    getOperationGroup(group) {
        let result = this.operationGroups.find((each) => group.toLowerCase() === each.$key.toLowerCase());
        if (!result) {
            result = new OperationGroup(group);
            this.operationGroups.push(result);
        }
        return result;
    }
    findGlobalParameter(predicate) {
        return this.globals.find(predicate);
    }
    addGlobalParameter(predicateOrParameter, create = undefined) {
        try {
            if (typeof predicateOrParameter !== "function") {
                // overload : parameter passed
                this.globals.push(predicateOrParameter);
                return predicateOrParameter;
            }
            // overload : predicate, parameter passed
            let p = this.findGlobalParameter(predicateOrParameter);
            if (!p) {
                this.globals.push((p = realize(create)));
            }
            return p;
        }
        finally {
            this.globalParameters = sortAscendingInvalidLast(this.globals, (each) => each.extensions?.["x-ms-priority"]);
        }
    }
}
function realize(f) {
    return f instanceof Function ? f() : f;
}
function sortAscendingInvalidLast(input, accessor) {
    return input.sort((a, b) => {
        const pA = accessor(a) ?? Number.MAX_VALUE;
        const pB = accessor(b) ?? Number.MAX_VALUE;
        return pA - pB;
    });
}
//# sourceMappingURL=code-model.js.map