text: import { EntityDefinition } from 'src/core/shopware';
  import utils from 'src/core/service/util.service';
  import { warn } from 'src/core/service/utils/debug.utils';
  import Entity from './entity.data';
  import EntityCollection from './entity-collection.data';
  import Criteria from './criteria.data';
  
  export default class EntityFactory {
      /**
       * Creates a new entity for the provided entity name.
       * Returns null for unknown entities.
       *
       * @param {String} entityName
       * @param {String} id
       * @param {Object} context
       * @returns {Entity|null}
       */
      create(entityName, id, context) {
          id = id || utils.createId();
  
          const definition = EntityDefinition.get(entityName);
  
          if (!definition) {
              warn('Entity factory', `No schema found for entity ${entityName}`);
              return null;
          }
  
          const data = {};
  
          const toManyAssociations = definition.getToManyAssociations();
          Object.keys(toManyAssociations).forEach((property) => {
              const associatedProperty = toManyAssociations[property].entity;
              data[property] = this.createCollection(entityName, id, property, associatedProperty, context);
          });
  
          const entity = new Entity(id, entityName, data);
          entity.markAsNew();
  
          return entity;
      }
  
      /**
       * @private
       * @param {String} entity
       * @param {String} id
       * @param {String} property
       * @param {String} related
       * @param {Object} context
       * @returns {EntityCollection}
       */
      createCollection(entity, id, property, related, context) {
          const subRoute = property.replace(/_/g, '-');
          const route = entity.replace(/_/g, '-');
          const source = `/${route}/${id}/${subRoute}`;
  
          const criteria = new Criteria();
          criteria.setLimit(10);
          criteria.setPage(1);
  
          return new EntityCollection(source, related, context, criteria);
      }
  }
  