import template from './ws-notification.html';

NotificationDirective.$inject = ['$sce', '$timeout'];

export default function NotificationDirective($sce, $timeout) {
  return {
    restrict: 'E',
    template,
    scope: {
      type: '@',
      headerString: '@?',
      bodyString: '@',
      actionString: '@?',
      actionable: '@',
      dismissible: '@',
      action: '&?',
      dismiss: '&?',
      timeout: '@?',
    },
    link(scope) {
      scope.visible = true;
      scope.headerStringSafe = $sce.trustAsHtml(scope.headerString);
      scope.bodyStringSafe = $sce.trustAsHtml(scope.bodyString);
      scope.actionStringSafe = $sce.trustAsHtml(scope.actionString);

      scope.hide = () => {
        scope.visible = false;
        if (scope.dismiss) {
          scope.dismiss();
        }
      };

      if (scope.timeout) {
        $timeout(scope.hide, scope.timeout);
      }
    },
  };
}
