"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = AddNewItem;
var _components = require("@wordpress/components");
var _i18n = require("@wordpress/i18n");
var _data = require("@wordpress/data");
var _coreData = require("@wordpress/core-data");
var _element = require("@wordpress/element");
var _icons = require("@wordpress/icons");
var _router = require("@wordpress/router");
var _url = require("@wordpress/url");
var _sidebarNavigationItem = _interopRequireDefault(require("../sidebar-navigation-item"));
var _defaultViews = require("./default-views");
var _lockUnlock = require("../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useLocation,
  useHistory
} = (0, _lockUnlock.unlock)(_router.privateApis);
function AddNewItemModalContent({
  type,
  setIsAdding
}) {
  const history = useHistory();
  const {
    path
  } = useLocation();
  const {
    saveEntityRecord
  } = (0, _data.useDispatch)(_coreData.store);
  const [title, setTitle] = (0, _element.useState)('');
  const [isSaving, setIsSaving] = (0, _element.useState)(false);
  const defaultViews = (0, _defaultViews.useDefaultViews)({
    postType: type
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("form", {
    onSubmit: async event => {
      event.preventDefault();
      setIsSaving(true);
      const {
        getEntityRecords
      } = (0, _data.resolveSelect)(_coreData.store);
      let dataViewTaxonomyId;
      const dataViewTypeRecords = await getEntityRecords('taxonomy', 'wp_dataviews_type', {
        slug: type
      });
      if (dataViewTypeRecords && dataViewTypeRecords.length > 0) {
        dataViewTaxonomyId = dataViewTypeRecords[0].id;
      } else {
        const record = await saveEntityRecord('taxonomy', 'wp_dataviews_type', {
          name: type
        });
        if (record && record.id) {
          dataViewTaxonomyId = record.id;
        }
      }
      const savedRecord = await saveEntityRecord('postType', 'wp_dataviews', {
        title,
        status: 'publish',
        wp_dataviews_type: dataViewTaxonomyId,
        content: JSON.stringify(defaultViews[0].view)
      });
      history.navigate((0, _url.addQueryArgs)(path, {
        activeView: savedRecord.id,
        isCustom: 'true'
      }));
      setIsSaving(false);
      setIsAdding(false);
    },
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
      spacing: "5",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.TextControl, {
        __next40pxDefaultSize: true,
        __nextHasNoMarginBottom: true,
        label: (0, _i18n.__)('Name'),
        value: title,
        onChange: setTitle,
        placeholder: (0, _i18n.__)('My view'),
        className: "patterns-create-modal__name-input"
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalHStack, {
        justify: "right",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
          __next40pxDefaultSize: true,
          variant: "tertiary",
          onClick: () => {
            setIsAdding(false);
          },
          children: (0, _i18n.__)('Cancel')
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
          __next40pxDefaultSize: true,
          variant: "primary",
          type: "submit",
          "aria-disabled": !title || isSaving,
          isBusy: isSaving,
          children: (0, _i18n.__)('Create')
        })]
      })]
    })
  });
}
function AddNewItem({
  type
}) {
  const [isAdding, setIsAdding] = (0, _element.useState)(false);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebarNavigationItem.default, {
      icon: _icons.plus,
      onClick: () => {
        setIsAdding(true);
      },
      className: "dataviews__siderbar-content-add-new-item",
      children: (0, _i18n.__)('New view')
    }), isAdding && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Modal, {
      title: (0, _i18n.__)('Add new view'),
      onRequestClose: () => {
        setIsAdding(false);
      },
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(AddNewItemModalContent, {
        type: type,
        setIsAdding: setIsAdding
      })
    })]
  });
}
//# sourceMappingURL=add-new-view.js.map