"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.Url = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _designSystem = require("@wix/design-system");
var _Field = require("../Field");
var _dataHooks = require("../dataHooks");
var _useTextInput = require("../useTextInput");
var _wixUiIconsCommon = require("@wix/wix-ui-icons-common");
var _react2 = require("@wix/bex-core/react");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/fields/Url/Url.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const Url = ({
  dataHook,
  schema,
  initialValue,
  onChange,
  inputRef
}) => {
  const {
    value,
    charCount,
    handleChange
  } = (0, _useTextInput.useTextInput)({
    initialValue,
    maxLength: schema.maxLength,
    onChange
  });
  const ref = (0, _react.useRef)({});
  const [fieldsStatus, setFieldStatus] = (0, _react.useState)({});
  const {
    translate: t
  } = (0, _react2.useWixPatternsContainer)();
  // Same regex as data-extensions https://github.com/wix-private/server-infra/blob/a6c3009d39bbb0312c929f68ee81c559e1eb5b4e/iptf/dataextensions/src/main/scala/com/wixpress/infra/dataextensions/runtime/validator/Formats.scala#L31
  const uriPattern = /^[A-Za-z][A-Za-z0-9+.-]*:(\/\/([A-Za-z0-9._~\-%!$&'()*+,;=:]*@)?[A-Za-z0-9._~\-!$&'()*+,;=%:\[\]]*(:[0-9]*)?)?[A-Za-z0-9._~\-%!$&'()*+,;=:@\/]*([?][A-Za-z0-9._~\-%!$&'()*+,;=:@\/?]*)?([#][A-Za-z0-9._~\-%!$&'()*+,;=:@\/?]*)?$/;
  return /*#__PURE__*/_react.default.createElement(_Field.Field, {
    dataHook: dataHook,
    type: schema.type,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 35,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.FormField, (0, _extends2.default)({
    dataHook: _dataHooks.dataHooks.Label,
    label: schema.label,
    charCount: charCount
  }, fieldsStatus, {
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 36,
      columnNumber: 7
    }
  }), /*#__PURE__*/_react.default.createElement(_designSystem.Input, {
    prefix: /*#__PURE__*/_react.default.createElement(_designSystem.Input.IconAffix, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 44,
        columnNumber: 13
      }
    }, /*#__PURE__*/_react.default.createElement(_wixUiIconsCommon.Link, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 45,
        columnNumber: 15
      }
    })),
    placeholder: t('cairo.customFields.url-input.placeholder'),
    dataHook: _dataHooks.dataHooks.Input,
    value: value,
    onChange: handleChange,
    onBlur: () => {
      ref.current.invalid = value ? !uriPattern.test(value) : false;
      if (ref.current.invalid) {
        setFieldStatus({
          status: 'error',
          statusMessage: t('cairo.customFields.url-input.error')
        });
      } else {
        setFieldStatus({
          status: undefined,
          statusMessage: undefined
        });
      }
    },
    ref: internalRef => {
      inputRef == null || inputRef(ref.current);
      ref.current.focus = () => internalRef == null ? void 0 : internalRef.focus();
    },
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 42,
      columnNumber: 9
    }
  })));
};
exports.Url = Url;
//# sourceMappingURL=Url.js.map