/*-
 * ~~~~~~licensing~~~~~~
 * object-producer-core
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.platform.core.producer.executor.generationmodel.merge;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ru.entaxy.esb.platform.runtime.base.extensions.api.EntaxyExtension;
import ru.entaxy.esb.platform.runtime.base.extensions.api.EntaxyExtensionsAware;
import ru.entaxy.platform.core.producer.executor.generationmodel.merge.MergingMap.Mapping;

public abstract class AbstractMergeProcessor implements MergeProcessor, EntaxyExtensionsAware {

	protected Map<String, List<String>> mapping;
	
	protected List<EntaxyExtension> entaxyExtensions = new ArrayList<>();
	
	protected AbstractMergeProcessor() {
		super();
		mapping = new HashMap<>();
		if (this.getClass().isAnnotationPresent(MergingMap.class)) {
			MergingMap map = this.getClass().getAnnotation(MergingMap.class);
			Mapping[] mappings = map.mappings();
			if (mappings != null)
				for (int i=0; i<mappings.length; i++) {
					String source = mappings[i].source();
					String[] targets = mappings[i].targets();
					if ((targets == null) || (targets.length == 0))
						continue;
					this.mapping.put(source, Arrays.asList(targets));
				}
		}
	}
	
	@Override
	public Map<String, List<String>> getMergingMap() {
		return mapping;
	}

	@Override
	public void addExtension(EntaxyExtension entaxyExtension) {
		if (!entaxyExtensions.contains(entaxyExtension))
			entaxyExtensions.add(entaxyExtension);
	}
	
	@Override
	public void removeExtension(EntaxyExtension entaxyExtension) {
		entaxyExtensions.remove(entaxyExtension);
	}
}
