'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.default = undefined;var _slicedToArray = function () {function sliceIterator(arr, i) {var _arr = [];var _n = true;var _d = false;var _e = undefined;try {for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {_arr.push(_s.value);if (i && _arr.length === i) break;}} catch (err) {_d = true;_e = err;} finally {try {if (!_n && _i["return"]) _i["return"]();} finally {if (_d) throw _e;}}return _arr;}return function (arr, i) {if (Array.isArray(arr)) {return arr;} else if (Symbol.iterator in Object(arr)) {return sliceIterator(arr, i);} else {throw new TypeError("Invalid attempt to destructure non-iterable instance");}};}();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 _format = require('@parity/api/lib/util/format');

var _certifier = require('./abi/certifier.json');var _certifier2 = _interopRequireDefault(_certifier);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}

var ZERO20 = '0x0000000000000000000000000000000000000000';
var ZERO32 = '0x0000000000000000000000000000000000000000000000000000000000000000';var

BadgeReg = function () {
  function BadgeReg(api, registry) {_classCallCheck(this, BadgeReg);
    this._api = api;
    this._registry = registry;

    registry.getContract('badgereg');
    this.certifiers = []; // by id
    this.contracts = {}; // by name
  }_createClass(BadgeReg, [{ key: 'getContract', value: function getContract()

    {
      return this._registry.getContract('badgereg');
    } }, { key: 'certifierCount', value: function certifierCount()

    {
      return this.
      getContract().
      then(function (badgeReg) {
        return badgeReg.instance.badgeCount.call({}, []).
        then(function (count) {return count.valueOf();});
      });
    } }, { key: 'fetchCertifier', value: function fetchCertifier(

    id) {var _this = this;
      if (this.certifiers[id]) {
        return Promise.resolve(this.certifiers[id]);
      }

      return this.
      getContract().
      then(function (badgeReg) {
        return badgeReg.instance.badge.call({}, [id]);
      }).
      then(function (_ref) {var _ref2 = _slicedToArray(_ref, 2),address = _ref2[0],name = _ref2[1];
        if (address === ZERO20) {
          throw new Error('Certifier ' + id + ' does not exist.');
        }

        name = (0, _format.bytesToHex)(name);
        name = name === ZERO32 ?
        null :
        (0, _format.hexToAscii)(name);

        return _this.fetchMeta(id).
        then(function (_ref3) {var title = _ref3.title,icon = _ref3.icon;
          var data = { address: address, id: id, name: name, title: title, icon: icon };

          _this.certifiers[id] = data;
          return data;
        });
      });
    } }, { key: 'fetchCertifierByName', value: function fetchCertifierByName(

    name) {var _this2 = this;
      return this.
      getContract().
      then(function (badgeReg) {
        return badgeReg.instance.fromName.call({}, [name]);
      }).
      then(function (_ref4) {var _ref5 = _slicedToArray(_ref4, 3),id = _ref5[0],address = _ref5[1],owner = _ref5[2];
        if (address === ZERO20) {
          throw new Error('Certifier ' + name + ' does not exist.');
        }

        return _this2.fetchMeta(id).
        then(function (_ref6) {var title = _ref6.title,icon = _ref6.icon;
          var data = { address: address, id: id, name: name, title: title, icon: icon };

          _this2.certifiers[id] = data;
          return data;
        });
      });
    } }, { key: 'fetchMeta', value: function fetchMeta(

    id) {
      return this.
      getContract().
      then(function (badgeReg) {
        return Promise.all([
        badgeReg.instance.meta.call({}, [id, 'TITLE']),
        badgeReg.instance.meta.call({}, [id, 'IMG'])]);

      }).
      then(function (_ref7) {var _ref8 = _slicedToArray(_ref7, 2),title = _ref8[0],icon = _ref8[1];
        title = (0, _format.bytesToHex)(title).replace(/(00)+$/, '');
        title = title === ZERO32 ? null : (0, _format.hexToAscii)(title);

        if ((0, _format.bytesToHex)(icon) === ZERO32) {
          icon = null;
        }

        return { title: title, icon: icon };
      });
    } }, { key: 'checkIfCertified', value: function checkIfCertified(

    certifier, address) {
      if (!this.contracts[certifier]) {
        this.contracts[certifier] = this._api.newContract(_certifier2.default, certifier);
      }

      var contract = this.contracts[certifier];

      return contract.instance.certified.call({}, [address]);
    } }]);return BadgeReg;}();exports.default = BadgeReg;