function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React from 'react';
import { Input } from 'antd';
import { LinkOutlined } from '@ant-design/icons';
import { useTextInputState } from '@pisell/utils';
import { validateUrl, validateUrlProtocol, normalizeUrl } from "../utils";
/**
 * PisellUrl 编辑态视图组件
 * 
 * 职责：
 * - 渲染编辑态 URL 输入框
 * - 失焦时自动补全协议
 * - 实时格式校验
 * - 错误提示展示
 * 
 * @param props 组件 Props
 */
var EditView = function EditView(props) {
  var _props$placeholder = props.placeholder,
    placeholder = _props$placeholder === void 0 ? '请输入 URL 地址' : _props$placeholder,
    _props$maxLength = props.maxLength,
    maxLength = _props$maxLength === void 0 ? 2048 : _props$maxLength,
    _props$allowClear = props.allowClear,
    allowClear = _props$allowClear === void 0 ? false : _props$allowClear,
    _props$autoFocus = props.autoFocus,
    autoFocus = _props$autoFocus === void 0 ? false : _props$autoFocus,
    _props$disabled = props.disabled,
    disabled = _props$disabled === void 0 ? false : _props$disabled,
    className = props.className,
    style = props.style,
    _props$autoCompletePr = props.autoCompleteProtocol,
    autoCompleteProtocol = _props$autoCompletePr === void 0 ? true : _props$autoCompletePr,
    _props$defaultProtoco = props.defaultProtocol,
    defaultProtocol = _props$defaultProtoco === void 0 ? 'https://' : _props$defaultProtoco,
    _props$strictValidati = props.strictValidation,
    strictValidation = _props$strictValidati === void 0 ? true : _props$strictValidati,
    allowedProtocols = props.allowedProtocols,
    _props$requireProtoco = props.requireProtocol,
    requireProtocol = _props$requireProtoco === void 0 ? false : _props$requireProtoco,
    errorMessage = props.errorMessage,
    onProtocolComplete = props.onProtocolComplete;

  // 状态管理（复用公共 Hook）
  var _useTextInputState = useTextInputState(_objectSpread(_objectSpread({}, props), {}, {
      validator: function validator(val) {
        if (!val) return true;

        // 规范化 URL（用于校验）
        var normalizedUrl = autoCompleteProtocol ? normalizeUrl(val, defaultProtocol) : val;

        // 1. 格式校验
        if (strictValidation && !validateUrl(normalizedUrl)) {
          return errorMessage || 'URL 地址格式不正确';
        }

        // 2. 协议必需校验
        if (requireProtocol && !val.match(/^[a-z]+:\/\//i)) {
          return 'URL 必须包含协议（如 http:// 或 https://）';
        }

        // 3. 协议白名单校验
        if (allowedProtocols && allowedProtocols.length > 0) {
          var protocolValidation = validateUrlProtocol(normalizedUrl, allowedProtocols);
          if (!protocolValidation.isValid) {
            return protocolValidation.errors[0];
          }
        }
        return true;
      }
    })),
    value = _useTextInputState.value,
    isValid = _useTextInputState.isValid,
    errors = _useTextInputState.errors,
    handleChange = _useTextInputState.handleChange,
    handleBlur = _useTextInputState.handleBlur,
    handleFocus = _useTextInputState.handleFocus;

  // 失焦时自动补全协议
  var handleBlurWithProtocol = function handleBlurWithProtocol() {
    if (autoCompleteProtocol && value && !value.match(/^[a-z]+:\/\//i)) {
      var normalized = normalizeUrl(value, defaultProtocol);
      if (normalized !== value) {
        handleChange(normalized);
        onProtocolComplete === null || onProtocolComplete === void 0 || onProtocolComplete(normalized);
      }
    }
    handleBlur();
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-url-edit"
  }, /*#__PURE__*/React.createElement(Input, {
    type: "url",
    value: value,
    onChange: function onChange(e) {
      return handleChange(e.target.value);
    },
    onBlur: handleBlurWithProtocol,
    onFocus: handleFocus,
    prefix: /*#__PURE__*/React.createElement(LinkOutlined, null),
    placeholder: placeholder,
    maxLength: maxLength,
    allowClear: allowClear,
    autoFocus: autoFocus,
    disabled: disabled,
    status: isValid ? undefined : 'error',
    className: className,
    style: style,
    "aria-label": props['aria-label'],
    "aria-describedby": props['aria-describedby']
  }), !isValid && errors.length > 0 && /*#__PURE__*/React.createElement("div", {
    className: "pisell-url-edit-error"
  }, errors[0]));
};
export default EditView;