import { ICompileService, IScope, ITranscludeFunction } from 'angular';


/**
 * $compile with a scope already attached, returning the compiled JQLite elements directly.
 */
export interface IScopedCompileService {
  (element: string | Element | JQuery, transclude?: ITranscludeFunction, maxPriority?: number): JQLite;
}

/**
 * Build a scoped $compile that always compiles against the same $scope.
 *
 * @param {angular.ICompileService} $compile Unscoped $compile
 * @param {angular.IScope}          $scope   The $scope to apply
 * @returns {IScopedCompileService}          A $scope applied $compile
 * @constructor
 */
export function ScopedCompileFactory($compile: ICompileService, $scope: IScope): IScopedCompileService {
  return function (element: string | Element | JQuery, transclude?: ITranscludeFunction, maxPriority?: number) {
    if (transclude) {
      if (maxPriority) {
        return $compile(element, transclude, maxPriority)($scope);
      }

      return $compile(element, transclude)($scope);
    }

    return $compile(element)($scope);
  };
}
