import { _ as _slicedToArray } from '../_verture/slicedToArray-76060636.js';
import { jsx, jsxs } from 'react/jsx-runtime';
import { useState, useEffect } from 'react';
import { $ as $prefixCls } from '../_verture/constant-66aa48a1.js';
import CloseCircleFilled from '@iam-com/icons/CloseCircleF';
import CompleteIcon from '@iam-com/icons/CheckCircleF';
import { s as styleInject } from '../_verture/style-inject.es-300983ab.js';
import '../_verture/unsupportedIterableToArray-cb478f24.js';

var css_248z = "@charset \"UTF-8\";\n/**\n* @author linhd\n* @date 2021/11/1 20:28\n* @description 文字隐藏...\n*/\n/**\n* @author Hanz\n* @date 2021/10/20 下午2:08\n* @description color\n*/\n.paraui-v3-password-rules {\n  padding: 14px 16px;\n  line-height: 1.43;\n}\n.paraui-v3-password-rules > .password-rules-item:first-child > .password-rules-item-title {\n  margin-top: 0px;\n}\n.paraui-v3-password-rules > .password-rules-item > .password-rules-item-title {\n  font-size: 14px;\n  color: rgb(46, 55, 67);\n  font-weight: 700;\n  margin-bottom: 6px;\n  margin-top: 10px;\n}\n.paraui-v3-password-rules > .password-rules-item > .password-rules-item-content > .rules-item-detail {\n  display: flex;\n  margin-bottom: 6px;\n}\n.paraui-v3-password-rules > .password-rules-item > .password-rules-item-content > .rules-item-detail:last-child {\n  margin-bottom: 0;\n}\n.paraui-v3-password-rules > .password-rules-item > .password-rules-item-content > .rules-item-detail > .item-flag {\n  min-width: 24px;\n}\n.paraui-v3-password-rules > .password-rules-item > .password-rules-item-content > .rules-item-detail > .item-label {\n  flex: 1;\n  font-size: 14px;\n  color: rgb(46, 55, 67);\n}\n.paraui-v3-password-rules > .password-rules-item > .password-rules-item-content > .rules-item-detail.rules-item-detail-icon > .item-flag {\n  margin-top: 0px;\n  line-height: 1;\n}\n.paraui-v3-password-rules > .password-rules-item > .password-rules-item-content > .rules-item-detail.rules-item-detail-icon > .item-flag svg {\n  font-size: 16px;\n  color: rgba(46, 55, 67, 0.4);\n  margin-top: 2px;\n}\n.paraui-v3-password-rules > .password-rules-item > .password-rules-item-content > .rules-item-detail.rules-item-detail-order > .item-flag {\n  margin-top: -1px;\n}\n.paraui-v3-password-rules > .password-rules-item > .password-rules-item-content > .rules-item-detail.rules-item-detail-success.rules-item-detail-icon > .item-flag svg {\n  color: rgb(83, 195, 27);\n}\n.paraui-v3-password-rules > .password-rules-item > .password-rules-item-content > .rules-item-detail.rules-item-detail-error.rules-item-detail-order > .item-flag {\n  color: rgb(235, 96, 84);\n}\n.paraui-v3-password-rules > .password-rules-item > .password-rules-item-content > .rules-item-detail.rules-item-detail-error.rules-item-detail-order > .item-label {\n  color: rgb(235, 96, 84);\n}\n.paraui-v3-password-rules.paraui-v3-password-rules-box-shadow {\n  box-shadow: 0px 2px 8px 0px rgba(171, 176, 185, 0.4);\n  border-radius: 4px;\n}";
styleInject(css_248z);

var PasswordRules = function PasswordRules(props) {
  var className = props.className,
    style = props.style,
    _props$boxShadow = props.boxShadow,
    boxShadow = _props$boxShadow === void 0 ? false : _props$boxShadow,
    value = props.value,
    config = props.config,
    onCheckAll = props.onCheckAll;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    configCom = _useState2[0],
    setConfigCom = _useState2[1];
  useEffect(function () {
    var bol = undefined;
    var obj = {};
    var configHand = handConfig();
    configHand.forEach(function (item) {
      var list = item.list || [];
      list.forEach(function (listItem) {
        // 判断校验
        var result;
        if (value !== undefined) {
          // 正则
          if (listItem.reg) {
            try {
              result = listItem.reg.test(value);
            } catch (e) {
              console.log(e);
            }
          }
          if (listItem.validator) {
            result = listItem.validator(listItem, value);
          }
        }
        listItem.result = result;
        if (result !== undefined) {
          obj[listItem.result === true ? 'success' : 'error'] = true;
        }
      });
    });
    setConfigCom(configHand);
    if (obj.error && !obj.success) {
      bol = false;
    }
    if (obj.success && !obj.error) {
      bol = true;
    }
    onCheckAll && onCheckAll(bol);
  }, [value, config, onCheckAll]);
  var handConfig = function handConfig() {
    if (!config) return [];
    return config instanceof Array ? config : [config];
  };
  /** 处理每一项 */
  var handContentItem = function handContentItem(item) {
    var list = item.list || [];
    var type = item.type || 'icon';
    var handRuleItem = function handRuleItem(listItem, inx) {
      // 判断校验
      var result = listItem.result;
      // 处理标志
      var handFlag = function handFlag() {
        if (type === 'order') {
          return inx + 1 + '、';
        }
        if (result === true) return jsx(CompleteIcon, {}); // 成功
        return jsx(CloseCircleFilled, {});
      };
      // 处理内容
      var handLabel = function handLabel() {
        if (typeof result !== "boolean" && result) return result;
        return listItem.label;
      };
      // 处理样式
      var handClassRulesItem = function handClassRulesItem() {
        var str = 'rules-item-detail';
        str += ' rules-item-detail-' + type;
        if (typeof result === "boolean" && result) {
          str += ' rules-item-detail-success';
        } else {
          if (result !== undefined) str += ' rules-item-detail-error';
        }
        return str;
      };
      return jsxs("div", Object.assign({
        className: handClassRulesItem()
      }, {
        children: [jsx("span", Object.assign({
          className: "item-flag"
        }, {
          children: handFlag()
        })), jsx("span", Object.assign({
          className: "item-label"
        }, {
          children: handLabel()
        }))]
      }), inx);
    };
    return jsx("div", Object.assign({
      className: "password-rules-item-content"
    }, {
      children: list.map(function (listItem, index) {
        return handRuleItem(listItem, index);
      })
    }));
  };
  /** 处理className */
  var handClass = function handClass() {
    var str = $prefixCls + '-password-rules';
    if (className) str += ' ' + className;
    if (boxShadow) str += ' ' + $prefixCls + '-password-rules-box-shadow';
    return str;
  };
  return jsx("div", Object.assign({
    className: handClass(),
    style: style
  }, {
    children: configCom.map(function (item, index) {
      return jsxs("div", Object.assign({
        className: 'password-rules-item'
      }, {
        children: [jsx("div", Object.assign({
          className: "password-rules-item-title"
        }, {
          children: item.title
        })), handContentItem(item)]
      }), index);
    })
  }));
};

export { PasswordRules as default };
