"use strict";

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

var _react = require("react");

var _utils = require("@teamix/utils");

var _utils2 = require("../utils");

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_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"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var useAutoLayout = function useAutoLayout(_ref, formRef) {
  var outBreakpoints = _ref.breakpoints,
      layout = _ref.layout,
      labelAlign = _ref.labelAlign,
      wrapperAlign = _ref.wrapperAlign,
      outLabelCol = _ref.labelCol,
      outWrapperCol = _ref.wrapperCol,
      autoLayout = _ref.autoLayout,
      schema = _ref.schema;

  var _ref2 = (0, _utils.isPlainObj)(autoLayout) ? autoLayout : {
    autoLabel: autoLayout
  },
      autoLabel = _ref2.autoLabel;

  var breakpoints = (0, _react.useMemo)(function () {
    return autoLabel ? false : outBreakpoints;
  }, []); // 当前断点不支持Col更新

  var _useState = (0, _react.useState)({
    labelCol: outLabelCol,
    wrapperCol: outWrapperCol
  }),
      _useState2 = _slicedToArray(_useState, 2),
      autoCol = _useState2[0],
      setAutoCol = _useState2[1];

  var labelCol = autoCol.labelCol,
      wrapperCol = autoCol.wrapperCol;
  (0, _react.useEffect)(function () {
    /**
     * autolayout
     */
    if (autoLabel) {
      var formWidth = formRef.current.getBoundingClientRect().width; // 表单宽度

      var labelArray = schema.map(function (item) {
        var text = item.title;

        if ((0, _utils.isStr)(text)) {
          var _item$decoratorProps;

          if (item.required) {
            text += '一'; // 必填 + 12
          }

          if ((_item$decoratorProps = item.decoratorProps) === null || _item$decoratorProps === void 0 ? void 0 : _item$decoratorProps.tooltip) {
            text += '一'; // 提示 + 18
          }

          return text;
        }

        return '';
      }); // 标题内容集合

      var labelWidthArray = (0, _utils.getWidthByCharacterMap)(labelArray); // 标题宽度集合

      var _autoWidthBySpace = (0, _utils.autoWidthBySpace)(labelWidthArray),
          expectedWidth = _autoWidthBySpace.expectedWidth,
          consoleArray = _autoWidthBySpace.consoleArray; // 智能计算预期宽度


      var expectedCol = Math.ceil(expectedWidth / formWidth * 24); // 预期宽度转换为 Col 值

      setAutoCol({
        labelCol: expectedCol,
        wrapperCol: wrapperCol
      });
    } else {
      setAutoCol({
        labelCol: outLabelCol,
        wrapperCol: outWrapperCol
      });
    }
  }, [outLabelCol, outWrapperCol]); // Layout配置By断点（配置断点后，目前不支持 Col 更新）

  var autoTeamixLayout = (0, _react.useMemo)(function () {
    return breakpoints === true ? {
      breakpoints: [480],
      layout: (0, _utils2.mergeArrayValue)(['vertical'], layout),
      labelAlign: (0, _utils2.mergeArrayValue)(['left'], labelAlign),
      wrapperAlign: (0, _utils2.mergeArrayValue)(['left'], wrapperAlign),
      labelCol: (0, _utils2.mergeArrayValue)([24], labelCol),
      wrapperCol: (0, _utils2.mergeArrayValue)([24], wrapperCol)
    } : {
      breakpoints: (0, _utils.isBool)(breakpoints) ? undefined : breakpoints,
      layout: layout,
      labelAlign: labelAlign,
      wrapperAlign: wrapperAlign,
      labelCol: labelCol,
      wrapperCol: wrapperCol
    };
  }, [breakpoints, layout, labelAlign, wrapperAlign, labelCol, wrapperCol]);
  return autoTeamixLayout;
};

var _default = useAutoLayout;
exports.default = _default;