 picker = angular.module('daterangepicker', [])
 
 picker.constant('dateRangePickerConfig',
+  clearLabel: 'Clear'
   locale:
     separator: ' - '
     format: 'YYYY-MM-DD'

      then f(value)
      else value.split(opts.locale.separator).map(f)

    _validate = (field, validator) ->
      (expected, actual) ->
        if expected and actual
          expected = moment(expected)
          actual = moment(actual)
          valid = validator(expected, actual)
          modelCtrl.$setValidity(field, valid)
          valid
        else
          modelCtrl.$setValidity(field, true)
          true

    _validateMin = _validate 'min', (min, start) -> min.isBefore(start) or min.isSame(start, 'day')
    _validateMax = _validate 'max', (max, end) -> max.isAfter(end) or max.isSame(end, 'day')

    modelCtrl.$formatters.push (val) ->
      if val and val.startDate and val.endDate
        # Update datepicker dates according to val before rendering.
        _setStartDate(val.startDate)
        _setEndDate(val.endDate)
        return val
      ''

    modelCtrl.$parsers.push (val) ->
      # Check if input is valid.
      value = {}
      if angular.isObject(val) and val.hasOwnProperty('startDate') and val.hasOwnProperty('endDate')
        value = val
      if angular.isString(val) and val.length > 0
        x = _parse(val)
        value.startDate = x[0]
        value.endDate = x[1]

      if value.startDate or value.endDate
        _validateMin($scope.dateMin, value.startDate)
        _validateMax($scope.dateMax, value.endDate)
        return value

      modelCtrl.$modelValue

    modelCtrl.$isEmpty = (val) ->
      # modelCtrl is empty if val is invalid or any of the ranges are not set.
      not val or val.startDate == null or val.endDate == null

    modelCtrl.$render = ->
      if not modelCtrl.$modelValue or modelCtrl.$modelValue.startDate == null
      then el.val('')
      else el.val(_format(modelCtrl.$modelValue))

    _init = ->
      el.daterangepicker opts, (start, end) ->
        $timeout ->
          modelCtrl.$setViewValue({startDate: start, endDate: end})
        modelCtrl.$render()


      # Needs to be after daterangerpicker has been created, otherwise
      # watchers that reinit will be attached to old daterangepicker instance.

  require: 'ngModel'
  restrict: 'A'
  scope:
    min: '='
    max: '='
    model: '=ngmodel'
    opts: '=options'
    clearable: '='
  link: ($scope, element, attrs, modelCtrl) ->
