/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-management-plugin
 * ==========
 * Copyright (C) 2020 - 2021 EmDev LLC
 * ==========
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ~~~~~~/licensing~~~~~~
 */
var Entaxy;
(function (Entaxy) {
    Entaxy._module
    .component('secondStepModalTest', {
        bindings: {
            groups: '<',
            fields: '<',
            errors: '<'
        },
        template:
        `
            <div class="col-md-4" style="margin-left:-20px;">
              <first-step-modal-test templates="$ctrl.groups" selected="$ctrl.groups[0]"
                    change-selection="$ctrl.change" filter="$ctrl.matchesFilter"></first-step-modal-test>
            </div>
            <div class="modal-body-form-container col-md-8">
              <form name="connectorForm" class="form-horizontal">
                <div class="form-group" ng-repeat="formField in $ctrl.viewedFormFields">
                  <label class="col-sm-3 control-label" ng-class="{'required-pf': formField.required}" for="{{formField.label}}">{{formField.label}}</label>
                  <div class="col-sm-7">
                    <input type="{{formField.type}}" id="{{formField.label}}" ng-class="{'form-control': formField.type !== 'checkbox'}"
                          ng-model="formField.value" ng-readonly="formField.readOnly">
                    <!--<span class="help-block">{{formField.helpText}}</span>-->
                  </div>
                  <div>
                    <button type="button" class="btn btn-link btn-add-modal" ng-click="$ctrl.addModal(formField)"
                          ng-if="formField.hasAddButton && !formField.nestedValue">
                      <span class="pficon pficon-add-circle-o"></span>
                    </button>
                    <button type="button" class="btn btn-link btn-add-modal" ng-click="$ctrl.deleteNestedValue(formField)"
                          ng-if="formField.hasAddButton && formField.nestedValue">
                      <!--<span class="pficon pficon-error-circle-o"></span>-->
                      <span class="pficon pficon-delete"></span>
                    </button>
                  </div>
                </div>
              </form>
            </div>
        `,
        controller: secondStepModalTestController
    })
    .name;

function secondStepModalTestController() {
        'ngInject';

        var ctrl = this;

        ctrl.$onInit = function() {

        }

        ctrl.change = function(group) {
            ctrl.viewedFormFields = ctrl.fields.filter((formField) => formField.group === group.name);
        }

        ctrl.matchesFilter = function(group, filter) {
            var match = false;

            _.forEach(ctrl.fields.filter((formField) => formField.group === group.name), (field) => {
                if (field.tag && field.tag.length !== 0) {
                    _.forEach(field.tag, (tag) => {
                        if (tag.match(filter.value) !== null) {
                            match = true;
                            return match;
                        }
                    });
                }
            });

//            if (filter.id === 'name') {
//                match = item.name.match(filter.value) !== null;
//            }
            return match;
        };

    }
    secondStepModalTestController.$inject = [];

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