/**
 * Copyright (c) 2020-present, Goldman Sachs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { hashArray } from '@finos/legend-shared';
import { CORE_HASH_STRUCTURE } from '../../../../../../../graph/Core_HashUtils.js';
import { AbstractFlatDataPropertyMapping } from './AbstractFlatDataPropertyMapping.js';
import { InferableMappingElementRootExplicitValue } from '../../../mapping/InferableMappingElementRoot.js';
import { FlatDataPropertyMapping } from './FlatDataPropertyMapping.js';
/**
 * We can think of embedded property mappings as a 'gateway' from one set of property mappings to another. They are in a sense
 * both an `InstanceSetImplementation` (since they hold property mappings that map to a class) and a `PropertyMapping` (as it holds a property).
 * The property's owner class belongs to the orginal/root `InstanceSetImplementation`. The property's type is the class mapped as part of the embedded property mapping
 *
 * NOTE: We model this class differently than what we do in Pure metamodel. We make it only implement `SetImplementation`, not extending it for 2 reasons:
 * 1. Javascript only support single inheritance unlike Pure
 * 2. In the general mental model, it is more sensible to think of embedded property mapping as a property mapping rather than a class mapping because
 * despite the fact that it has the shape similar to a class mapping and it can contain multiple property mappings, it itselt is not a class mapping, it must
 * exist "embedded" within a property mapping.
 */
export class EmbeddedFlatDataPropertyMapping extends AbstractFlatDataPropertyMapping {
    _PARENT;
    _isEmbedded = true;
    root = InferableMappingElementRootExplicitValue.create(false);
    class;
    id;
    propertyMappings = [];
    rootInstanceSetImplementation; // in Pure we call this `setMappingOwner`
    mappingClass;
    constructor(owner, property, rootInstanceSetImplementation, source, _class, id, target) {
        super(owner, property, source, target);
        this.class = _class;
        this.id = id;
        this.rootInstanceSetImplementation = rootInstanceSetImplementation;
        this._PARENT = rootInstanceSetImplementation._PARENT;
    }
    get hashCode() {
        return hashArray([
            CORE_HASH_STRUCTURE.EMBEDDED_FLAT_DATA_PROPERTY_MAPPING,
            super.hashCode,
            this.id.valueForSerialization ?? '',
            this.class.value.path,
            // skip `root` since we disregard it in embedded property mappings
            hashArray(this.propertyMappings.filter(
            // TODO: we should also handle of other property mapping types
            // using some form of extension mechanism
            // This is a rather optimistic check as we make assumption on the type of property mapping included here
            (propertyMapping) => {
                if (propertyMapping instanceof FlatDataPropertyMapping) {
                    // TODO: use `isStubbed_RawLambda` when we move this out of the metamodel
                    return (Boolean(propertyMapping.transform.parameters) ||
                        Boolean(propertyMapping.transform.body));
                }
                return true;
            })),
        ]);
    }
    accept_SetImplementationVisitor(visitor) {
        return visitor.visit_EmbeddedFlatDataSetImplementation(this);
    }
    accept_PropertyMappingVisitor(visitor) {
        return visitor.visit_EmbeddedFlatDataPropertyMapping(this);
    }
}
//# sourceMappingURL=EmbeddedFlatDataPropertyMapping.js.map