/**
 * @ngdoc directive
 * @name uiSelectSt
 * @restrict A
 * @element ui-select inside ui-grid
 * @param { number } fontSize? Size of the font - If null/undefined, it will get the font-size of the body
 * @description Is used to created cellTemplate for ui-grid
 * @example
 * <pre>
 *      <ui-select ng-model="MODEL_COL_FIELD" ui-select-st font-size="12">
 *      or
 *      <ui-select ng-model="MODEL_COL_FIELD" multiple theme="ui-select-st" remove-on-list="{searchBy: 'myKey'}" ui-select-st font-size="12">
 * </pre>
 */
/* @ngInject */
export function uiSelectStDirective($timeout) {
    return {
        require: 'uiSelect',
        restrict: 'A',
        link: function($scope, $elm, $attr, $select){
            $elm.addClass('ui-select-st');
            let fontSize = window.getComputedStyle(document.body, null).getPropertyValue('font-size');
            if (angular.isDefined($attr.fontSize)) fontSize = $scope.$eval($attr.fontSize) + 'px';
            if (fontSize){
                $timeout(() => {
                    angular.element($elm[0].querySelector('.ui-select-search')).css('font-size', fontSize);
                    angular.element($elm[0].querySelector('.ui-select-match .ui-select-toggle .ui-select-match-text')).css('font-size', fontSize);
                    angular.element($elm[0].querySelector('.ui-select-choices')).css('font-size', fontSize);
                });
            }
            $scope.$on('closeUiSelect', () => {
                if ($select.open) $select.close();
            });
        }
    }
}