"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelsNamespace = void 0;
const codemodel_1 = require("@autorest/codemodel");
const linq_1 = require("@azure-tools/linq");
const codegen_csharp_1 = require("@azure-tools/codegen-csharp");
const clientruntime_1 = require("../clientruntime");
const object_1 = require("../schema/object");
const schema_resolver_1 = require("../schema/schema-resolver");
const model_class_1 = require("./model-class");
const class_1 = require("../../cmdlets/class");
class ApiVersionNamespace extends codegen_csharp_1.Namespace {
    constructor(namespace, objectInitializer) {
        super(namespace);
        this.apply(objectInitializer);
        this.add(new codegen_csharp_1.ImportDirective(`static ${clientruntime_1.ClientRuntime.Extensions}`));
    }
    get outputFolder() {
        return this.name.replace(/^.*\.Models/g, 'Models').replace(/\./g, '/');
    }
}
class ModelsNamespace extends codegen_csharp_1.Namespace {
    constructor(parent, schemas, state, objectInitializer) {
        super('Models', parent);
        this.schemas = schemas;
        this.state = state;
        this.subNamespaces = new linq_1.Dictionary();
        this.resolver = new schema_resolver_1.SchemaDefinitionResolver(this.state.project.fixedArray);
        this.newResolver = new schema_resolver_1.SchemaDefinitionResolver(this.state.project.fixedArray);
        this.subNamespaces[this.fullName] = this;
        this.apply(objectInitializer);
        this.add(new codegen_csharp_1.ImportDirective(`static ${clientruntime_1.ClientRuntime.Extensions}`));
        // special case... hook this up before we get anywhere.
        state.project.modelsNamespace = this;
        if (schemas.objects) {
            for (const schema of schemas.objects) {
                this.NewResolveTypeDeclaration(schema, true, state);
            }
        }
        if (schemas.dictionaries) {
            for (const schema of schemas.dictionaries) {
                this.NewResolveTypeDeclaration(schema, true, state);
            }
        }
        if (schemas.any) {
            for (const schema of schemas.any) {
                this.NewResolveTypeDeclaration(schema, true, state);
            }
        }
        if (schemas.strings) {
            for (const schema of schemas.strings) {
                this.NewResolveTypeDeclaration(schema, true, state);
            }
        }
        if (schemas.sealedChoices) {
            for (const schema of schemas.sealedChoices) {
                this.NewResolveTypeDeclaration(schema, true, state);
            }
        }
        //todo, need to add support for other types
    }
    get outputFolder() {
        return 'Models';
    }
    NewResolveTypeDeclaration(schema, required, state) {
        var _a, _b, _c, _d, _e, _f;
        if (!schema) {
            throw new Error('SCHEMA MISSING?');
        }
        const td = this.newResolver.resolveTypeDeclaration(schema, required, state, state.project.fixedArray);
        if (!((_a = schema.language.csharp) === null || _a === void 0 ? void 0 : _a.skip)) {
            if (td instanceof object_1.ObjectImplementation) {
                // it's a class object.
                // create it if necessary
                const fullname = ((_b = schema.language.csharp) === null || _b === void 0 ? void 0 : _b.namespace) || this.fullName;
                const ns = this.subNamespaces[fullname] || this.add(new ApiVersionNamespace(fullname));
                ns.header = this.state.project.license;
                const mc = ((_c = schema.language.csharp) === null || _c === void 0 ? void 0 : _c.classImplementation) || new model_class_1.ModelClass(ns, td, this.state, { description: (_d = schema.language.csharp) === null || _d === void 0 ? void 0 : _d.description });
                // this gets implicity created during class creation:
                return (_e = schema.language.csharp) === null || _e === void 0 ? void 0 : _e.interfaceImplementation;
            }
            if (state.project.azure && /^api-?version$/i.exec(((_f = schema.language.csharp) === null || _f === void 0 ? void 0 : _f.name) || '')) {
                return td;
            }
            if ((0, class_1.isEnumImplementation)(schema)) {
                schema.language.csharp = schema.language.csharp || new codemodel_1.Language();
                return schema.language.csharp.typeDeclaration = td;
            }
        }
        return td;
    }
}
exports.ModelsNamespace = ModelsNamespace;
//# sourceMappingURL=namespace.js.map