/*
 * @Author: your name
 * @Date: 2021-01-21 15:19:16
 * @LastEditTime: 2021-01-21 15:22:35
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \precise_delivery_distribution_front\src\plugins\smooth-dnd\util.ts
 */
const isArray = function(obj: any) {
  return Object.prototype.toString.call(obj) === "[object Array]";
};

export const getTagProps = (
  props: Record<string, any>,
  tagClasses?: string
) => {
  const tag = props.tag;
  if (tag) {
    if (typeof tag === "string") {
      const result: Record<string, any> = { value: tag };
      if (tagClasses) {
        result.props = { class: tagClasses };
      }
      return result;
    } else if (typeof tag === "object") {
      const result = { value: tag.value || "div", props: tag.props || {} };

      if (tagClasses) {
        if (result.props.class) {
          if (isArray(result.props.class)) {
            result.props.class.push(tagClasses);
          } else {
            result.props.class = [tagClasses, result.props.class];
          }
        } else {
          result.props.class = tagClasses;
        }
      }

      return result;
    }
  }
  return { value: "div" };
};

export const validateTagProp = (tag: any) => {
  if (tag) {
    if (typeof tag === "string") return true;
    if (typeof tag === "object") {
      if (
        typeof tag.value === "string" ||
        typeof tag.value === "function" ||
        typeof tag.value === "object"
      ) {
        return true;
      }
    }
    return false;
  }
  return true;
};
