'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _helper = require('./Shared/helper.js');

var _CIconModule = require('./CIcon.module.css');

var _CIconModule2 = _interopRequireDefault(_CIconModule);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

//component - CoreUI / CIconRaw

var CIconRaw = function CIconRaw(props) {
  var className = props.className,
      name = props.name,
      content = props.content,
      customClasses = props.customClasses,
      size = props.size,
      src = props.src,
      title = props.title,
      use = props.use,
      attributes = _objectWithoutProperties(props, ['className', 'name', 'content', 'customClasses', 'size', 'src', 'title', 'use']);

  // methods

  var toCamelCase = function toCamelCase(str) {
    return str.replace(/([-_][a-z0-9])/ig, function ($1) {
      return $1.toUpperCase().replace('-', '');
    });
  };

  //vars

  var iconName = function () {
    var iconNameIsKebabCase = name && name.includes('-');
    return iconNameIsKebabCase ? toCamelCase(name) : name;
  }();
  var titleCode = function () {
    return title ? '<title>' + title + '</title>' : '';
  }();
  var code = function () {
    if (content) {
      return content;
    } else if (_react2.default.icons) {
      return _react2.default.icons[iconName] ? _react2.default.icons[iconName] : _react2.default.icons['cil' + iconName] ? _react2.default.icons['cil' + iconName] : (0, _helper.colog)('Not existing icon: ' + iconName);
    }
    return content;
  }();
  var iconCode = function () {
    return Array.isArray(code) ? code[1] || code[0] : code;
  }();
  var scale = function () {
    return Array.isArray(code) && code.length > 1 ? code[0] : '64 64';
  }();
  var viewBox = function () {
    return attributes.viewBox || '0 0 ' + scale;
  }();
  var computedSize = function () {
    var addCustom = !size && (attributes.width || attributes.height);
    return size === 'custom' || addCustom ? 'custom-size' : size;
  }();
  //console.log(iconName, computedSize)
  /*
    computedClasses () {
        const size = this.computedSize
        return this.customClasses || ['c-icon', { [`c-icon-${size}`]: size }]
      }
  */

  //render

  var class1 = (0, _helper.mapToCssModules)((0, _classnames2.default)(className, 'c-icon', computedSize ? 'c-icon-' + computedSize : ''));

  var class2 = (0, _helper.mapToCssModules)((0, _classnames2.default)('c-icon', computedSize ? 'c-icon-' + computedSize : ''), _CIconModule2.default);

  var classes = customClasses || class1 + ' ' + class2;

  //console.log(code, viewBox, classes, titleCode+' - '+iconCode);
  //v-html={titleCode+iconCode}

  return _react2.default.createElement(
    _react2.default.Fragment,
    null,
    !src && !use ? _react2.default.createElement('svg', _extends({}, attributes, {
      xmlns: 'http://www.w3.org/2000/svg',
      viewBox: viewBox,
      className: classes,
      role: 'img',
      dangerouslySetInnerHTML: { __html: titleCode + iconCode }
    })) : src ? _react2.default.createElement('img', _extends({}, attributes, {
      src: src,
      role: 'img'
    })) : use ? _react2.default.createElement(
      'svg',
      _extends({}, attributes, {
        xmlns: 'http://www.w3.org/2000/svg',
        className: classes,
        role: 'img'
      }),
      _react2.default.createElement('use', { href: use })
    ) : ''
  );
};

CIconRaw.propTypes = process.env.NODE_ENV !== "production" ? {
  tag: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.string]),
  className: _propTypes2.default.string,
  //
  name: _propTypes2.default.string,
  content: _propTypes2.default.oneOfType([String, Array]),
  size: _propTypes2.default.oneOf(['custom', 'custom-size', 'sm', 'lg', 'xl', '2xl', '3xl', '4xl', '5xl', '6xl', '7xl', '8xl', '9xl']),
  customClasses: _propTypes2.default.oneOfType([String, Array, Object]),
  src: _propTypes2.default.string,
  title: _propTypes2.default.string,
  use: _propTypes2.default.string
} : {};

CIconRaw.defaultProps = {};

exports.default = CIconRaw;
module.exports = exports['default'];