"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.cdmObjectRefPersistence = void 0;
const internal_1 = require("../../internal");
const copyDataUtils = require("../../Utilities/CopyDataUtils");
const utils = require("./utils");
class cdmObjectRefPersistence {
    static toData(instance, resOpt, options) {
        // We don't know what object we are creating to initialize to any
        // tslint:disable-next-line:no-any
        let copy = {};
        if (!internal_1.StringUtils.isBlankByCdmStandard(instance.namedReference)) {
            const identifier = utils.copyIdentifierRef(instance, resOpt, options);
            if (instance.simpleNamedReference) {
                return identifier;
            }
            const replace = cdmObjectRefPersistence.copyRefData(instance, resOpt, copy, identifier, options);
            if (replace) {
                copy = replace;
            }
        }
        else if (instance.explicitReference) {
            const erCopy = instance.explicitReference.copyData(resOpt, options);
            const replace = cdmObjectRefPersistence.copyRefData(instance, resOpt, copy, erCopy, options);
            if (replace) {
                copy = replace;
            }
        }
        if (instance.optional !== undefined) {
            copy.optional = instance.optional;
        }
        if (instance.appliedTraits.length > 0) {
            // We don't know if the object we are copying has applied traits or not and hence use any
            // tslint:disable-next-line:no-any
            copy.appliedTraits = copyDataUtils.arrayCopyData(resOpt, instance.appliedTraits, options);
        }
        return copy;
    }
    static copyRefData(instance, resOpt, copy, refTo, options) {
        switch (instance.objectType) {
            case internal_1.cdmObjectType.attributeGroupRef:
                copy.attributeGroupReference = refTo;
                return copy;
            case internal_1.cdmObjectType.dataTypeRef:
                copy.dataTypeReference = refTo;
                return copy;
            case internal_1.cdmObjectType.entityRef:
                copy.entityReference = refTo;
                return copy;
            case internal_1.cdmObjectType.purposeRef:
                copy.purposeReference = refTo;
                return copy;
            case internal_1.cdmObjectType.traitRef:
                const traitRef = copy;
                traitRef.traitReference = refTo;
                traitRef.arguments = copyDataUtils.arrayCopyData(resOpt, instance.arguments, options);
                return traitRef;
            case internal_1.cdmObjectType.traitGroupRef:
                const traitGroupRef = copy;
                traitGroupRef.traitGroupReference = refTo;
                return traitGroupRef;
            default:
                return undefined;
        }
    }
}
exports.cdmObjectRefPersistence = cdmObjectRefPersistence;
//# sourceMappingURL=cdmObjectRefPersistence.js.map