"use strict";

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

var _binding = _interopRequireDefault(require("../util/binding"));

var _types = require("../util/types");

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const {
  Internals
} = _binding.default;
/**
 * @param {any} target
 * @param {VTree} vTree
 */

const invokeRef = (target = _types.EMPTY.OBJ, vTree) => {
  let {
    ref
  } = target.props || target; // Allow refs to be passed to HTML elements. When in a DOM environment
  // a Node will be passed to the ref function and assigned.

  if (!ref) {
    target = Internals.NodeCache.get(vTree);
    ref = vTree.attributes.ref;
  }

  if (typeof ref === 'function') {
    ref(target);
  } else if (typeof ref === 'object' && ref) {
    ref.current = target;
  } else if (typeof ref === 'string') {
    target.refs = _objectSpread(_objectSpread({}, target.refs), {}, {
      [ref]: target
    });
  }
};

const invokeRefsForVTrees = (
/** @type {(VTree | null)[]} */
...vTrees) => {
  for (let i = 0; i < vTrees.length; i++) {
    const vTree = vTrees[i];
    if (!vTree) continue;

    const componentTree = _types.ComponentTreeCache.get(vTree);

    const instance = _types.InstanceCache.get(componentTree || vTree);

    if (vTree.childNodes.length) {
      invokeRefsForVTrees(...vTree.childNodes);
    }

    if (!instance) {
      invokeRef(Internals.NodeCache.get(vTree), vTree);
      continue;
    } // If any instances exist, loop through them and invoke the respective `ref`
    // logic.


    invokeRef(instance, vTree);
  }
};
/**
 * @param {VTree} vTree
 *
 * @returns {void}
 */


function componentDidMount(vTree) {
  const componentTree = _types.ComponentTreeCache.get(vTree);
  /** @type {VTree[]} */


  const childTrees = [];

  _types.ComponentTreeCache.forEach((parentTree, childTree) => {
    if (parentTree === componentTree) {
      childTrees.push(childTree);
    }
  });

  invokeRefsForVTrees(vTree); // Only trigger a mount for the first element.

  if (childTrees.length === 0 || childTrees[0] !== vTree) {
    return;
  } // If there is a parent, ensure it is called recursively.


  if (_types.ComponentTreeCache.has(componentTree)) {
    componentDidMount(componentTree);
  }

  const instance = _types.InstanceCache.get(componentTree);

  instance && instance.componentDidMount && instance.componentDidMount();
}

module.exports = exports.default;