<?php

namespace Modules\Admin\EntityMngWsAccounts;

/**
 * This class return the full and updated list of access level configuration, it also consider adding operations from plugins.
 */
class AccountAccessConfig
{

	public $accessLevel = [
		'customer' => [
			'select' =>	true,
			'search' =>	true,
			'insert' =>	true,
			'update' =>	true,
			'delete' =>	true,
		],
		'corporate' => [
			'select' =>	true,
			'search' =>	true,
			'insert' =>	true,
			'update' =>	true,
			'delete' =>	true,
			'match'	 => true,
			'updateProfile'	 => true,
		],
		'corporateOffice' => [
			'select' =>	true,
			'insert' =>	true,
			'update' =>	true,
			'delete' =>	true,
		],
		'contactPerson' => [
			'select' =>	true,
			'search' =>	true,
		],
		'city' => [
			'select' =>	true,
			'insert' => true,
			'search' => true,
			'match'=> true,
		],
		'order' => [
			'select' =>	true,
			'update' =>	true,
		],
		'service' => [
			'select' =>	true,
			'process' => true,
			'update' => true,
			'cancel' => true,
		],
		'offer' => [
			'select' =>	true,
			'update' =>	true,
		],
		'miceProject' => [
			'select' =>	true,
			'match' =>	true,
		],
		'documentSeries' => [
			'select' =>	true,
		],
		'document' => [
			'select' =>	true,
			'insert' => true,
			'sendEmail' => true,
		],
		'supplier' => [
			'select' =>	true,
			'search' =>	true,
			'insert' =>	true,
			'update' =>	true,
			'delete' =>	true,
			'match'	 => true,
		],
		'supplierProduct' => [
			'select' =>	true,
			'insert' =>	true,
			'update' =>	true,
			'delete' =>	true,
			'match'	 => true,
		],

		'supplierInvoice' => [
			'select' => true,
			'insert' => true,
			'update' => true,
		],

		'country' => [
			'select' =>	true,
		],
		'offerProfileRule' => [
			'select' =>	true,
		],
		'importedService' => [
			'select' =>	true,
			'match' =>	true,
		],
		'importedOrder' => [
			'select' =>	true,
			'match' =>	true,
		],
		'importedDocument' => [
		//			'select' =>	true,
			'process' => true,
		],
		'clientInvoice' => [
			'select' =>	true,
			'insert' =>	true,
			'groupRows' => true,
		],
		'cashingDocument' => [
			'select' =>	true,
			'search' =>	true,
			'insert' =>	true,
			'update' =>	true,
			'delete' =>	true,
			'match'	 => true,
		],

		'balance' => [
			'select' => true,
			'insert' => true,
		],

		'webhook' => [
			'insert' =>	true,
		],
		'general' => [
			'calculateOffer' => true,
			'getCurrencyRates' => true,
		],
	];


	// return full list of account access config array
	public function getAccountAccess()
	{
		// trigger that allows plugin to change the access level array
		\Engine\Plugins\Manager::getInstance()->executeEvent('Modules\Admin\EntityMngWsAccounts\AccountAccessConfig-extendAccountAccess', [
			'accessLevel' => &$this->accessLevel,
		]);
		return $this->accessLevel;
	}

	/**
	 * reset access level config array with only enitites that have atleast one operation
	 * @return Array
	 */
	public function getAccountAvailableAccess()
	{
		// get full list of entities and operations including plugins
		$accessLevelConfigArr = $this->getAccountAccess();

		// filter array based on conditions
		foreach($accessLevelConfigArr as $entity => $operations) {
			// filter for any 'FALSE' operations
			$accessLevelConfigArr[$entity] = array_filter($operations);
		}

		// filter entities having no operations
		$accessLevelConfigArr = array_filter($accessLevelConfigArr);

		return $accessLevelConfigArr;
	}
}
