/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-management-plugin
 * ==========
 * 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~~~~~~
 */
var Entaxy;
(function (Entaxy) {
    Entaxy._module
    .component('entaxyConnectorsWithFlow', {
        bindings: {
            direction: '@',
            connectors: '<',
            profileName: '<',
            editCustomizationPointFn: '<',
            editProfilePropertiesFn: '<',
            editPropertiesFn: '<',
            saveConnectorFn: '<',
            updateFn: '<'
        },
        template:
        `
          <div class="connectors-group-container">
            <span class="connectors-group-title">{{$ctrl.connectorsGroupTitle}}</span>
            <div class="connectors-with-flow-container">
              <div class="connectors-container">
                <entaxy-normal-connector-block ng-repeat="connector in $ctrl.connectors"
                      ng-if="!connector.isCustom" direction="$ctrl.direction" connector="connector"
                      edit-connector-properties-fn="$ctrl.editConnectorProperties"
                      save-connector-fn="$ctrl.saveConnector"
                      edit-connector-customization-point-fn="$ctrl.editConnectorCustomizationPoint">
                </entaxy-normal-connector-block>
                <entaxy-custom-connector-block ng-repeat="connector in $ctrl.connectors"
                      ng-if="connector.isCustom" direction="$ctrl.direction" connector="connector"
                      edit-connector-properties-fn="$ctrl.editConnectorProperties"
                      save-connector-fn="$ctrl.saveConnector"
                      edit-connector-customization-point-fn="$ctrl.editConnectorCustomizationPoint">
                </entaxy-custom-connector-block>
              </div>
              <entaxy-in-flow-block ng-if="$ctrl.isDirectionIn"
                        edit-profile-properties-fn="$ctrl.editProfileProperties"
                        edit-flow-customization-point-fn="$ctrl.editFlowCustomizationPoint"></entaxy-in-flow-block>
              <entaxy-out-flow-block ng-if="!$ctrl.isDirectionIn"
                        edit-profile-properties-fn="$ctrl.editProfileProperties"
                        edit-flow-customization-point-fn="$ctrl.editFlowCustomizationPoint"></entaxy-out-flow-block>
            </div>
          </div>
        `,
        controller: entaxyConnectorsWithFlowController
    })
    .name;

    function entaxyConnectorsWithFlowController(workspace) {
        'ngInject';

        let ctrl = this;

        ctrl.$onInit = function() {
            if (ctrl.direction) {
                ctrl.isDirectionIn = ctrl.direction === 'in';
                ctrl.connectorsGroupTitle = '--- ' + ctrl.direction.toUpperCase() + ' ---';
            } else {
                Entaxy.log.error('Connectors group direction is not defined');
            }
        }

        ctrl.editConnectorProperties = function (connector, event) {
            event.stopPropagation();

            ctrl.editPropertiesFn(connector, Entaxy.RUNTIME_TYPE.CONNECTOR, 'Connector');
        }

        ctrl.editConnectorCustomizationPoint = function (connector, customizationPoint, event) {
            event.stopPropagation();

            let propertyName = getPropertyNameForConnectorCustomizationPoint(customizationPoint);

            ctrl.editCustomizationPointFn(connector.mbeanName, propertyName, connector);
        }

        ctrl.editProfileProperties = function (groupName, event) {
            event.stopPropagation();

            ctrl.editProfilePropertiesFn(groupName);
        }

        ctrl.editFlowCustomizationPoint = function (customizationPoint, event) {
            event.stopPropagation();

            let selectedMbeanName = workspace.getSelectedMBeanName();
            let propertyName = getPropertyNameForFlowCustomizationPoint(customizationPoint);

            ctrl.editCustomizationPointFn(selectedMbeanName, propertyName);
        }

        function getPropertyNameForConnectorCustomizationPoint(customizationPointName) {
            switch (customizationPointName) {
                case 'pre-route':
                case 'pre-output':
                    return customizationPointName;
                case 'response':
                case 'postprocess':
                    return 'responseRoute';
                case 'custom-route':
                    return ctrl.isDirectionIn ? 'customInputRoute' : 'customOutputRoute';
            };
        }

        function getPropertyNameForFlowCustomizationPoint(customizationPointName) {
            switch (customizationPointName) {
                case 'pre-route':
                    return ctrl.direction + '-flow-' + customizationPointName;
                case 'response':
                case 'postprocess':
                    return ctrl.direction + '-flow-response';
                case 'connector-selector':
                    return customizationPointName;
            };
        }

        ctrl.saveConnector = function (connector, event) {
            event.stopPropagation();

            ctrl.saveConnectorFn(connector.mbeanName, true, false);
        }
    }
    entaxyConnectorsWithFlowController.$inject = ['workspace'];

})(Entaxy || (Entaxy = {}));
