"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = ContentOnlySettingsMenu;
var _blockEditor = require("@wordpress/block-editor");
var _coreData = require("@wordpress/core-data");
var _components = require("@wordpress/components");
var _data = require("@wordpress/data");
var _i18n = require("@wordpress/i18n");
var _store = require("../../store");
var _lockUnlock = require("../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function ContentOnlySettingsMenuItems({
  clientId,
  onClose
}) {
  const {
    entity,
    onNavigateToEntityRecord,
    canEditTemplates
  } = (0, _data.useSelect)(select => {
    const {
      getBlockEditingMode,
      getBlockParentsByBlockName,
      getSettings,
      getBlockAttributes
    } = select(_blockEditor.store);
    const contentOnly = getBlockEditingMode(clientId) === 'contentOnly';
    if (!contentOnly) {
      return {};
    }
    const patternParent = getBlockParentsByBlockName(clientId, 'core/block', true)[0];
    let record;
    if (patternParent) {
      record = select(_coreData.store).getEntityRecord('postType', 'wp_block', getBlockAttributes(patternParent).ref);
    } else {
      const {
        getCurrentTemplateId,
        getRenderingMode
      } = select(_store.store);
      const templateId = getCurrentTemplateId();
      const {
        getContentLockingParent
      } = (0, _lockUnlock.unlock)(select(_blockEditor.store));
      if (getRenderingMode() === 'template-locked' && !getContentLockingParent(clientId) && templateId) {
        record = select(_coreData.store).getEntityRecord('postType', 'wp_template', templateId);
      }
    }
    const _canEditTemplates = select(_coreData.store).canUser('create', {
      kind: 'postType',
      name: 'wp_template'
    });
    return {
      canEditTemplates: _canEditTemplates,
      entity: record,
      onNavigateToEntityRecord: getSettings().onNavigateToEntityRecord
    };
  }, [clientId]);
  if (!entity) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(TemplateLockContentOnlyMenuItems, {
      clientId: clientId,
      onClose: onClose
    });
  }
  const isPattern = entity.type === 'wp_block';
  let helpText = isPattern ? (0, _i18n.__)('Edit the pattern to move, delete, or make further changes to this block.') : (0, _i18n.__)('Edit the template to move, delete, or make further changes to this block.');
  if (!canEditTemplates) {
    helpText = (0, _i18n.__)('Only users with permissions to edit the template can move or delete this block');
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.__unstableBlockSettingsMenuFirstItem, {
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
        onClick: () => {
          onNavigateToEntityRecord({
            postId: entity.id,
            postType: entity.type
          });
        },
        disabled: !canEditTemplates,
        children: isPattern ? (0, _i18n.__)('Edit pattern') : (0, _i18n.__)('Edit template')
      })
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalText, {
      variant: "muted",
      as: "p",
      className: "editor-content-only-settings-menu__description",
      children: helpText
    })]
  });
}
function TemplateLockContentOnlyMenuItems({
  clientId,
  onClose
}) {
  const {
    contentLockingParent
  } = (0, _data.useSelect)(select => {
    const {
      getContentLockingParent
    } = (0, _lockUnlock.unlock)(select(_blockEditor.store));
    return {
      contentLockingParent: getContentLockingParent(clientId)
    };
  }, [clientId]);
  const blockDisplayInformation = (0, _blockEditor.useBlockDisplayInformation)(contentLockingParent);
  const blockEditorActions = (0, _data.useDispatch)(_blockEditor.store);
  if (!blockDisplayInformation?.title) {
    return null;
  }
  const {
    modifyContentLockBlock
  } = (0, _lockUnlock.unlock)(blockEditorActions);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.__unstableBlockSettingsMenuFirstItem, {
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
        onClick: () => {
          modifyContentLockBlock(contentLockingParent);
          onClose();
        },
        children: (0, _i18n._x)('Unlock', 'Unlock content locked blocks')
      })
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalText, {
      variant: "muted",
      as: "p",
      className: "editor-content-only-settings-menu__description",
      children: (0, _i18n.__)('Temporarily unlock the parent block to edit, delete or make further changes to this block.')
    })]
  });
}
function ContentOnlySettingsMenu() {
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.BlockSettingsMenuControls, {
    children: ({
      selectedClientIds,
      onClose
    }) => selectedClientIds.length === 1 && /*#__PURE__*/(0, _jsxRuntime.jsx)(ContentOnlySettingsMenuItems, {
      clientId: selectedClientIds[0],
      onClose: onClose
    })
  });
}
//# sourceMappingURL=content-only-settings-menu.js.map