Liferay.Loader.define("frontend-js-web@4.0.60/liferay/util/focus_form_field", ['module', 'exports', 'require', './get_element', './in_browser_view'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = focusFormField;

    var _get_element = _interopRequireDefault(require("./get_element"));

    var _in_browser_view = _interopRequireDefault(require("./in_browser_view"));

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

    /**
     * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
     * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
     */
    function getDisabledParents(element) {
      var result = [];

      while (element.parentElement) {
        if (element.parentElement.getAttribute('disabled')) {
          result.push(element.parentElement);
        }

        element = element.parentElement;
      }

      return result;
    }

    function focusFormField(element) {
      element = (0, _get_element.default)(element);

      if ((0, _in_browser_view.default)(element)) {
        var disabledParents = getDisabledParents(element);
        var focusable = !element.getAttribute('disabled') && element.offsetWidth > 0 && element.offsetHeight > 0 && !disabledParents.length;
        var form = element.closest('form');

        if (!form || focusable) {
          element.focus();
        } else if (form) {
          var portletName = form.getAttribute('data-fm-namespace');
          var formReadyEventName = portletName + 'formReady';

          var formReadyHandler = function formReadyHandler(event) {
            var elFormName = form.getAttribute('name');
            var formName = event.formName;

            if (elFormName === formName) {
              element.focus();
              Liferay.detach(formReadyEventName, formReadyHandler);
            }
          };

          Liferay.on(formReadyEventName, formReadyHandler);
        }
      }
    }
    //# sourceMappingURL=focus_form_field.js.map
  }
});
//# sourceMappingURL=focus_form_field.js.map