/*-
 * ~~~~~~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('entaxyModalGroupFields', {
        bindings: {
            groups: '<',
            fields: '<',
            errors: '<'
        },
        template:
        `
          <div class="modal-group-fields-container">
            <div class="modal-groups-list-container">
              <entaxy-modal-list items="$ctrl.groups" selected="$ctrl.groups[0]"
                    change-selection="$ctrl.change" filter="$ctrl.matchesFilter"></entaxy-modal-list>
            </div>
            <div class="modal-group-fields-form-container">
              <form name="connectorForm" class="form-horizontal">
                <div class="form-group" ng-class="{'has-error': $ctrl.errors[formField.label]}"
                      ng-repeat="formField in $ctrl.viewedFormFields" ng-if="!formField.isBackRef">
                  <label class="col-sm-4 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" ng-show="$ctrl.errors[formField.label]">{{$ctrl.errors[formField.label]}}</span>
                  </div>
                </div>
              </form>
            </div>
          </div>
        `,
        controller: entaxyModalGroupFieldsController
    })
    .name;

    function entaxyModalGroupFieldsController() {

        var ctrl = this;

        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;
                        }
                    });
                }
            });

            return match;
        };
    }

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