Liferay.Loader.define("layout-content-page-editor-web@2.0.110/page_editor/app/components/fragment-configuration-fields/LinkField", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/form', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', '../../../common/components/MappingSelector', '../../../prop-types/index', '../../config/constants/editableTypes', '../../services/InfoItemService', '../../store/index', '../../utils/isMapped', '../../utils/useId'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    function _typeof(obj) {
      "@babel/helpers - typeof";
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function _typeof(obj) {
          return typeof obj;
        };
      } else {
        _typeof = function _typeof(obj) {
          return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
        };
      }return _typeof(obj);
    }

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

    var _form = _interopRequireWildcard(require("@frontend-taglib-clay$clayui/form"));

    var _propTypes = _interopRequireDefault(require("frontend-js-react-web$prop-types"));

    var _react = _interopRequireWildcard(require("frontend-js-react-web$react"));

    var _MappingSelector = _interopRequireDefault(require("../../../common/components/MappingSelector"));

    var _index = require("../../../prop-types/index");

    var _editableTypes = require("../../config/constants/editableTypes");

    var _InfoItemService = _interopRequireDefault(require("../../services/InfoItemService"));

    var _index2 = require("../../store/index");

    var _isMapped = _interopRequireDefault(require("../../utils/isMapped"));

    var _useId = require("../../utils/useId");

    function _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { default: obj };
    }

    function _getRequireWildcardCache() {
      if (typeof WeakMap !== "function") return null;var cache = new WeakMap();_getRequireWildcardCache = function _getRequireWildcardCache() {
        return cache;
      };return cache;
    }

    function _interopRequireWildcard(obj) {
      if (obj && obj.__esModule) {
        return obj;
      }if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") {
        return { default: obj };
      }var cache = _getRequireWildcardCache();if (cache && cache.has(obj)) {
        return cache.get(obj);
      }var newObj = {};var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;if (desc && (desc.get || desc.set)) {
            Object.defineProperty(newObj, key, desc);
          } else {
            newObj[key] = obj[key];
          }
        }
      }newObj.default = obj;if (cache) {
        cache.set(obj, newObj);
      }return newObj;
    }

    function ownKeys(object, enumerableOnly) {
      var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {
          return Object.getOwnPropertyDescriptor(object, sym).enumerable;
        });keys.push.apply(keys, symbols);
      }return keys;
    }

    function _objectSpread(target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {
          ownKeys(Object(source), true).forEach(function (key) {
            _defineProperty(target, key, source[key]);
          });
        } else if (Object.getOwnPropertyDescriptors) {
          Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
        } else {
          ownKeys(Object(source)).forEach(function (key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
          });
        }
      }return target;
    }

    function _defineProperty(obj, key, value) {
      if (key in obj) {
        Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });
      } else {
        obj[key] = value;
      }return obj;
    }

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

    function _nonIterableRest() {
      throw new TypeError("Invalid attempt to destructure non-iterable instance");
    }

    function _iterableToArrayLimit(arr, i) {
      if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
        return;
      }var _arr = [];var _n = true;var _d = false;var _e = undefined;try {
        for (var _i = arr[Symbol.iterator](), _s; !(_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 SOURCE_OPTIONS = {
      fromContentField: {
        label: "".concat(Liferay.Language.get('from-content-field')),
        value: 'fromContentField'
      },
      manual: {
        label: "".concat(Liferay.Language.get('manual')),
        value: 'manual'
      }
    };
    var TARGET_OPTIONS = [{
      label: "".concat(Liferay.Language.get('self')),
      value: '_self'
    }, {
      label: "".concat(Liferay.Language.get('blank')),
      value: '_blank'
    }, {
      label: "".concat(Liferay.Language.get('parent')),
      value: '_parent'
    }, {
      label: "".concat(Liferay.Language.get('top')),
      value: '_top'
    }];
    exports.TARGET_OPTIONS = TARGET_OPTIONS;

    function LinkField(_ref) {
      var field = _ref.field,
          onValueSelect = _ref.onValueSelect,
          value = _ref.value;

      var _useState = (0, _react.useState)(value || {}),
          _useState2 = _slicedToArray(_useState, 2),
          nextValue = _useState2[0],
          setNextValue = _useState2[1];

      var _useState3 = (0, _react.useState)(nextValue.href),
          _useState4 = _slicedToArray(_useState3, 2),
          nextHref = _useState4[0],
          setNextHref = _useState4[1];

      var _useState5 = (0, _react.useState)(null),
          _useState6 = _slicedToArray(_useState5, 2),
          mappedHrefPreview = _useState6[0],
          setMappedHrefPreview = _useState6[1];

      var languageId = (0, _index2.useSelector)(function (state) {
        return state.languageId;
      });

      var _useState7 = (0, _react.useState)((0, _isMapped.default)(value) ? SOURCE_OPTIONS.fromContentField.value : SOURCE_OPTIONS.manual.value),
          _useState8 = _slicedToArray(_useState7, 2),
          source = _useState8[0],
          setSource = _useState8[1];

      var hrefInputId = (0, _useId.useId)();
      var hrefPreviewInputId = (0, _useId.useId)();
      var sourceInputId = (0, _useId.useId)();
      var targetInputId = (0, _useId.useId)();
      (0, _react.useEffect)(function () {
        if (nextValue.classNameId && nextValue.classPK && nextValue.fieldId) {
          setMappedHrefPreview('');

          _InfoItemService.default.getInfoItemFieldValue(_objectSpread(_objectSpread({}, nextValue), {}, {
            languageId: languageId,
            onNetworkStatus: function onNetworkStatus() {}
          })).then(function (_ref2) {
            var fieldValue = _ref2.fieldValue;
            setMappedHrefPreview(fieldValue || '');
          });
        } else {
          setMappedHrefPreview(null);
        }
      }, [languageId, nextValue]);

      var handleChange = function handleChange(value) {
        var updatedValue = _objectSpread(_objectSpread({}, nextValue), value);

        onValueSelect(field.name, updatedValue);
        setNextValue(updatedValue);
      };

      return (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_form.default.Group, {
          small: true
        }, /*#__PURE__*/_react.default.createElement("label", {
          htmlFor: sourceInputId
        }, Liferay.Language.get('link')), /*#__PURE__*/_react.default.createElement(_form.ClaySelectWithOption, {
          id: sourceInputId,
          onChange: function handleSourceChange(event) {
            onValueSelect(field.name, {});
            setNextValue({});
            setSource(event.target.value);
            setMappedHrefPreview(null);
          },
          options: Object.values(SOURCE_OPTIONS),
          value: source
        })), source === SOURCE_OPTIONS.manual.value && /*#__PURE__*/_react.default.createElement(_form.default.Group, {
          small: true
        }, /*#__PURE__*/_react.default.createElement("label", {
          htmlFor: hrefInputId
        }, Liferay.Language.get('url')), /*#__PURE__*/_react.default.createElement(_form.ClayInput, {
          id: hrefInputId,
          onBlur: function onBlur() {
            return handleChange({
              href: nextHref
            });
          },
          onChange: function onChange(event) {
            return setNextHref(event.target.value);
          },
          type: "text",
          value: nextHref || ''
        })), source === SOURCE_OPTIONS.fromContentField.value && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_MappingSelector.default, {
          fieldType: _editableTypes.EDITABLE_TYPES.link,
          mappedItem: nextValue,
          onMappingSelect: function onMappingSelect(mappedItem) {
            return handleChange(mappedItem);
          }
        }), mappedHrefPreview !== null && /*#__PURE__*/_react.default.createElement(_form.default.Group, {
          small: true
        }, /*#__PURE__*/_react.default.createElement("label", {
          htmlFor: hrefPreviewInputId
        }, Liferay.Language.get('url')), /*#__PURE__*/_react.default.createElement(_form.ClayInput, {
          id: hrefPreviewInputId,
          readOnly: true,
          value: mappedHrefPreview
        }))), /*#__PURE__*/_react.default.createElement(_form.default.Group, {
          small: true
        }, /*#__PURE__*/_react.default.createElement("label", {
          htmlFor: targetInputId
        }, Liferay.Language.get('target')), /*#__PURE__*/_react.default.createElement(_form.ClaySelectWithOption, {
          id: targetInputId,
          onChange: function onChange(event) {
            return handleChange({
              target: event.target.value
            });
          },
          options: TARGET_OPTIONS,
          value: nextValue.target
        })))
      );
    }
    //# sourceMappingURL=LinkField.js.map
  }
});
//# sourceMappingURL=LinkField.js.map