<?php

namespace Atom\runtime_7456b800;

/**
 * 子组件序列化工具
 *
 * @package Atom\runtime_7456b800
 * @auther chenxiao07
 */
class Atom_Children {

    // 1. When the children contains components - because a functional component
    // may return an Array instead of a single root. In this case, just a simple
    // normalization is needed - if any child is an Array, we flatten the whole
    // thing with Array.prototype.concat. It is guaranteed to be only 1-level deep
    // because functional components already normalize their own children.
    /**
     * @param $children mixed
     * @return array
     */
    public static function simpleStringify($children) {
        return Atom_Util_Shared::isPlainArray($children) ? join("", $children) : "";
    }

    // 2. When the children contains constrcuts that always generated nested Arrays,
    // e.g. <template>, <slot>, v-for, or when the children is provided by user
    // with hand-written render functions / JSX. In such cases a full normalization
    // is needed to cater to all possible types of children values.
    /**
     * @param $children mixed
     * @param $normalizationType init
     * @return array|null
     */
    public static function stringify($children, $normalizationType = 1) {

        if (Atom_Util_Shared::isPrimitive($children)) {
            return $children;
        }

        if ($normalizationType === 1) {
            return self::simpleStringify($children);
        }

        if ($normalizationType === 2 && Atom_Util_Shared::isPlainArray($children)) {
            return self::stringifyArrayChildren($children);
        }

        return "";
    }

    /**
     * @param $children mixed
     * @param $normalizationType init
     * @return array|null
     */
    public static function normalize($children, $normalizationType = 1) {

        if (Atom_Util_Shared::isPrimitive($children)) {
            return array($children);
        }

        if (Atom_Util_Shared::isPlainArray($children)) {
            return self::normalizeArrayChildren($children);
        }

        return array();

    }

    /**
     * @param $children mixed
     * @param $nestedIndex string
     * @return array
     */
    private static function stringifyArrayChildren($children) {

        $res = "";

        for ($i = 0; $i < count($children); $i++) {

            $c = $children[$i];

            // 表示不认识它，直接遗弃
            if ($c == null || is_bool($c)) {
                continue;
            }

            if (Atom_Util_Shared::isPrimitive($c)) {
                $res .= $c;
            }
            // nested
            else if (Atom_Util_Shared::isPlainArray($c)) {
                $res .= self::stringifyArrayChildren($c);
            }
        }

        return $res;
    }

    /**
     * @param $children mixed
     * @param $nestedIndex string
     * @return array
     */
    private static function normalizeArrayChildren($children) {

        $res = array();

        for ($i = 0; $i < count($children); $i++) {

            $c = $children[$i];

            // 表示不认识它，直接遗弃
            if ($c == null || is_bool($c)) {
                continue;
            }

            if (Atom_Util_Shared::isPrimitive($c)) {
                array_push($res, $c);
            }
            // nested
            else if (Atom_Util_Shared::isPlainArray($c)) {
                $res = array_merge($res, self::normalizeArrayChildren($c));
            }
        }

        return $res;
    }

    /**
     * 子节点是否全为空，如果是，就清空它们
     * @param $children array
     * @return array
     */
    public static function skipEmptyChidlren($children) {
        foreach ($children as $child) {
            if ($child !== '<!---->') {
                return $children;
            }
        }
        return array();
    }


}
