"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getAttributeObject = getAttributeObject;
exports.getAttributeProps = getAttributeProps;
exports.getDocument = getDocument;
exports.getPropKey = getPropKey;
exports.parseJson = parseJson;
var _model = require("./model");
/* -----------------------------------
 *
 * parseJson
 *
 * -------------------------------- */

function parseJson(value) {
  var tagName = this.tagName;
  var formatProps = this.__options.formatProps;
  var result = {};
  try {
    result = JSON.parse(value);
  } catch (_unused) {
    console.error(_model.ErrorTypes.Json, ": <".concat(tagName.toLowerCase(), ">"));
  }
  if (formatProps) {
    result = formatProps(result);
  }
  return result;
}

/* -----------------------------------
 *
 * getDocument
 *
 * -------------------------------- */

function getDocument(html) {
  var value = "<!DOCTYPE html>\n<html><body>".concat(html, "</body></html>");
  var nodes;
  try {
    nodes = new DOMParser().parseFromString(value, 'text/html');
  } catch (_unused2) {
    // no-op
  }
  if (!nodes) {
    return void 0;
  }
  return nodes.body;
}

/* -----------------------------------
 *
 * getAttributeObject
 *
 * -------------------------------- */

function getAttributeObject(attributes) {
  var result = {};
  if (!(attributes !== null && attributes !== void 0 && attributes.length)) {
    return result;
  }
  for (var i = attributes.length - 1; i >= 0; i--) {
    var item = attributes[i];
    result[item.name] = item.value;
  }
  return result;
}

/* -----------------------------------
 *
 * getAttributeProps
 *
 * -------------------------------- */

function getAttributeProps(attributes, allowed) {
  var values = getAttributeObject(attributes);
  var result = {};
  for (var _i = 0, _Object$keys = Object.keys(values); _i < _Object$keys.length; _i++) {
    var key = _Object$keys[_i];
    if ((allowed === null || allowed === void 0 ? void 0 : allowed.indexOf(key)) === -1) {
      continue;
    }
    result[getPropKey(key)] = values[key];
  }
  return result;
}

/* -----------------------------------
 *
 * Attribute
 *
 * -------------------------------- */

function getPropKey(value) {
  var sanitised = value.trim().replace(/[\s_]/g, '-');
  return sanitised.charAt(0).toLowerCase() + sanitised.slice(1).replace(/-([a-z])/g, function (_ref) {
    var value = _ref[1];
    return value.toUpperCase();
  });
}

/* -----------------------------------
 *
 * Export
 *
 * -------------------------------- */