'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.default = undefined;var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {return typeof obj;} : function (obj) {return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;};var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}(); // Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

var _browserWrapper = require('solc/browser-wrapper');var _browserWrapper2 = _interopRequireDefault(_browserWrapper);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}var

SolidityUtils = function () {function SolidityUtils() {_classCallCheck(this, SolidityUtils);}_createClass(SolidityUtils, null, [{ key: 'compile', value: function compile(
    data, compiler) {var
      sourcecode = data.sourcecode,build = data.build,optimize = data.optimize,files = data.files,_data$name = data.name,name = _data$name === undefined ? '' : _data$name;

      var start = Date.now();

      console.log('[solidity] compiling...');

      var input = _defineProperty({},
      name, sourcecode);


      var findFiles = function findFiles(path) {
        var file = files.find(function (f) {return f.name === path;});

        if (file) {
          return { contents: file.sourcecode };
        } else {
          return { error: 'File not found' };
        }
      };

      var compiled = compiler.compile({ sources: input }, optimize ? 1 : 0, findFiles);

      var time = Math.round((Date.now() - start) / 100) / 10;

      console.log('[solidity] done compiling in ' + time + 's');

      compiled.version = build.longVersion;
      compiled.sourcecode = sourcecode;

      return compiled;
    } }, { key: 'getCompiler', value: function getCompiler(

    build) {var
      longVersion = build.longVersion,path = build.path;
      var URL = 'https://cdn.rawgit.com/ethereum/solc-bin/gh-pages/bin/' + path;
      var isWorker = (typeof window === 'undefined' ? 'undefined' : _typeof(window)) !== 'object';

      return fetch(URL).
      then(function (r) {return r.text();}).
      then(function (code) {
        // `window` for main thread, `self` for workers
        var _self = isWorker ? self : window;

        _self.Module = {};

        var solcCode = code.replace('var Module;', 'var Module=' + (isWorker ? 'self' : 'window') + '.Module;');

        console.log('[solidity] evaluating ' + longVersion);

        try {
          // eslint-disable-next-line no-eval
          eval(solcCode);
        } catch (e) {
          return Promise.reject(e);
        }

        console.log('[solidity] done evaluating ' + longVersion);

        var compiler = (0, _browserWrapper2.default)(_self.Module);

        delete _self.Module;

        return compiler;
      });
    } }]);return SolidityUtils;}();exports.default = SolidityUtils;