'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

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"); } }; }();

exports.default = () => (data, recordTypeConfig) => {
  // Make namespace prefixes consistent. accounts-common-list is sometimes ns2 and sometimes
  var _Object$keys$0$split = Object.keys(recordTypeConfig.fields)[0].split(':', 2),
      _Object$keys$0$split2 = _slicedToArray(_Object$keys$0$split, 2);

  const docNodeNsPrefix = _Object$keys$0$split2[0],
        docNodeName = _Object$keys$0$split2[1];

  var _data$entrySeq$first = data.entrySeq().first(),
      _data$entrySeq$first2 = _slicedToArray(_data$entrySeq$first, 2);

  const rootNodeFullName = _data$entrySeq$first2[0],
        rootNodeData = _data$entrySeq$first2[1];

  var _rootNodeFullName$spl = rootNodeFullName.split(':', 2),
      _rootNodeFullName$spl2 = _slicedToArray(_rootNodeFullName$spl, 2);

  const rootNodeNsPrefix = _rootNodeFullName$spl2[0],
        rootNodeName = _rootNodeFullName$spl2[1];


  if (rootNodeName === docNodeName && rootNodeNsPrefix !== docNodeNsPrefix) {
    const rootNodeNsUri = rootNodeData.get(`@xmlns:${rootNodeNsPrefix}`);

    const updatedRootNodeData = rootNodeData.delete(`@xmlns:${rootNodeNsPrefix}`).set(`@xmlns:${docNodeNsPrefix}`, rootNodeNsUri);

    const updatedData = data.delete(`${rootNodeNsPrefix}:${rootNodeName}`).set(`${docNodeNsPrefix}:${docNodeName}`, updatedRootNodeData);

    return updatedData;
  }

  return data;
};