"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _components = require("@wordpress/components");
var _data = require("@wordpress/data");
var _i18n = require("@wordpress/i18n");
var _element = require("@wordpress/element");
var _store = require("../store");
var _components2 = require("../components");
var _lockUnlock = require("../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

// The implementation of content locking is mainly in this file, although the mechanism
// to stop temporarily editing as blocks when an outside block is selected is on component StopEditingAsBlocksOnOutsideSelect
// at block-editor/src/components/block-list/index.js.
// Besides the components on this file and the file referenced above the implementation
// also includes artifacts on the store (actions, reducers, and selector).
function ContentLockControlsPure({
  clientId,
  isSelected
}) {
  const {
    templateLock,
    isLockedByParent,
    isEditingAsBlocks
  } = (0, _data.useSelect)(select => {
    const {
      getContentLockingParent,
      getTemplateLock,
      getTemporarilyEditingAsBlocks
    } = (0, _lockUnlock.unlock)(select(_store.store));
    return {
      templateLock: getTemplateLock(clientId),
      isLockedByParent: !!getContentLockingParent(clientId),
      isEditingAsBlocks: getTemporarilyEditingAsBlocks() === clientId
    };
  }, [clientId]);
  const {
    stopEditingAsBlocks,
    modifyContentLockBlock
  } = (0, _lockUnlock.unlock)((0, _data.useDispatch)(_store.store));
  const isContentLocked = !isLockedByParent && templateLock === 'contentOnly';
  const stopEditingAsBlockCallback = (0, _element.useCallback)(() => {
    stopEditingAsBlocks(clientId);
  }, [clientId, stopEditingAsBlocks]);
  if (!isContentLocked && !isEditingAsBlocks) {
    return null;
  }
  const showStopEditingAsBlocks = isEditingAsBlocks && !isContentLocked;
  const showStartEditingAsBlocks = !isEditingAsBlocks && isContentLocked && isSelected;
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [showStopEditingAsBlocks && /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components2.BlockControls, {
        group: "other",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ToolbarButton, {
          onClick: stopEditingAsBlockCallback,
          children: (0, _i18n.__)('Done')
        })
      })
    }), showStartEditingAsBlocks && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components2.BlockSettingsMenuControls, {
      children: ({
        selectedClientIds,
        onClose
      }) => selectedClientIds.length === 1 && selectedClientIds[0] === clientId && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
        onClick: () => {
          modifyContentLockBlock(clientId);
          onClose();
        },
        children: (0, _i18n.__)('Modify')
      })
    })]
  });
}
var _default = exports.default = {
  edit: ContentLockControlsPure,
  hasSupport() {
    return true;
  }
};
//# sourceMappingURL=content-lock-ui.js.map