"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _slugid = _interopRequireDefault(require("slugid"));
var _constants = require("../constants");
var _core = require("../../../extension/core");
const PARAGRAPH_TAGS = ['DIV', 'P'];
const listRule = (element, parseChild) => {
  const {
    nodeName,
    childNodes,
    firstChild,
    parentElement
  } = element;
  if (nodeName === 'UL') {
    return {
      id: _slugid.default.nice(),
      type: _constants.UNORDERED_LIST,
      children: parseChild(childNodes)
    };
  }
  if (nodeName === 'OL') {
    return {
      id: _slugid.default.nice(),
      type: _constants.ORDERED_LIST,
      children: parseChild(childNodes)
    };
  }
  if (nodeName === 'LI' && firstChild && PARAGRAPH_TAGS.includes(firstChild.nodeName)) {
    return {
      id: _slugid.default.nice(),
      type: _constants.LIST_ITEM,
      children: parseChild(childNodes)
    };
  }
  if (nodeName === 'LI' && firstChild && !PARAGRAPH_TAGS.includes(firstChild.nodeName)) {
    return {
      id: _slugid.default.nice(),
      type: _constants.LIST_ITEM,
      children: [{
        id: _slugid.default.nice(),
        type: _constants.PARAGRAPH,
        children: parseChild(childNodes)
      }]
    };
  }
  if (PARAGRAPH_TAGS.includes(nodeName) && parentElement && parentElement.nodeName === 'LI') {
    if (Array.from(childNodes).length === 0) {
      return {
        id: _slugid.default.nice(),
        type: _constants.PARAGRAPH,
        children: [(0, _core.generateDefaultText)()]
      };
    }
    return {
      id: _slugid.default.nice(),
      type: _constants.PARAGRAPH,
      children: parseChild(childNodes)
    };
  }
  return;
};
var _default = exports.default = listRule;