import { Memory } from "./Memory.js";

export class Block {

	parameters = {};

	constructor(name, pipeline) {
		this.name     = name;
		this.pipeline = pipeline;
		this.id       = -1;

		this.passes  = [];
		this.passMap = new Map();

		this.memory = new Memory("block");
	}

	getPreviousBlock() {
		if (this.id === 0) return null;
		return this.pipeline.blocks[this.id - 1];
	}

	addPass( name, passInstance ) {

		if (this.passMap.has(name)) {
			throw new Error(`Pass '${name}' already exists in block '${this.name}'.`);
		}

		passInstance.passName     = name;
		passInstance.indexInBlock = this.passes.length;

		passInstance.pipeline     = this.pipeline;
		passInstance.block        = this;
		passInstance.device       = this.pipeline.device;

		if( this.layerIndex !== undefined ) {

			passInstance.layerIndex = this.layerIndex;

		}


		this.passes.push(passInstance);
		this.passMap.set(name, passInstance);

		return passInstance;
	}

	getPass(name) {
		return this.passMap.get(name);
	}

	getAllPasses() {
		return this.passes;
	}

	getPreviousPass(passInstance) {
		const idx = passInstance.indexInBlock;
		if (idx <= 0) return null;
		return this.passes[idx - 1];
	}

	setLayerIndex( layerIndex ) {

		this.layerIndex = layerIndex;

		for (var i = 0; i < this.passes.length; i++) {

			this.passes[i].layerIndex = layerIndex;

		}


	}
	
}
