/* eslint-env node */
'use strict';

const Funnel = require('broccoli-funnel');
const MergeTrees = require('broccoli-merge-trees');
const Path = require('path');

module.exports = {
  name: 'ember-mapbox-gl',

  treeForVendor(vendorTree) {
    const mapboxGlTree = new Funnel(Path.dirname(require.resolve('mapbox-gl/dist/mapbox-gl.js')), {
      files: [ 'mapbox-gl.js', 'mapbox-gl.css' ],
    });

    if (vendorTree) {
      return new MergeTrees([ vendorTree, mapboxGlTree ]);
    }

    return mapboxGlTree;
  },

  included() {
    this._super.included.apply(this, arguments);

    this.import('vendor/mapbox-gl.js', {
      using: [
        { transformation: 'amd', as: 'mapbox-gl' }
      ]
    });

    this.import('vendor/mapbox-gl.css');
  }
};

/* eslint-env node */
'use strict';

const Funnel = require('broccoli-funnel');
const MergeTrees = require('broccoli-merge-trees');
const Path = require('path');

module.exports = {
  name: 'ember-mapbox-gl',

  treeForStyles(tree) {
    const mapboxGlTree = new Funnel(Path.dirname(require.resolve('mapbox-gl')), {
      files: [ 'mapbox-gl.css' ],
      destDir: 'app/styles'
    });

    if (tree) {
      return new MergeTrees([ tree, mapboxGlTree ]);
    }

    return mapboxGlTree;
  },

  treeForVendor(tree) {
    const mapboxGlTree = new Funnel(Path.dirname(require.resolve('mapbox-gl')), {
      files: [
        'mapbox-gl.js',
        'mapbox-gl-dev.js'
      ],
      destDir: '/mapbox-gl'
    });

    if (tree) {
      return new MergeTrees([ tree, mapboxGlTree ]);
    }

    return mapboxGlTree;
  },

  included(app) {
    this._super.included.apply(this, arguments);

    const srcFile = process.env.EMBER_ENV === 'production'
      ? 'vendor/mapbox-gl/mapbox-gl.js'
      : 'vendor/mapbox-gl/mapbox-gl-dev.js';

    app.import(srcFile, {
      using: [
        { transformation: 'amd', as: 'mapbox-gl' }
      ]
    });

    app.import('app/styles/mapbox-gl.css');
  }
};

send css to the style tree and import the dev build when not in production