module.exports = (grunt) ->
  grunt.initConfig
    pkg: grunt.file.readJSON 'package.json'

    concurrent:
      watchCss: [
        'watch:lessIndex',
        'watch:sassBootstrap'
      ]
      options:
        logConcurrentOutput: true

    less:
      options:
        ieCompat: false
        sourceMap: true
      devIndex:
        files:
          'index.css': ['index.less']

    lesshint:
      options:
        allowWarnings: true
      src: [
        'lib/**/*.less'
        'index.less'
      ]

    sass:
      options:
        sourceMap: true
        includePaths: [
          "node_modules/bootstrap/scss",
          "lib/vendor/bootstrap"
        ]
      devBootstrap:
        files:
          'lib/vendor/bootstrap/index.css': ['lib/vendor/bootstrap/bootstrap.scss']

    postcss:
      options:
        map:
          inline: false
        processors: [
          require('autoprefixer')({
            browsers: ['last 3 version']
          })
          require('postcss-em-media-query')()
          require('cssnano')({
            zindex: false
          })
        ]
      devIndex:
        src: 'index.css'

    eslint:
      options:
        configFile: '.eslintrc.js'
      target: [
        '../../../../app/design/frontend/Flagbit/beyerdynamic/web/js/*.js',
        '../../../../app/code/Beyerdynamic/ProductDetailPage/view/frontend/web/js/*.js'
      ]

    browserSync:
      default:
        options:
          proxy: 'https://beyerdynamic.dev'
          reloadDelay: 4000
          open: false
          watchTask: true
          ghostMode: false
          online: false
          cors: true
        bsFiles:
          src: ['index.css']
      styleguide:
        options:
          open: false
          watchTask: true
          ghostMode: false
          online: false
          cors: true
          server:
            baseDir: './',
            index: 'index.html'
        bsFiles:
          src: ['index.css']
    
    copy:
      indexCss:
        files:
          '../beyerdynamic-magento/src/pub/node_modules/@flagbit/beyer-components/index.css': 'index.css'

    clean:
      indexCss: [
        'index.css',
        '*.map'
      ]
      bootstrapCss: ['lib/vendor/bootstrap/*.css*']

    exec:
      typescript:
        cmd: 'tsc -w'

    watch:
      lessIndex:
        files: [
          'lib/**/*.less'
          'index.less'
        ]
        tasks: [
          'less:devIndex',
          'postcss:devIndex',
          'copy:indexCss'
        ]
      sassBootstrap:
        files: ['lib/vendor/bootstrap/*.scss']
        tasks: [
          'sass:devBootstrap',
          'less:devIndex',
          'postcss:devIndex'
        ]

  grunt.loadNpmTasks 'grunt-browser-sync'
  grunt.loadNpmTasks 'grunt-concurrent'
  grunt.loadNpmTasks 'grunt-contrib-clean'
  grunt.loadNpmTasks 'grunt-contrib-copy'
  grunt.loadNpmTasks 'grunt-contrib-less'
  grunt.loadNpmTasks 'grunt-contrib-watch'
  grunt.loadNpmTasks 'grunt-eslint'
  grunt.loadNpmTasks 'grunt-exec'
  grunt.loadNpmTasks 'grunt-lesshint'
  grunt.loadNpmTasks 'grunt-postcss'
  grunt.loadNpmTasks 'grunt-sass'

  grunt.registerTask 'default', ['clean', 'sass:devBootstrap', 'less:devIndex', 'postcss:devIndex']
  grunt.registerTask 'w', ['clean', 'sass:devBootstrap', 'less:devIndex', 'postcss:devIndex', 'concurrent:watchCss']
  grunt.registerTask 'sync', ['clean', 'sass:devBootstrap', 'less:devIndex', 'postcss:devIndex', 'browserSync:default',
    'concurrent:watchCss']
  grunt.registerTask 'copysync', ['clean', 'sass:devBootstrap', 'less:devIndex', 'postcss:devIndex', 'copy:indexCss', 'browserSync:default',
    'concurrent:watchCss']
  grunt.registerTask 'styleguide', ['clean', 'sass:devBootstrap', 'less:devIndex', 'postcss:devIndex',
    'browserSync:styleguide', 'concurrent:watchCss']
  grunt.registerTask 'lint', ['eslint']
  grunt.registerTask 'tsc', ['exec:typescript']

