Liferay.Loader.define("layout-seo-web@1.0.53/js/seo/PreviewSeo.es", ['module', 'exports', 'require', 'frontend-js-react-web', 'frontend-js-metal-web$metal', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', './PreviewSeoEvents.es'], 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 = _default;

    var _frontendJsReactWeb = require("frontend-js-react-web");

    var _metal = require("frontend-js-metal-web$metal");

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

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

    var _PreviewSeoEvents = require("./PreviewSeoEvents.es");

    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 _objectWithoutProperties(source, excluded) {
      if (source == null) return {};var target = _objectWithoutPropertiesLoose(source, excluded);var key, i;if (Object.getOwnPropertySymbols) {
        var sourceSymbolKeys = Object.getOwnPropertySymbols(source);for (i = 0; i < sourceSymbolKeys.length; i++) {
          key = sourceSymbolKeys[i];if (excluded.indexOf(key) >= 0) continue;if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;target[key] = source[key];
        }
      }return target;
    }

    function _objectWithoutPropertiesLoose(source, excluded) {
      if (source == null) return {};var target = {};var sourceKeys = Object.keys(source);var key, i;for (i = 0; i < sourceKeys.length; i++) {
        key = sourceKeys[i];if (excluded.indexOf(key) >= 0) continue;target[key] = source[key];
      }return target;
    }

    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 MAX_LENGTH_DESCIPTION = 160;

    var PreviewSeo = function PreviewSeo(_ref) {
      var _ref$description = _ref.description,
          description = _ref$description === void 0 ? '' : _ref$description,
          _ref$direction = _ref.direction,
          direction = _ref$direction === void 0 ? 'ltr' : _ref$direction,
          _ref$displayType = _ref.displayType,
          displayType = _ref$displayType === void 0 ? 'serp' : _ref$displayType,
          _ref$imgUrl = _ref.imgUrl,
          imgUrl = _ref$imgUrl === void 0 ? '' : _ref$imgUrl,
          _ref$title = _ref.title,
          title = _ref$title === void 0 ? '' : _ref$title,
          _ref$titleSuffix = _ref.titleSuffix,
          titleSuffix = _ref$titleSuffix === void 0 ? '' : _ref$titleSuffix,
          _ref$url = _ref.url,
          url = _ref$url === void 0 ? '' : _ref$url;
      var titleUrl = [/*#__PURE__*/_react.default.createElement("div", {
        className: "preview-seo-title text-truncate",
        key: "title"
      }, title, titleSuffix && " - ".concat(titleSuffix)), /*#__PURE__*/_react.default.createElement("div", {
        className: "preview-seo-url text-truncate",
        key: "url"
      }, url)];
      return (/*#__PURE__*/_react.default.createElement("div", {
          className: "preview-seo preview-seo-".concat(displayType),
          dir: direction
        }, imgUrl && /*#__PURE__*/_react.default.createElement("div", {
          className: "aspect-ratio aspect-ratio-191-to-100 preview-seo-image"
        }, /*#__PURE__*/_react.default.createElement("img", {
          alt: "",
          className: "aspect-ratio-item aspect-ratio-item-center-middle aspect-ratio-item-flush",
          src: imgUrl
        })), displayType === 'og' ? titleUrl.reverse() : titleUrl, /*#__PURE__*/_react.default.createElement("div", {
          className: "preview-seo-description"
        }, description.length < MAX_LENGTH_DESCIPTION ? description : "".concat(description.slice(0, MAX_LENGTH_DESCIPTION), "\u2026")))
      );
    };

    var PreviewSeoContainer = function PreviewSeoContainer(_ref2) {
      var displayType = _ref2.displayType,
          portletNamespace = _ref2.portletNamespace,
          targets = _ref2.targets,
          titleSuffix = _ref2.titleSuffix;
      var defaultLanguage = Liferay.ThemeDisplay.getLanguageId();

      var _useState = (0, _react.useState)(defaultLanguage),
          _useState2 = _slicedToArray(_useState, 2),
          language = _useState2[0],
          setLanguage = _useState2[1];

      var _useState3 = (0, _react.useState)({}),
          _useState4 = _slicedToArray(_useState3, 2),
          fields = _useState4[0],
          setFields = _useState4[1];

      var isMounted = (0, _frontendJsReactWeb.useIsMounted)();

      var _useState5 = (0, _react.useState)({}),
          _useState6 = _slicedToArray(_useState5, 2),
          inputTargets = _useState6[0],
          setInputTargets = _useState6[1];

      var getDefaultValue = (0, _react.useCallback)(function (type) {
        var defaultValue = targets[type] && targets[type].defaultValue;

        if ((0, _metal.isObject)(defaultValue)) {
          defaultValue = defaultValue[language] || defaultValue[defaultLanguage];
        }

        return defaultValue;
      }, [defaultLanguage, targets, language]);
      (0, _react.useEffect)(function () {
        var inputLocalizedLocaleChangedHandle = Liferay.on('inputLocalized:localeChanged', function (event) {
          var newLanguage = event.item && event.item.getAttribute('data-value');

          if (newLanguage && isMounted()) {
            setLanguage(newLanguage);
          }
        });
        return function () {
          Liferay.detach(inputLocalizedLocaleChangedHandle);
        };
      }, [isMounted]);
      (0, _react.useEffect)(function () {
        var setFieldState = function setFieldState(_ref3) {
          var type = _ref3.type,
              props = _objectWithoutProperties(_ref3, ["type"]);

          if (!isMounted()) {
            return;
          }

          setFields(function (state) {
            return _objectSpread(_objectSpread({}, state), {}, _defineProperty({}, type, _objectSpread({}, props)));
          });
        };

        var inputTargets = Object.entries(targets).reduce(function (acc, _ref4) {
          var _ref5 = _slicedToArray(_ref4, 2),
              type = _ref5[0],
              _ref5$ = _ref5[1],
              id = _ref5$.id,
              value = _ref5$.value;

          if (id) {
            var input = document.getElementById("".concat(portletNamespace).concat(id));
            var defaultLanguageInput = document.getElementById("".concat(portletNamespace).concat(id, "_").concat(defaultLanguage));
            acc[type] = {
              defaultLanguageInput: defaultLanguageInput,
              input: input,
              type: type
            };
          } else if (value) {
            setFieldState({
              type: type,
              value: value
            });
          }

          return acc;
        }, {});
        setInputTargets(inputTargets);

        var handleInputChange = function handleInputChange(_ref6) {
          var event = _ref6.event,
              type = _ref6.type;
          var target = event.target;

          if (!target) {
            return;
          }

          var disabled = target.disabled,
              value = target.value;
          setFieldState({
            disabled: disabled,
            type: type,
            value: value
          });
        };

        var inputs = Object.values(inputTargets).reduce(function (acc, _ref7) {
          var input = _ref7.input,
              type = _ref7.type;

          var listener = function listener(event) {
            handleInputChange({
              event: event,
              type: type
            });
          };

          input.addEventListener('input', listener);
          acc.push({
            input: input,
            listener: listener
          });
          return acc;
        }, []);
        var previewSeoOnChangeHandle = (0, _PreviewSeoEvents.previewSeoOnChange)(portletNamespace, setFieldState);
        return function () {
          inputs.forEach(function (_ref8) {
            var input = _ref8.input,
                listener = _ref8.listener;
            return input.removeEventListener('input', listener);
          });
          Liferay.detach(previewSeoOnChangeHandle);
        };
      }, [defaultLanguage, isMounted, portletNamespace, targets]);
      (0, _react.useEffect)(function () {
        if (!isMounted()) {
          return;
        }

        var newFieldsState = Object.values(inputTargets).reduce(function (acc, _ref9) {
          var input = _ref9.input,
              type = _ref9.type;
          var disabled = input.disabled,
              value = input.value;
          acc[type] = {
            disabled: disabled,
            value: value
          };
          return acc;
        }, {});
        setFields(function (prevFieldsState) {
          return _objectSpread(_objectSpread({}, prevFieldsState), newFieldsState);
        });
      }, [inputTargets, isMounted, language]);

      var getValue = function getValue(type) {
        var disabled = fields[type] && fields[type].disabled;
        var value = fields[type] && fields[type].value;

        if (disabled || !value) {
          var defaultLanguageInput = inputTargets[type] && inputTargets[type].defaultLanguageInput;
          var defaultLanguageInputValue = defaultLanguageInput && !defaultLanguageInput.disabled && defaultLanguageInput.value;

          if (language !== defaultLanguage && defaultLanguageInputValue) {
            value = defaultLanguageInputValue;
          } else {
            value = getDefaultValue(type);
          }
        }

        return value || '';
      };

      return (/*#__PURE__*/_react.default.createElement(PreviewSeo, {
          description: getValue('description'),
          direction: Liferay.Language.direction[language],
          displayType: displayType,
          imgUrl: getValue('imgUrl'),
          title: getValue('title'),
          titleSuffix: titleSuffix,
          url: getValue('url')
        })
      );
    };

    function _default(props) {
      return (/*#__PURE__*/_react.default.createElement(PreviewSeoContainer, props)
      );
    }
    //# sourceMappingURL=PreviewSeo.es.js.map
  }
});
//# sourceMappingURL=PreviewSeo.es.js.map