'use strict';

require('babel-polyfill');

var _edgegrid = require('edgegrid');

var _edgegrid2 = _interopRequireDefault(_edgegrid);

var _dotenv = require('dotenv');

var _dotenv2 = _interopRequireDefault(_dotenv);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _akamaiNginx = require('./src/akamai-nginx.js');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var mode, luaOutputFile, localPapiJson, edgercSection, edgegrid, valueMap;
    return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
            switch (_context.prev = _context.next) {
                case 0:

                    // load .env vars
                    _dotenv2.default.config();

                    mode = process.env.AKA_MODE ? process.env.AKA_MODE.toUpperCase() : 'LOCAL';

                    console.log('\n#### STARTING IN ' + mode + ' MODE ####');

                    // lua output file
                    luaOutputFile = process.env.AKA_LUA_OUTPUT_FILE ? process.env.AKA_LUA_OUTPUT_FILE : 'lua/akamai.lua';


                    if (process.env.AKA_MODE !== 'PAPI') {
                        // to run locally against your papi json, run 'npm run configure'
                        // and set env var AKA_PAPI_JSON_FILE to your json, eg. 'papiJson/your.property-v1.papi.json'
                        // lua output file
                        localPapiJson = process.env.AKA_PAPI_JSON_FILE ? process.env.AKA_PAPI_JSON_FILE : 'sample.papi.json';


                        (0, _akamaiNginx.setLocalConfig)(localPapiJson, luaOutputFile);
                    } else {
                        // edgerc section name
                        edgercSection = process.env.AKA_EDGERC_SECTION ? process.env.AKA_EDGERC_SECTION : 'default';

                        // assumes you have configured edgegrid

                        edgegrid = new _edgegrid2.default({
                            path: process.env.AKA_EDGERC,
                            section: edgercSection
                        });

                        // assumes you have set env vars as output of 'npm run configure' or another method

                        (0, _akamaiNginx.setApiConfig)(edgegrid, process.env.AKA_CONTRACT_ID, process.env.AKA_GROUP_ID, process.env.AKA_PROPERTY_ID, process.env.AKA_PROPERTY_VERSION, luaOutputFile);
                    }

                    // map old to new values in generated lua based on local json (gitignored)
                    valueMap = [['staging-old.akamai.com', 'origin'], ['origin.akamai-customer.com', 'origin']];


                    if (_fs2.default.existsSync(__dirname + '/../valueMap.local.json')) {
                        console.log('loading valueMap from valueMap.local.json');
                        valueMap = JSON.parse(_fs2.default.readFileSync(__dirname + '/../valueMap.local.json', 'utf8'));
                    }

                    (0, _akamaiNginx.setValueMap)(new Map(valueMap));

                    // behaviours to skip altogether
                    (0, _akamaiNginx.setSkipBehaviors)(['webApplicationFirewall']);

                    _context.next = 11;
                    return (0, _akamaiNginx.generateConf)();

                case 11:

                    console.log('nginx config written to ' + luaOutputFile);

                case 12:
                case 'end':
                    return _context.stop();
            }
        }
    }, _callee, this);
}))();