@univerjs-pro/sheets-pivot-ui 1.5.2 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cjs/index.js CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@univerjs-pro/engine-pivot"),t=require("@univerjs-pro/sheets-pivot"),n=require("@univerjs/core"),r=require("@univerjs/sheets"),i=require("@univerjs/ui"),a=require("@univerjs/sheets-ui"),o=require("rxjs"),s=require("@univerjs/design"),c=require("@univerjs-pro/license"),l=require("@univerjs/engine-render"),u=require("@univerjs/icons"),d=require("react/jsx-runtime"),f=require("react"),p=require("@univerjs/engine-formula"),m=require("@univerjs/sheets-formula-ui"),h=require("@univerjs/docs"),g=require("@univerjs/docs-ui");const _=`UNIVER_SHEET_PIVOT_PANEL`,v=`UNIVER_SHEET_PIVOT_PANEL_ID`,y=`UNIVER_SHEET_PIVOT_FILTER_PANEL`,b=`UNIVER_SHEET_PIVOT_FILTER_PANEL_ID`,x=`UNIVER_SHEET_PIVOT_FIELD_SETTING`,S=`UNIVER_SHEET_PIVOT_FIELD_SETTING_ID`,C=`UNIVER_SHEET_PIVOT_FIELD_NUMFMT`,w=`UNIVER_SHEET_PIVOT_FIELD_NUMFMT_ID`,T=`UNIVER_SHEET_PIVOT_SHOW_DATA_AS_DIALOG`,E=`UNIVER_SHEET_PIVOT_VALUE_FILTER_DIALOG`,D=`UNIVER_SHEET_PIVOT_EXCEL_DATE_GROUP_DIALOG`,O=`UNIVER_SHEET_PIVOT_NUMBER_GROUP_DIALOG`,k=`UNIVER_SHEET_PIVOT_MANUAL_GROUP_DIALOG`,A=`univer.sheet.pivot-table-range-selector`,j=`UNIVER_SHEET_PIVOT_MOBILE_ACTION_PANEL`,M=`UNIVER_SHEET_PIVOT_MOBILE_ACTION_PANEL_ID`,N={type:n.CommandType.OPERATION,id:`sheet.operation.open-pivot-table-range-selector-panel`,async handler(i){let a=i.get(n.IUniverInstanceService),o=i.get(n.ICommandService),s=(0,r.getSheetCommandTarget)(a),c=i.get(n.LocaleService);if(!s)return!1;let{unitId:l,subUnitId:u,worksheet:d,workbook:f}=s,p=i.get(r.SheetsSelectionsService).getCurrentLastSelection(),m=(p==null?void 0:p.range)??{startRow:0,endRow:0,startColumn:0,endColumn:0,rangeType:n.RANGE_TYPE.NORMAL},h=await P(i,l,u,(0,r.isSingleCellSelection)(p)?(0,r.expandToContinuousRange)(m,{up:!0,left:!0,right:!0,down:!0},d):m,d.getName());if(!h)return!1;let g=f.getSheets().find(e=>e.getName()===h.sourceSheetName);if(!g)return!1;let _;if(h.positionType===t.PositionType.New)_={row:0,col:0,unitId:l,subUnitId:(0,n.generateRandomId)(),sheetName:f.generateNewSheetName(`${c.t(`sheets-pivot-ui.sheet`)}`)};else{if(!h.positionRange||!h.positionSheetName)return!1;let e=f.getSheets().find(e=>e.getName()===h.positionSheetName);if(!e)return!1;_={row:h.positionRange.startRow,col:h.positionRange.startColumn,unitId:l,subUnitId:e.getSheetId(),sheetName:e.getName()}}let v=(0,e.generateHexNumber)(8);return await o.executeCommand(t.AddPivotTableCommand.id,{pivotTableId:v,pivotTableConfig:{targetCellInfo:_,sourceRangeInfo:{range:h.sourceRange,unitId:l,sheetName:h.sourceSheetName,subUnitId:g.getSheetId()}},positionType:h.positionType})?{unitId:_.unitId,subUnitId:_.subUnitId,pivotTableId:v}:!1}};async function P(e,t,r,a,o){let s=e.get(i.IDialogService),c=e.get(n.LocaleService);return new Promise(e=>{let n,i={unitId:t,subUnitId:r,sourceRange:a,positionRange:null,sourceSheetName:o,positionSheetName:o,onConfirm:t=>{e(t),n==null||n.dispose()},onCancel:()=>{e(null),n==null||n.dispose()}};n=s.open({id:A,title:{title:c.t(`sheets-pivot-ui.dialog.source.insertPivotTable`)},draggable:!0,mask:!1,maskClosable:!1,children:{label:{name:A,props:i}},width:400,onClose:()=>{n.dispose()}})})}const F={type:n.CommandType.OPERATION,id:`sheet.operation.set-pivot-date-group`,async handler(e,r){if(!r)return!1;let i=I(e);return i?e.get(n.ICommandService).executeCommand(t.SetPivotDateGroupCommand.id,{unitId:i.unitId,subUnitId:i.subUnitId,pivotTableId:i.pivotTableId,tableFieldId:i.tableFieldId,dateType:r.dateType}):!1}};function I(i,a=e.PivotDateGroupingMode.Legacy){let o=i.get(n.IUniverInstanceService),s=(0,r.getSheetCommandTarget)(o);if(!s)return;let c=i.get(r.SheetsSelectionsService).getCurrentLastSelection(),l=c==null?void 0:c.primary;if(!l)return;let{unitId:u,subUnitId:d}=s,f=l.startRow,p=l.startColumn,m=i.get(t.SheetsPivotTableAdaptorModel).getPivotTableIdByCell(u,d,f,p);if(!m)return;let h=L(i,u,d,m,f,p);if(!h)return;let g=i.get(t.SheetsPivotTableConfigModel),_=g.getPivotTableConfig(u,d,m);if((0,e.getPivotDateGroupingMode)(_==null?void 0:_.fieldsConfig.options)!==a)return;let v=g.getCollection(u,m),y=(0,t.getPivotDateGroupContext)(_==null?void 0:_.fieldsConfig,v,h);if(y)return{unitId:u,subUnitId:d,pivotTableId:m,tableFieldId:h,row:f,col:p,dateGroupContext:y}}function L(e,n,r,i,a,o){let s=e.get(t.SheetsPivotTableAdaptorModel).getPivotItemRenderInfoCache(n,r,i);if(!s)return;let c=e.get(t.SheetsPivotTableAdaptorModel).getPivotTableCellData(n,r,a,o),l=(0,t.resolvePivotSheetCell)(a,o,c,s);return l==null?void 0:l.tableFieldId}function R(n){let r=I(n,e.PivotDateGroupingMode.ExcelLike);if(!r)return;let i=n.get(t.SheetsPivotTableConfigModel),a=i.getPivotTableConfig(r.unitId,r.subUnitId,r.pivotTableId),o=a==null?void 0:a.fieldsConfig.dimension[r.tableFieldId];if(!o)return;let s=i.getCollection(r.unitId,r.pivotTableId),c=(r.dateGroupContext.area===e.PivotTableFiledAreaEnum.Row?a.fieldsConfig.rowFields:a.fieldsConfig.columnFields).flatMap(t=>{let n=a.fieldsConfig.dimension[t],i=n?s==null?void 0:s.fields[n.dataFieldId]:void 0;return i&&(0,e.isDateGroupFieldJSON)(i)&&i.originalFieldId===r.dateGroupContext.originalDataFieldId&&i.excelLike?[i.excelLike]:[]}),l=c[0],u=l?{units:c.map(e=>e.unit),autoStart:l.autoStart,autoEnd:l.autoEnd,...l.start===void 0?{}:{start:l.start},...l.end===void 0?{}:{end:l.end},...l.daysInterval===void 0?{}:{daysInterval:l.daysInterval}}:void 0;return{...r,targetFieldName:o.displayName,grouping:u}}const z={id:`sheet.operation.set-pivot-excel-date-group`,type:n.CommandType.OPERATION,async handler(e){let r=R(e);if(!r)return!1;let a=e.get(t.SheetsPivotTableConfigModel),o=a.getPivotTableConfig(r.unitId,r.subUnitId,r.pivotTableId),s=a.getCollection(r.unitId,r.pivotTableId),c=e.get(i.IDialogService),l=e.get(n.LocaleService),u=e.get(n.IUniverInstanceService).getUnit(r.unitId),d=(u==null?void 0:u.getDateSystem())??n.DateSystem.Date1900,f=null,p=!1,m=!1,h=()=>{p||(p=!0,f==null||f.dispose(),f=null)},g=async i=>{if(!(m||p)){m=!0;try{let c=R(e);if(!c||c.unitId!==r.unitId||c.subUnitId!==r.subUnitId||c.pivotTableId!==r.pivotTableId||c.tableFieldId!==r.tableFieldId||a.getPivotTableConfig(r.unitId,r.subUnitId,r.pivotTableId)!==o||a.getCollection(r.unitId,r.pivotTableId)!==s)return;await e.get(n.ICommandService).executeCommand(t.SetPivotExcelDateGroupCommand.id,{unitId:r.unitId,subUnitId:r.subUnitId,pivotTableId:r.pivotTableId,tableFieldId:r.tableFieldId,grouping:{...i,units:i.units.concat()}})}catch{}finally{h()}}};try{f=c.open({id:`UNIVER_SHEET_PIVOT_EXCEL_DATE_GROUP_DIALOG_ID`,title:{title:l.t(`sheets-pivot-ui.dateGroup.excelLikeTitle`)},draggable:!0,mask:!0,maskClosable:!1,width:440,children:{label:{name:D,props:{targetFieldName:r.targetFieldName,dateSystem:d,grouping:r.grouping,onConfirm:g,onCancel:h}}},onClose:h})}catch{return h(),!1}return!0}};function B(e){return e.map(e=>({...e,members:e.members.map(e=>({...e}))}))}function V(t){let r;try{r=JSON.parse(t)}catch{return}if(!(!Array.isArray(r)||r.length<1||typeof r[0]!=`string`))return r[0]===n.RecordValueType.Blank?e.GlobalConfig.BLANK_PLACEHOLDER:String(r[1])}function ee(i,a){var o;let s=()=>({error:`invalidSelection`}),c=(0,r.getSheetCommandTarget)(i.get(n.IUniverInstanceService)),l=i.get(r.SheetsSelectionsService).getCurrentSelections();if(!c||!l.length)return s();let{unitId:u,subUnitId:d}=c,f=l[0].primary;if(!f)return s();let p=i.get(t.SheetsPivotTableAdaptorModel),m=p.getPivotTableIdByCell(u,d,f.startRow,f.startColumn),h=i.get(t.SheetsPivotTableConfigModel),g=m&&h.getPivotTableConfig(u,d,m),_=m&&h.getCollection(u,m),v=m&&p.getPivotItemRenderInfoCache(u,d,m);if(!m||!g||!_||!v)return s();let y=(0,t.resolvePivotSheetCell)(f.startRow,f.startColumn,p.getPivotTableCellData(u,d,f.startRow,f.startColumn),v),b=y==null?void 0:y.tableFieldId,x=b&&g.fieldsConfig.dimension[b];if(!b||!x||!(g.fieldsConfig.rowFields.includes(b)||g.fieldsConfig.columnFields.includes(b)))return s();let S=_.fields[x.dataFieldId];if(!S)return s();let C=S.dataFieldType===e.PivotDataFieldTypeEnum.textGroupField?S:void 0;if(!C&&S.dataFieldType!==e.PivotDataFieldTypeEnum.defaultNormalField)return s();let w=(C==null?void 0:C.originalFieldId)??S.id;if(((o=_.fields[w])==null?void 0:o.dataFieldType)!==e.PivotDataFieldTypeEnum.defaultNormalField)return s();let T=B((C==null?void 0:C.grouping.groups)??[]),E=/* @__PURE__ */ new Map,D=/* @__PURE__ */ new Set,O=0;for(let n of l){let{startRow:r,endRow:i,startColumn:a,endColumn:o}=n.range;if(O+=(i-r+1)*(o-a+1),O>512)return s();for(let n=r;n<=i;n++)for(let r=a;r<=o;r++){if(p.getPivotTableIdByCell(u,d,n,r)!==m)return s();let i=(0,t.resolvePivotSheetCell)(n,r,p.getPivotTableCellData(u,d,n,r),v),a=i==null?void 0:i.info;if(!i||i.area!==`rowRanges`&&i.area!==`colRanges`||i.tableFieldId!==b||!a||a.isValue||a.isSubtotal||a.isBottomTotal||a.isTopTotal||a.rowType&&a.rowType!==`item`||a.level<1)return s();let o=a.paths[a.level-1];if(o===void 0)return s();if(C){let e;try{e=JSON.parse(o)}catch{return s()}if(!Array.isArray(e)||e.length!==2||typeof e[1]!=`string`)return s();if(e[0]===`group`){if(!T.some(t=>t.id===e[1]))return s();D.add(e[1]);continue}if(e[0]!==`member`)return s();let t=V(e[1]);if(t===void 0)return s();E.set(e[1],{path:t,memberKey:e[1]})}else{var k;let t=(k=p.getPivotTableCellData(u,d,n,r))==null?void 0:k.v,i=t==null||t===``?e.GlobalConfig.BLANK_PLACEHOLDER:String(t);E.set(o,{path:i,tupleKey:o})}}}return a===`group`&&(E.size<2||D.size>0)||a!==`group`&&(E.size>0||D.size!==1)?s():{context:{unitId:u,subUnitId:d,pivotTableId:m,tableFieldId:b,sourceFieldId:w,targetFieldName:x.displayName,groups:T,memberPaths:Array.from(E.values()),selectedGroupId:Array.from(D)[0]}}}function te(e,t){return ee(e,t).context}function H(e,t){e.get(i.IMessageService).show({type:s.MessageType.Error,content:e.get(n.LocaleService).t(`sheets-pivot-ui.manualGroup.${t}`)})}function ne(e,t,n){let r=te(e,t);return!!r&&r.unitId===n.unitId&&r.subUnitId===n.subUnitId&&r.pivotTableId===n.pivotTableId&&r.tableFieldId===n.tableFieldId&&r.sourceFieldId===n.sourceFieldId&&r.selectedGroupId===n.selectedGroupId&&JSON.stringify(r.memberPaths)===JSON.stringify(n.memberPaths)&&JSON.stringify(r.groups)===JSON.stringify(n.groups)}async function re(e,r){let a=ee(e,r),o=a.context;if(!o)return H(e,a.error),!1;let s=e.get(t.SheetsPivotTableConfigModel),c=s.getPivotTableConfig(o.unitId,o.subUnitId,o.pivotTableId),l=s.getCollection(o.unitId,o.pivotTableId),u=[];if(r===`group`){try{let n=(await e.get(t.SheetsPivotRPCService).getPivotDisplayConfig({unitId:o.unitId,subUnitId:o.subUnitId,pivotTableId:o.pivotTableId,params:{type:t.PivotDisplayDataType.ManualGroupMembers,manualGroupMembers:{sourceFieldId:o.sourceFieldId,paths:o.memberPaths}}})).result.manualGroupMembers;if(!n||n.error||n.members.length<2)return H(e,(n==null?void 0:n.error)??`invalidSelection`),!1;u=n.members}catch{return H(e,`invalidSelection`),!1}if(!ne(e,r,o)||s.getPivotTableConfig(o.unitId,o.subUnitId,o.pivotTableId)!==c||s.getCollection(o.unitId,o.pivotTableId)!==l)return!1}let d=e.get(i.IDialogService),f=e.get(n.LocaleService),p=o.groups.find(e=>e.id===o.selectedGroupId),m=(p==null?void 0:p.name)??f.t(`sheets-pivot-ui.manualGroup.defaultName`,String(o.groups.length+1));if(!p){let e=o.groups.length+1;for(;o.groups.some(e=>e.name===m);)e++,m=f.t(`sheets-pivot-ui.manualGroup.defaultName`,String(e))}let h=null,g=!1,_=!1,v=()=>{g||(g=!0,h==null||h.dispose(),h=null)},y=async i=>{if(_||g)return;let a=i.trim();if(!a||o.groups.some(e=>e.name===a&&e.id!==o.selectedGroupId))H(e,`invalidSelection`);else{_=!0;try{if(!ne(e,r,o)||s.getPivotTableConfig(o.unitId,o.subUnitId,o.pivotTableId)!==c||s.getCollection(o.unitId,o.pivotTableId)!==l)return;let i=r===`group`?o.groups.concat({id:(0,n.generateRandomId)(),name:a,members:u.map(e=>({...e}))}):o.groups.map(e=>e.id===o.selectedGroupId?{...e,name:a}:e);await e.get(n.ICommandService).executeCommand(t.SetPivotManualGroupsCommand.id,{unitId:o.unitId,subUnitId:o.subUnitId,pivotTableId:o.pivotTableId,sourceFieldId:o.sourceFieldId,groups:i})}catch{}finally{v()}}};try{h=d.open({id:`UNIVER_SHEET_PIVOT_MANUAL_GROUP_DIALOG_ID`,title:{title:f.t(r===`group`?`sheets-pivot-ui.manualGroup.groupTitle`:`sheets-pivot-ui.manualGroup.renameTitle`)},draggable:!0,mask:!0,maskClosable:!1,width:400,children:{label:{name:k,props:{name:m,onConfirm:y,onCancel:v}}},onClose:v})}catch{return v(),!1}return!0}const ie={id:`sheet.operation.set-pivot-manual-group`,type:n.CommandType.OPERATION,handler:e=>re(e,`group`)},ae={id:`sheet.operation.rename-pivot-manual-group`,type:n.CommandType.OPERATION,handler:e=>re(e,`rename`)},oe={id:`sheet.operation.ungroup-pivot-manual-group`,type:n.CommandType.OPERATION,async handler(e){let r=ee(e,`ungroup`),i=r.context;if(!i)return H(e,r.error),!1;let a=e.get(t.SheetsPivotTableConfigModel),o=a.getPivotTableConfig(i.unitId,i.subUnitId,i.pivotTableId),s=a.getCollection(i.unitId,i.pivotTableId);return!ne(e,`ungroup`,i)||a.getPivotTableConfig(i.unitId,i.subUnitId,i.pivotTableId)!==o||a.getCollection(i.unitId,i.pivotTableId)!==s?!1:e.get(n.ICommandService).executeCommand(t.SetPivotManualGroupsCommand.id,{unitId:i.unitId,subUnitId:i.subUnitId,pivotTableId:i.pivotTableId,sourceFieldId:i.sourceFieldId,groups:i.groups.filter(e=>e.id!==i.selectedGroupId)})}};function se(e){return typeof e.autoStart==`boolean`&&typeof e.autoEnd==`boolean`&&Number.isFinite(e.interval)&&e.interval>0&&(e.start===void 0||Number.isFinite(e.start))&&(e.end===void 0||Number.isFinite(e.end))&&(e.autoStart||e.start!==void 0)&&(e.autoEnd||e.end!==void 0)&&(e.start===void 0||e.end===void 0||e.start<=e.end)}function ce(t,n){if(t.dataFieldType===e.PivotDataFieldTypeEnum.numberGroupField){let r=t,i=n.fields[r.originalFieldId];return!i||i.dataFieldType!==e.PivotDataFieldTypeEnum.defaultNormalField||i.fieldDataType!==e.PivotDataFieldDataTypeEnum.number?void 0:{sourceFieldId:r.originalFieldId,grouping:{...r.grouping}}}if(t.dataFieldType===e.PivotDataFieldTypeEnum.defaultNormalField&&t.fieldDataType===e.PivotDataFieldDataTypeEnum.number)return{sourceFieldId:t.id}}function le(e){var i;let a=(0,r.getSheetCommandTarget)(e.get(n.IUniverInstanceService)),o=(i=e.get(r.SheetsSelectionsService).getCurrentLastSelection())==null?void 0:i.primary;if(!a||!o)return;let{unitId:s,subUnitId:c}=a,l=o.startRow,u=o.startColumn,d=e.get(t.SheetsPivotTableAdaptorModel),f=d.getPivotTableIdByCell(s,c,l,u);if(!f)return;let p=d.getPivotItemRenderInfoCache(s,c,f);if(!p)return;let m=(0,t.resolvePivotSheetCell)(l,u,d.getPivotTableCellData(s,c,l,u),p),h=m==null?void 0:m.tableFieldId;if(!h||m.area!==`rowRanges`&&m.area!==`colRanges`)return;let g=e.get(t.SheetsPivotTableConfigModel),_=g.getPivotTableConfig(s,c,f),v=g.getCollection(s,f),y=_==null?void 0:_.fieldsConfig.dimension[h];if(!y||!v||!(_.fieldsConfig.rowFields.includes(h)||_.fieldsConfig.columnFields.includes(h)))return;let b=v.fields[y.dataFieldId];if(!b)return;let x=ce(b,v);if(x)return{unitId:s,subUnitId:c,pivotTableId:f,tableFieldId:h,targetFieldName:y.displayName,...x}}const ue={id:`sheet.operation.set-pivot-number-group`,type:n.CommandType.OPERATION,async handler(e){let r=le(e);if(!r)return!1;let a=e.get(t.SheetsPivotTableConfigModel),s=a.getCollection(r.unitId,r.pivotTableId),c=a.getPivotTableConfig(r.unitId,r.subUnitId,r.pivotTableId),l=e.get(n.IUniverInstanceService),u=()=>{let t=le(e);return!!t&&t.unitId===r.unitId&&t.subUnitId===r.subUnitId&&t.pivotTableId===r.pivotTableId&&t.tableFieldId===r.tableFieldId&&t.sourceFieldId===r.sourceFieldId&&JSON.stringify(t.grouping)===JSON.stringify(r.grouping)},d=e.get(i.IDialogService),f=e.get(n.LocaleService),p=null,m=!1,h=!1,g=new o.Subject,_=async()=>{if(!m&&u())try{let n=(await e.get(t.SheetsPivotRPCService).getPivotDisplayConfig({unitId:r.unitId,subUnitId:r.subUnitId,pivotTableId:r.pivotTableId,params:{type:t.PivotDisplayDataType.NumberGroupDefaults,numberGroupDefaults:{tableFieldId:r.tableFieldId}}})).result.numberGroupDefaults;return!m&&u()&&n&&se(n)?n:void 0}catch{return}},v=()=>{m||(m=!0,g.next(),g.complete(),p==null||p.dispose(),p=null)},y=await _();if(!y)return v(),!1;let b=l.unitDisposed$.pipe((0,o.filter)(e=>e.getUnitId()===r.unitId),(0,o.tap)(v)),x=(0,o.defer)(()=>{if(m||!l.getUnit(r.unitId))return v(),o.EMPTY;let e=a.watchPivotTableChanges(r.unitId,r.subUnitId,r.pivotTableId).pipe((0,o.debounceTime)(0),(0,o.map)(()=>void 0));return(0,o.merge)((0,o.of)(y),e).pipe((0,o.switchMap)(e=>e&&a.getCollection(r.unitId,r.pivotTableId)===s&&a.getPivotTableConfig(r.unitId,r.subUnitId,r.pivotTableId)===c?(0,o.of)(e):(0,o.concat)((0,o.of)(void 0),_())),(0,o.takeUntil)((0,o.merge)(g,b)))}),S=async i=>{if(!(h||m)){h=!0;try{if(!se(i)||!u())return;await e.get(n.ICommandService).executeCommand(t.SetPivotNumberGroupCommand.id,{unitId:r.unitId,subUnitId:r.subUnitId,pivotTableId:r.pivotTableId,tableFieldId:r.tableFieldId,grouping:{...i}})}catch{}finally{v()}}};try{p=d.open({id:`UNIVER_SHEET_PIVOT_NUMBER_GROUP_DIALOG_ID`,title:{title:f.t(`sheets-pivot-ui.numberGroup.title`)},draggable:!0,mask:!0,maskClosable:!1,width:400,children:{label:{name:O,props:{targetFieldName:r.targetFieldName,grouping:r.grouping,defaults$:x,onConfirm:S,onCancel:v}}},onClose:v})}catch{return v(),!1}return!0}},de=[{type:e.PivotShowAsTypeEnum.normal,localeKey:`sheets-pivot-ui.showValuesAs.normal`,requiresParameters:!1,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.percentOfGrandTotal,localeKey:`sheets-pivot-ui.showValuesAs.percentOfGrandTotal`,requiresParameters:!1,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.percentOfRowTotal,localeKey:`sheets-pivot-ui.showValuesAs.percentOfRowTotal`,requiresParameters:!1,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.percentOfColumnTotal,localeKey:`sheets-pivot-ui.showValuesAs.percentOfColumnTotal`,requiresParameters:!1,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.percentOfParentRowTotal,localeKey:`sheets-pivot-ui.showValuesAs.percentOfParentRowTotal`,requiresParameters:!1,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.percentOfParentColumnTotal,localeKey:`sheets-pivot-ui.showValuesAs.percentOfParentColumnTotal`,requiresParameters:!1,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.index,localeKey:`sheets-pivot-ui.showValuesAs.index`,requiresParameters:!1,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.percentOfParent,localeKey:`sheets-pivot-ui.showValuesAs.percentOfParent`,requiresParameters:!0,baseItemMode:`none`,parentFieldOnly:!0},{type:e.PivotShowAsTypeEnum.percentOf,localeKey:`sheets-pivot-ui.showValuesAs.percentOf`,requiresParameters:!0,baseItemMode:`item`},{type:e.PivotShowAsTypeEnum.differenceFrom,localeKey:`sheets-pivot-ui.showValuesAs.differenceFrom`,requiresParameters:!0,baseItemMode:`item-or-relative`},{type:e.PivotShowAsTypeEnum.percentDifferenceFrom,localeKey:`sheets-pivot-ui.showValuesAs.percentDifferenceFrom`,requiresParameters:!0,baseItemMode:`item-or-relative`},{type:e.PivotShowAsTypeEnum.runningTotal,localeKey:`sheets-pivot-ui.showValuesAs.runningTotal`,requiresParameters:!0,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.percentRunningTotal,localeKey:`sheets-pivot-ui.showValuesAs.percentRunningTotal`,requiresParameters:!0,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.rankAscending,localeKey:`sheets-pivot-ui.showValuesAs.rankAscending`,requiresParameters:!0,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.rankDescending,localeKey:`sheets-pivot-ui.showValuesAs.rankDescending`,requiresParameters:!0,baseItemMode:`none`}],fe={type:n.CommandType.OPERATION,id:`sheet.operation.set-pivot-show-data-as`,async handler(e,r){let a=r&&de.find(e=>e.type===r.type);if(!a)return!1;let o=me(e);if(!o)return!1;if(!a.requiresParameters)return e.get(n.ICommandService).executeCommand(t.UpdatePivotFieldSettingCommand.id,{unitId:o.unitId,subUnitId:o.subUnitId,pivotTableId:o.pivotTableId,tableFieldId:o.tableFieldId,showDataAs:{type:a.type,baseFieldId:``,baseItem:``},tableFieldInfo:o.tableFieldInfo,dataFieldInfo:o.dataFieldInfo});let s=he(e,o);if(!s)return!1;let c=pe(s.config,s.collection,a);if(c.length===0)return!1;let l={};if(a.baseItemMode!==`none`){try{l=await ge(e,s.config,o.pivotTableId,c)}catch{return!1}if(!c.some(e=>l[e]&&(a.baseItemMode===`item-or-relative`||l[e].items.length>0)))return!1}let u=e.get(i.IDialogService),d=e.get(n.LocaleService),f=null,p=!1,m=!1,h=()=>{p||(p=!0,f==null||f.dispose(),f=null)};try{f=u.open({id:`UNIVER_SHEET_PIVOT_SHOW_DATA_AS_DIALOG_ID`,title:{title:d.t(`sheets-pivot-ui.showValuesAs.dialogTitle`)},draggable:!0,mask:!0,maskClosable:!1,width:400,children:{label:{name:T,props:{unitId:o.unitId,subUnitId:o.subUnitId,pivotTableId:o.pivotTableId,tableFieldId:o.tableFieldId,type:a.type,baseItems:l,onCancel:h,onConfirm:async r=>{if(!m){m=!0;try{let s=he(e,o);if(!s||!_e(s.config,s.collection,a,r))return;if(a.baseItemMode!==`none`&&r.baseItemType===void 0){var i;if(!((i=(await ge(e,s.config,s.context.pivotTableId,[r.baseFieldId]))[r.baseFieldId])!=null&&i.items.includes(r.baseItem)))return}await e.get(n.ICommandService).executeCommand(t.UpdatePivotFieldSettingCommand.id,{unitId:s.context.unitId,subUnitId:s.context.subUnitId,pivotTableId:s.context.pivotTableId,tableFieldId:s.context.tableFieldId,showDataAs:r,tableFieldInfo:s.context.tableFieldInfo,dataFieldInfo:s.context.dataFieldInfo})}catch{}finally{h()}}}}}},onClose:h})}catch{return h(),!1}return!0}};function pe(e,t,n){let{rowFields:r,columnFields:i,dimension:a}=e.fieldsConfig;return(n.parentFieldOnly?[...r.slice(0,-1),...i.slice(0,-1)]:[...r,...i]).filter(e=>{let n=a[e];return!!(n&&(t.fields[n.dataFieldId]||t.customFields.find(e=>e.id===n.dataFieldId)))})}function me(e){var i,a;let o=(0,r.getSheetCommandTarget)(e.get(n.IUniverInstanceService)),s=(i=e.get(r.SheetsSelectionsService).getCurrentLastSelection())==null?void 0:i.primary;if(!o||!s)return;let{unitId:c,subUnitId:l}=o,u=s.startRow,d=s.startColumn,f=e.get(t.SheetsPivotTableAdaptorModel),p=f.getPivotTableIdByCell(c,l,u,d);if(!p)return;let m=e.get(t.SheetsPivotTableConfigModel).getPivotTableConfig(c,l,p),h=f.getPivotItemRenderInfoCache(c,l,p),g=f.getPivotTableCellData(c,l,u,d);if(!m||!h)return;let _=(0,t.resolvePivotSheetCell)(u,d,g,h);if(!_)return;let v=(0,t.resolvePivotValueFieldId)(_,m.fieldsConfig.valueFields);if(v)return(a=he(e,{unitId:c,subUnitId:l,pivotTableId:p,tableFieldId:v}))==null?void 0:a.context}function he(e,n){let{unitId:r,subUnitId:i,pivotTableId:a,tableFieldId:o}=n,s=e.get(t.SheetsPivotTableConfigModel),c=s.getPivotTableConfig(r,i,a),l=s.getCollection(r,a),u=c==null?void 0:c.fieldsConfig.measure[o];if(!c||!l||!u||!c.fieldsConfig.valueFields.includes(u.id))return;let d=l.fields[u.dataFieldId]??l.customFields.find(e=>e.id===u.dataFieldId);if(!d)return;let f={...u.showDataAs};return{config:c,collection:l,context:{unitId:r,subUnitId:i,pivotTableId:a,tableFieldId:u.id,currentShowDataAs:f,tableFieldInfo:{tableFieldId:u.id,displayName:u.displayName,dataFieldId:u.dataFieldId,format:u.format,subTotalType:u.subtotal,showDataAs:{...f}},dataFieldInfo:{id:d.id,dataFieldId:d.id,name:d.name,type:d.fieldDataType,checked:!0,pivotTableId:a}}}}async function ge(e,n,r,i){let{unitId:a,subUnitId:o}=n.targetCellInfo,s=n.targetCellInfo,c=e.get(t.SheetsPivotTableAdaptorModel).getPivotItemRenderInfoCache(a,o,r);if(!c)throw Error(`[sheets-pivot-ui]: Pivot render cache is unavailable`);return(await Promise.all(i.map(async n=>{try{return[n,(await e.get(t.SheetsPivotRPCService).getPivotDisplayConfig({unitId:a,subUnitId:o,pivotTableId:r,params:{type:t.PivotDisplayDataType.Filter,filter:{row:s.row,col:s.col,tableFieldId:n,pivotItemRenderInfoCache:c}}})).result.filter]}catch{return[n,void 0]}}))).reduce((e,[t,n])=>(n&&(e[t]={items:n.items,itemTypes:n.itemTypes,...n.itemLabels?{itemLabels:n.itemLabels}:{},format:n.format}),e),{})}function _e(t,n,r,i){return i.type!==r.type||!pe(t,n,r).includes(i.baseFieldId)?!1:r.baseItemMode===`none`?i.baseItem===``&&i.baseItemType===void 0:i.baseItemType===void 0?i.baseItem!==``:r.baseItemMode===`item-or-relative`&&i.baseItem===``&&(i.baseItemType===e.PivotShowAsBaseItemTypeEnum.previous||i.baseItemType===e.PivotShowAsBaseItemTypeEnum.next)}function ve(e){return{...e,expected:Array.isArray(e.expected)?e.expected.concat():e.expected}}function ye(e,n){let r=e.get(t.SheetsPivotTableConfigModel).getPivotTableConfig(n.unitId,n.subUnitId,n.pivotTableId);if(r&&r.fieldsConfig.dimension[n.targetFieldId]&&(r.fieldsConfig.rowFields.includes(n.targetFieldId)||r.fieldsConfig.columnFields.includes(n.targetFieldId)))return r}function be(e){var i,a;let o=(0,r.getSheetCommandTarget)(e.get(n.IUniverInstanceService)),s=(i=e.get(r.SheetsSelectionsService).getCurrentLastSelection())==null?void 0:i.primary;if(!o||!s)return;let{unitId:c,subUnitId:l}=o,u=s.startRow,d=s.startColumn,f=e.get(t.SheetsPivotTableAdaptorModel),p=f.getPivotTableIdByCell(c,l,u,d);if(!p)return;let m=e.get(t.SheetsPivotTableConfigModel).getPivotTableConfig(c,l,p),h=f.getPivotItemRenderInfoCache(c,l,p);if(!m||!h)return;let g=(0,t.resolvePivotSheetCell)(u,d,f.getPivotTableCellData(c,l,u,d),h),_=g&&(0,t.resolvePivotValueFilterTarget)(g,m.fieldsConfig);if(!_)return;let v=m.fieldsConfig.dimension[_.targetFieldId],y=m.fieldsConfig.valueFields.flatMap(e=>{let t=m.fieldsConfig.measure[e];return t?[{id:t.id,label:t.displayName}]:[]});if(!v||y.length===0)return;let b=(a=m.fieldsConfig.valueFilter)==null||(a=a.find(e=>e.fieldId===_.targetFieldId))==null?void 0:a.filterInfo;return{unitId:c,subUnitId:l,pivotTableId:p,targetFieldId:_.targetFieldId,targetFieldName:v.displayName,valueFields:y,defaultValueFieldId:_.defaultValueFieldId,...b?{currentFilter:ve(b)}:{}}}const xe={type:n.CommandType.OPERATION,id:`sheet.operation.set-pivot-value-filter`,async handler(r){let a=be(r);if(!a)return!1;let o=r.get(i.IDialogService),s=r.get(n.LocaleService),c=null,l=!1,u=!1,d=()=>{l||(l=!0,c==null||c.dispose(),c=null)},f=async i=>{if(!u){u=!0;try{let o=ye(r,a);if(!o||i&&(!o.fieldsConfig.measure[i.valueFieldId]||!o.fieldsConfig.valueFields.includes(i.valueFieldId)||!(0,e.isValueFilterOperator)(i.operator)))return;await r.get(n.ICommandService).executeCommand(t.SetPivotValueFilterCommand.id,{unitId:a.unitId,subUnitId:a.subUnitId,pivotTableId:a.pivotTableId,fieldId:a.targetFieldId,valueFilterInfo:i?{...i,type:e.PivotFilterTypeEnum.ValueFilter,expected:Array.isArray(i.expected)?i.expected.concat():i.expected}:void 0})}catch{}finally{d()}}};try{c=o.open({id:`UNIVER_SHEET_PIVOT_VALUE_FILTER_DIALOG_ID`,title:{title:s.t(`sheets-pivot-ui.valueFilter.dialogTitle`)},draggable:!0,mask:!0,maskClosable:!1,width:420,children:{label:{name:E,props:{targetFieldId:a.targetFieldId,targetFieldName:a.targetFieldName,valueFields:a.valueFields,defaultValueFieldId:a.defaultValueFieldId,currentFilter:a.currentFilter,onConfirm:e=>f(e),onClear:()=>f(void 0),onCancel:d}}},onClose:d})}catch{return d(),!1}return!0}},Se=`sheet.pivot.context-menu.date-group`,Ce=`sheet.pivot.context-menu.excel-date-group`,we=`sheet.pivot.context-menu.number-group`,Te=`sheet.pivot.context-menu.manual-group`,Ee=`sheet.pivot.context-menu.manual-rename`,De=`sheet.pivot.context-menu.manual-ungroup`,Oe=`sheet.pivot.context-menu.show-data-as`,ke=`sheet.pivot.context-menu.value-filter`,Ae={year:`sheets-pivot-ui.dateGroup.year`,"year-quarter":`sheets-pivot-ui.dateGroup.yearQuarter`,quarter:`sheets-pivot-ui.dateGroup.quarter`,"year-month":`sheets-pivot-ui.dateGroup.yearMonth`,month:`sheets-pivot-ui.dateGroup.month`,"month-date":`sheets-pivot-ui.dateGroup.monthDate`,"year-month-date":`sheets-pivot-ui.dateGroup.yearMonthDate`,date:`sheets-pivot-ui.dateGroup.date`,hour:`sheets-pivot-ui.dateGroup.hour`,minute:`sheets-pivot-ui.dateGroup.minute`,"hour-minute":`sheets-pivot-ui.dateGroup.hourMinute`};function je(e){return{id:N.id,type:i.MenuItemType.BUTTON,icon:`PivotTableIcon`,hidden$:(0,i.getMenuHiddenObservable)(e,n.UniverInstanceType.UNIVER_SHEET),disabled$:(0,a.getObservableWithExclusiveRange$)(e,(0,a.getCurrentRangeDisable$)(e,{workbookTypes:[r.WorkbookEditablePermission],worksheetTypes:[r.WorksheetViewPermission,r.WorksheetPivotTablePermission],rangeTypes:[r.RangeProtectionPermissionViewPoint]})),tooltip:`sheets-pivot-ui.title`,title:`sheets-pivot-ui.title`}}function Me(e){return{id:Se,type:i.MenuItemType.SUBITEMS,title:`sheets-pivot-ui.dateGroup.title`,hidden$:Be(e)}}function Ne(e){return{id:Ce,commandId:z.id,type:i.MenuItemType.BUTTON,title:`sheets-pivot-ui.dateGroup.excelLikeTitle`,hidden$:U(e).pipe((0,o.map)(()=>!R(e)))}}function Pe(e){return{id:we,commandId:ue.id,type:i.MenuItemType.BUTTON,title:`sheets-pivot-ui.numberGroup.title`,hidden$:U(e).pipe((0,o.map)(()=>!le(e)))}}function Fe(e){return{id:Te,commandId:ie.id,type:i.MenuItemType.BUTTON,title:`sheets-pivot-ui.manualGroup.groupTitle`,hidden$:U(e).pipe((0,o.map)(()=>!te(e,`group`)))}}function Ie(e){return{id:Ee,commandId:ae.id,type:i.MenuItemType.BUTTON,title:`sheets-pivot-ui.manualGroup.renameTitle`,hidden$:U(e).pipe((0,o.map)(()=>!te(e,`rename`)))}}function Le(e){return{id:De,commandId:oe.id,type:i.MenuItemType.BUTTON,title:`sheets-pivot-ui.manualGroup.ungroupTitle`,hidden$:U(e).pipe((0,o.map)(()=>!te(e,`ungroup`)))}}function Re(e){return t=>({id:ze(e),commandId:F.id,type:i.MenuItemType.BUTTON,title:Ae[e],params:{dateType:e},disabled$:Ve(t,e),activated$:He(t,e)})}function ze(e){return`sheet.pivot.context-menu.date-group.${e}`}function Be(e){return U(e).pipe((0,o.map)(()=>!I(e)))}function Ve(e,n){return U(e).pipe((0,o.map)(()=>{let r=I(e);return!r||(0,t.isPivotDateGroupTypeDisabled)(r.dateGroupContext,n)}))}function He(e,t){return U(e).pipe((0,o.map)(()=>{let n=I(e);return(n==null?void 0:n.dateGroupContext.currentDateType)===t}))}function U(e){let i=e.get(t.SheetsPivotTableConfigModel),a=()=>{var i;let a=(0,r.getSheetCommandTarget)(e.get(n.IUniverInstanceService)),o=(i=e.get(r.SheetsSelectionsService).getCurrentLastSelection())==null?void 0:i.primary;if(!a||!o)return;let{unitId:s,subUnitId:c}=a,l=e.get(t.SheetsPivotTableAdaptorModel).getPivotTableIdByCell(s,c,o.startRow,o.startColumn);return l?{unitId:s,subUnitId:c,pivotTableId:l}:void 0};return(0,o.merge)(e.get(r.SheetsSelectionsService).selectionMoveEnd$,i.pivotConfigChange$.pipe((0,o.filter)(e=>{let t=a();return(t==null?void 0:t.unitId)===e.unitId&&t.subUnitId===e.subUnitId&&t.pivotTableId===e.pivotTableId})),i.collectionChange$.pipe((0,o.filter)(e=>{let t=a();if(!t||!e.pivotTableIds.includes(t.pivotTableId))return!1;let n=i.getPivotTableConfig(t.unitId,t.subUnitId,t.pivotTableId);return e.unitId===((n==null?void 0:n.sourceRangeInfo.unitId)??t.unitId)}))).pipe((0,o.startWith)(null))}const Ue=t.PIVOT_DATE_GROUP_TYPES.map(e=>({dateType:e,id:ze(e),menuItemFactory:Re(e)}));function We(e){return{id:Oe,type:i.MenuItemType.SUBITEMS,title:`sheets-pivot-ui.showValuesAs.title`,hidden$:qe(e).pipe((0,o.map)(()=>!me(e)))}}function Ge(t){return n=>({id:Ke(t.type),commandId:fe.id,type:i.MenuItemType.BUTTON,title:t.localeKey,params:{type:t.type},activated$:qe(n).pipe((0,o.map)(()=>{let r=me(n);return!!(r&&(r.currentShowDataAs.type??e.PivotShowAsTypeEnum.normal)===t.type)}))})}function Ke(e){return`sheet.pivot.context-menu.show-data-as.${e}`}function qe(e){return e.get(r.SheetsSelectionsService).selectionMoveEnd$.pipe((0,o.startWith)(null))}const Je=de.map(e=>({rule:e,id:Ke(e.type),menuItemFactory:Ge(e)}));function Ye(e){return{id:ke,commandId:xe.id,type:i.MenuItemType.BUTTON,title:`sheets-pivot-ui.valueFilter.title`,hidden$:e.get(r.SheetsSelectionsService).selectionMoveEnd$.pipe((0,o.startWith)(null),(0,o.map)(()=>!be(e)))}}const Xe=Ue.reduce((e,t,n)=>(e[t.id]={order:n+1,menuItemFactory:t.menuItemFactory},e),{}),Ze=Je.reduce((e,t,n)=>(e[t.id]={order:n+1,menuItemFactory:t.menuItemFactory},e),{}),Qe={[i.RibbonDataGroup.ORGANIZATION]:{[N.id]:{order:1,gridLayout:{row:2,column:1},menuItemFactory:je}},[i.ContextMenuPosition.MAIN_AREA]:{[i.ContextMenuGroup.DATA]:{[N.id]:{order:1,menuItemFactory:je},[Se]:{order:2,menuItemFactory:Me,...Xe},[Ce]:{order:3,menuItemFactory:Ne},[we]:{order:4,menuItemFactory:Pe},[Te]:{order:5,menuItemFactory:Fe},[Ee]:{order:6,menuItemFactory:Ie},[De]:{order:7,menuItemFactory:Le},[Oe]:{order:8,menuItemFactory:We,...Ze},[ke]:{order:9,menuItemFactory:Ye}}},[i.ContextMenuPosition.COL_HEADER]:{[i.ContextMenuGroup.DATA]:{[N.id]:{order:1,menuItemFactory:je},[Se]:{order:2,menuItemFactory:Me,...Xe},[Ce]:{order:3,menuItemFactory:Ne},[we]:{order:4,menuItemFactory:Pe},[Te]:{order:5,menuItemFactory:Fe},[Ee]:{order:6,menuItemFactory:Ie},[De]:{order:7,menuItemFactory:Le}}}};var $e=`@univerjs-pro/sheets-pivot-ui`,et=`1.5.2`;const tt={type:n.CommandType.OPERATION,id:`sheet.operation.open-pivot-table-filter-panel`,async handler(e,n){if(!n)return!1;let{row:r,col:a,unitId:o,subUnitId:s,tableFieldId:c,pivotTableId:l}=n,u=e.get(t.SheetsPivotTableAdaptorModel),d=l||u.getPivotTableIdByCell(o,s,r,a);if(!d)return!1;let f=e.get(t.SheetsPivotRPCService),p=u.getPivotItemRenderInfoCache(o,s,d);if(!(p!=null&&p.rangesInfo))return!1;let m=u.getPivotTableCellData(o,s,r,a),h=(await f.getPivotDisplayConfig({unitId:o,subUnitId:s,pivotTableId:d,params:{type:t.PivotDisplayDataType.Filter,filter:{row:r,col:a,tableFieldId:c,cellData:m,pivotItemRenderInfoCache:p}}})).result.filter;if(!h)return!1;let g=e.get(i.IDialogService),_={id:b,draggable:!0,mask:!1,maskClosable:!1,width:336,dialogStyles:{header:{padding:`16px 16px 0`,height:0},body:{padding:`0 16px 16px`}},closable:!1,children:{label:y,unitId:o,subUnitId:s,key:`${r}-${a}-${c||``}`,filterItemInfo:h},onClose:()=>{g.close(b)}};return g.open(_),!0}},nt={type:n.CommandType.OPERATION,id:`sheet.operation.open-pivot-table-field-setting-panel`,handler(e,t){if(!t)return!1;let r=e.get(n.LocaleService),{tableFieldId:a,pivotTableId:o,unitId:s,subUnitId:c,tableFieldInfo:l,dataFieldInfo:u}=t,d=e.get(i.IDialogService),f={id:S,title:{title:r.t(`sheets-pivot-ui.fieldSettings`)},draggable:!0,mask:!1,maskClosable:!1,width:400,children:{label:x,tableFieldId:a,pivotTableId:o,tableFieldInfo:l,dataFieldInfo:u,unitId:s,subUnitId:c,version:Math.random(),key:`field-setting-${a||``}`,closeFn:()=>{d.close(S)}},onClose:()=>{d.close(S)}};return d.open(f),!0}};function rt(t,n,r){let i=r.fieldIds,{fieldsConfig:a}=n,{rowFields:o,columnFields:s,valueFields:c,filterFields:l,hiddenFields:u,dimension:d,measure:f,valueIndex:p,valuePosition:m}=a,h=/* @__PURE__ */ new Set;[...o,...s,...c,...l,...u].forEach(e=>{let t=d[e]||f[e];t&&h.add(t.dataFieldId)});let g=i.map(e=>{let n=r.fields[e];return{checked:h.has(e),dataFieldId:n.id,id:n.id,name:n.name,type:n.fieldDataType,pivotTableId:t}}),_=o.flatMap(e=>{let t=d[e];return t?[{dataFieldId:t.dataFieldId,tableFieldId:t.id,displayName:t.displayName}]:[]}),v=s.flatMap(e=>{let t=d[e];return t?[{dataFieldId:t.dataFieldId,tableFieldId:t.id,displayName:t.displayName}]:[]}),y=c.flatMap(e=>{let t=f[e];return t?[{dataFieldId:t.dataFieldId,tableFieldId:t.id,displayName:t.displayName,format:t.format,subTotalType:t.subtotal}]:[]}),b=l.flatMap(e=>{let t=d[e];return t?[{dataFieldId:t.dataFieldId,tableFieldId:t.id,displayName:t.displayName}]:[]}),x=u.flatMap(e=>{let t=d[e];return t?[{dataFieldId:t.dataFieldId,tableFieldId:t.id,displayName:t.displayName,filterInfo:t.filterInfo}]:[]});return y.length>1&&(m===e.PivotTableValuePositionEnum.Row?_:v).splice(p,0,{dataFieldId:``,tableFieldId:`PivotMultipleValueId`,displayName:`ΣValue`}),{sourceList:g,sourceRange:n.sourceRangeInfo,rowFields:_,columnFields:v,valueFields:y,filterFields:b,hiddenFields:x,positionInfo:n.targetCellInfo}}const it={type:n.CommandType.OPERATION,id:`sheet.operation.open-pivot-table-panel`,async handler(e,a){let o=a==null?void 0:a.pivotTableId;if(!o)return!1;let s=e.get(i.ISidebarService),c=e.get(n.IUniverInstanceService),l=e.get(n.LocaleService),u=e.get(t.SheetsPivotTableConfigModel),d=(0,r.getSheetCommandTarget)(c);if(!d)return!1;let{unitId:f,subUnitId:p}=d,m=u.getPivotTableConfig(f,p,o),h=u.getCollection(f,o);if(!m||!h)return!1;let g=rt(o,m,h),y={header:{title:l.t(`sheets-pivot-ui.title`)},id:v,children:{label:_,panelInfo:g,version:Math.random(),setVersion:()=>Math.random(),pivotTableId:o,unitId:f,subUnitId:p},width:346};return s.open(y),!0}},at=`sheets-pivot-ui.config`,ot={};function st(e){"@babel/helpers - typeof";return st=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},st(e)}function ct(e,t){if(st(e)!=`object`||!e)return e;var n;if(typeof Symbol<`u`&&(n=e[Symbol.toPrimitive])!==void 0){var r=n.call(e,t||`default`);if(st(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function lt(e){var t=ct(e,`string`);return st(t)==`symbol`?t:t+``}function W(e,t,n){return(t=lt(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function G(e,t){return function(n,r){t(n,r,e)}}function K(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let q=class extends n.Disposable{constructor(e,n,r,i,a,o,s,c){var l;super(),this._localeService=e,this._sheetInterceptorService=n,this._injector=r,this._confirmService=i,this._sheetsPivotTableService=a,this._configService=o,this._sheetPermissionCheckController=s,this._sheetsRenderService=c,W(this,`_defaultOverride`,!1),this.disposeWithMe(this._sheetsRenderService.registerSelfRenderingMutations(t.InitializePivotRuntimeLocalMutation.id)),this._initUIInterceptListener(),this._defaultOverride=((l=this._configService.getConfig(t.SHEETS_PIVOT_PLUGIN_CONFIG_KEY))==null?void 0:l.defaultOverride)??!1}_getPivotAppliedRanges(e,n,r){return(0,t.getPivotAppliedRanges)(e,n,r,this._localeService.getDirection())}_initUIInterceptListener(){this._sheetsPivotTableService.asyncInterceptor.interceptAsync(this._sheetsPivotTableService.asyncInterceptor.getInterceptPoints().PIVOT_PERFORM_CHECK,{handler:async(e,n)=>{let{cellInfo:i,view:a,isEmpty:o}=n,{unitId:s,subUnitId:c}=i,l=this._getPivotAppliedRanges(i,a,o);if(!(0,r.checkRangesEditablePermission)(this._injector,s,c,l)){let e=this._localeService.t(`sheets-pivot-ui.msg.permissionError`);this._sheetPermissionCheckController.blockExecuteWithoutPermission(e)}let u=this._sheetInterceptorService.generateMutationsByRanges({unitId:s,subUnitId:c,ranges:l}),d=(0,t.generateSetRangeValueMutations)(this._injector,s,c,l);if(u.redos.length||d.shouldClear){if(this._defaultOverride)return Promise.resolve(!0);{let e=await this._confirmService.confirm({id:`sheet-pivot.confirm.add-sheet-pivot`,title:{title:this._localeService.t(`sheets-pivot-ui.perform.tips`)},children:{title:this._localeService.t(`sheets-pivot-ui.perform.info`)},cancelText:this._localeService.t(`sheets-pivot-ui.perform.cancel`),confirmText:this._localeService.t(`sheets-pivot-ui.perform.confirm`)});return Promise.resolve(e)}}return Promise.resolve(!0)}})}};q=K([G(0,(0,n.Inject)(n.LocaleService)),G(1,(0,n.Inject)(r.SheetInterceptorService)),G(2,(0,n.Inject)(n.Injector)),G(3,(0,n.Inject)(n.IConfirmService)),G(4,(0,n.Inject)(t.SheetsPivotTableService)),G(5,(0,n.Inject)(n.IConfigService)),G(6,(0,n.Inject)(r.SheetPermissionCheckController)),G(7,(0,n.Inject)(a.SheetsRenderService))],q);function ut(e){let{editable:r,pivotTableId:a,subUnitId:o,unitId:c}=e,l=(0,i.useDependency)(n.ICommandService),u=(0,i.useDependency)(i.IDialogService),f=(0,i.useDependency)(n.LocaleService),p=[{command:{id:it.id,params:{pivotTableId:a}},disabled:!r,label:`sheets-pivot-ui.mobile.edit`},{command:{id:t.RemovePivotTableCommand.id,params:{unitId:c,subUnitId:o,pivotTableId:a}},disabled:!r,label:`sheets-pivot-ui.mobile.delete`}];async function m(e){await l.executeCommand(e.id,e.params),u.close(M)}return/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-flex-col univer-gap-2`,children:p.map(e=>{let t=f.t(e.label);return/* @__PURE__ */ (0,d.jsx)(s.MobileActionRow,{title:t,"aria-label":t,variant:`subtle`,disabled:e.disabled,onClick:()=>m(e.command)},e.command.id)})})}const dt=[{label:`1930-08-05`,suffix:`yyyy-MM-dd`},{label:`1930/08/05`,suffix:`yyyy/MM/dd`},{label:`1930年08月05日`,suffix:`yyyy"年"MM"月"dd"日"`},{label:`08-05`,suffix:`MM-dd`},{label:`8月5日`,suffix:`M"月"d"日"`},{label:`13:30:30`,suffix:`h:mm:ss`},{label:`13:30`,suffix:`h:mm`},{label:`下午01:30`,suffix:`A/P hh:mm`},{label:`下午1:30`,suffix:`A/P h:mm`},{label:`下午1:30:30`,suffix:`A/P h:mm:ss`},{label:`08-05 下午 01:30`,suffix:`MM-dd A/P hh:mm`}],ft=[{label:`(1,235)`,suffix:`#,##0_);(#,##0)`},{label:`(1,235) `,suffix:`#,##0_);[Red](#,##0)`,color:`red`},{label:`1,234.56`,suffix:`#,##0.00_);#,##0.00`},{label:`1,234.56`,suffix:`#,##0.00_);[Red]#,##0.00`,color:`red`},{label:`-1,234.56`,suffix:`#,##0.00_);-#,##0.00`},{label:`-1,234.56`,suffix:`#,##0.00_);[Red]-#,##0.00`,color:`red`}],pt=[{label:e=>`${e}1,235`,suffix:e=>`"${e}"#,##0.00_);"${e}"#,##0.00`},{label:e=>`${e}1,235`,suffix:e=>`"${e}"#,##0.00_);[Red]"${e}"#,##0.00`,color:`red`},{label:e=>`(${e}1,235)`,suffix:e=>`"${e}"#,##0.00_);("${e}"#,##0.00)`},{label:e=>`(${e}1,235)`,suffix:e=>`"${e}"#,##0.00_);[Red]("${e}"#,##0.00)`,color:`red`},{label:e=>`-${e}1,235`,suffix:e=>`"${e}"#,##0.00_);-"${e}"#,##0.00`},{label:e=>`-${e}1,235`,suffix:e=>`"${e}"#,##0.00_);[Red]-"${e}"#,##0.00`,color:`red`}],mt=`PivotMultipleValueId`;function ht(t,n){if(n===e.PivotDataFieldDataTypeEnum.blank)return`sheets-pivot-ui.dialog.filter.blank`;if(n===e.PivotDataFieldDataTypeEnum.other&&t===e.GlobalConfig.otherDataKey)return`sheets-pivot-ui.showValuesAs.other`}function gt(e,n){let r=(0,t.isolateTextForBidiContext)(n,e);return e.includes(`{0}`)?e.replace(`{0}`,r):`${e}${r}`}const _t=t=>{switch(t){case`filter`:return e.PivotTableFiledAreaEnum.Filter;case`row`:return e.PivotTableFiledAreaEnum.Row;case`column`:return e.PivotTableFiledAreaEnum.Column;case`value`:return e.PivotTableFiledAreaEnum.Value;default:return e.PivotTableFiledAreaEnum.Row}};function vt(e,t){switch(t.rangeType||n.RANGE_TYPE.NORMAL){case n.RANGE_TYPE.NORMAL:return{startRow:t.startRow,startColumn:t.startColumn,endRow:t.endRow,endColumn:t.endColumn};case n.RANGE_TYPE.ROW:return{startRow:t.startRow,startColumn:0,endRow:t.endRow,endColumn:e.getColumnCount()-1};case n.RANGE_TYPE.COLUMN:return{startRow:0,startColumn:t.startColumn,endRow:e.getRowCount()-1,endColumn:t.endColumn};case n.RANGE_TYPE.ALL:return{startRow:0,startColumn:0,endRow:e.getRowCount()-1,endColumn:e.getColumnCount()-1}}}function yt(e,t){if(!t||!e||t.endRow-t.startRow<=0)return 1;let{startRow:n,startColumn:r,endRow:i,endColumn:a}=vt(e,t),o=e.getCellMatrix(),s=!1;for(let e=n;e<=i;e++){for(let t=r;t<=a;t++)if(o.getValue(e,t)!=null){s=!0;break}if(s)break}return s?2:0}async function bt(e,n,r){let{commandService:i,pivotTableInfo:a,pivotTableId:o,unitId:s,subUnitId:c}=e,l=[],{rowFields:u,columnFields:d,filterFields:f,valueFields:p}=a,m=[...u,...d,...f,...p];n===`PivotMultipleValueId`&&p.forEach(e=>{l.push(e.tableFieldId)}),r===`source`&&m.forEach(e=>{e.dataFieldId===n&&l.push(e.tableFieldId)}),r!==`source`&&m.find(e=>e.tableFieldId===n)&&l.push(n),await i.executeCommand(t.RemovePivotFieldCommand.id,{pivotTableId:o,fieldIds:l,unitId:s,subUnitId:c})}async function xt(e,n,r,i,a){let o={unitId:i,subUnitId:a,token:n,dataRangeInfo:r};await e.executeCommand(t.UpdatePivotTableSourceRangeCommand.id,o)}const St=(e,t)=>{let{rowFields:n,columnFields:r,filterFields:i}=e,a=[...n,...r,...i],o=[];return a.forEach(e=>{e.dataFieldId===t&&o.push(e.tableFieldId)}),o};async function Ct(e,n,r,i,a){if(r===`source`&&i===`source`)return;let o=n===mt,{commandService:s,pivotTableInfo:c,pivotTableId:l,unitId:u,subUnitId:d}=e;if(r===`source`&&i!==`source`){let{sourceList:e}=c,r=e.find(e=>e.id===n);if(r){let e=St(c,n);if(i!==`value`&&e.length>0){let n=e[0];await s.executeCommand(t.MovePivotFieldCommand.id,{pivotTableId:l,fieldId:n,area:_t(i),index:a,unitId:u,subUnitId:d})}else await s.executeCommand(t.AddPivotFieldCommand.id,{pivotTableId:l,dataFieldId:r.dataFieldId,fieldArea:_t(i),index:a,unitId:u,subUnitId:d})}}if(r!==`source`&&i===`source`){let{rowFields:t,columnFields:r,filterFields:i,valueFields:a}=c,s=[...t,...r,...i,...a],l=[];o?a.forEach(e=>{l.push(e.tableFieldId)}):s.find(e=>e.tableFieldId===n)&&l.push(n),await wt(e,l)}else r!==`source`&&i!==`source`&&await Et(e,n,r,i,a)}async function wt(e,n){let{commandService:r,pivotTableId:i,unitId:a,subUnitId:o}=e;await r.executeCommand(t.RemovePivotFieldCommand.id,{pivotTableId:i,fieldIds:n,unitId:a,subUnitId:o})}async function Tt(e,n,r){let{commandService:i,pivotTableId:a,unitId:o,subUnitId:s}=e;await i.executeCommand(t.UpdatePivotValuePositionCommand.id,{pivotTableId:a,position:n,index:r,unitId:o,subUnitId:s})}async function Et(n,r,i,a,o){let s=r===mt,{commandService:c,pivotTableInfo:l,pivotTableId:u,unitId:d,subUnitId:f}=n;if(s){let n=_t(a);(n===e.PivotTableFiledAreaEnum.Row||n===e.PivotTableFiledAreaEnum.Column)&&await c.executeCommand(t.UpdatePivotValuePositionCommand.id,{pivotTableId:u,position:n===e.PivotTableFiledAreaEnum.Row?e.PivotTableValuePositionEnum.Row:e.PivotTableValuePositionEnum.Column,index:o,unitId:d,subUnitId:f});return}let{rowFields:p,columnFields:m,filterFields:h,valueFields:g}=l,_=[...p,...m,...h,...g].find(e=>e.tableFieldId===r);if(_){let e=[...p,...m,...h];if(i===`value`&&a!==`value`&&e.find(e=>e.dataFieldId===_.dataFieldId)){await wt(n,[r]);return}await c.executeCommand(t.MovePivotFieldCommand.id,{pivotTableId:u,fieldId:r,area:_t(a),index:o,unitId:d,subUnitId:f})}}function Dt(e,t,n,r,i){e.executeCommand(tt.id,{row:-1,col:-1,unitId:t,subUnitId:n,tableFieldId:i,pivotTableId:r})}function Ot(e,t,n,r,i,a){let{sourceList:o,rowFields:s,columnFields:c,valueFields:l,filterFields:u}=a,d=[...s,...c,...l,...u].find(e=>e.tableFieldId===n),f=o.find(e=>e.id===(d==null?void 0:d.dataFieldId));if(d&&f){let a={tableFieldId:n,pivotTableId:t,unitId:r,subUnitId:i,tableFieldInfo:d,dataFieldInfo:f};e.executeCommand(nt.id,a)}}const kt=[{type:e.PivotSubtotalTypeEnum.average,textKey:`sheets-pivot-ui.subtotal.average`},{type:e.PivotSubtotalTypeEnum.count,textKey:`sheets-pivot-ui.subtotal.count`},{type:e.PivotSubtotalTypeEnum.countNums,textKey:`sheets-pivot-ui.subtotal.countNums`},{type:e.PivotSubtotalTypeEnum.max,textKey:`sheets-pivot-ui.subtotal.max`},{type:e.PivotSubtotalTypeEnum.min,textKey:`sheets-pivot-ui.subtotal.min`},{type:e.PivotSubtotalTypeEnum.product,textKey:`sheets-pivot-ui.subtotal.product`},{type:e.PivotSubtotalTypeEnum.stdDev,textKey:`sheets-pivot-ui.subtotal.stdDev`},{type:e.PivotSubtotalTypeEnum.stdDevp,textKey:`sheets-pivot-ui.subtotal.stdDevp`},{type:e.PivotSubtotalTypeEnum.sum,textKey:`sheets-pivot-ui.subtotal.sum`},{type:e.PivotSubtotalTypeEnum.var,textKey:`sheets-pivot-ui.subtotal.var`},{type:e.PivotSubtotalTypeEnum.varp,textKey:`sheets-pivot-ui.subtotal.varp`}],At=(e,t=0)=>e?n.numfmt.getFormatInfo(e).maxDecimals??t:t,jt=e=>Array(Math.min(Math.max(0,Number(e)),30)).fill(0).join(``),J=(e,t)=>e.split(`;`).map(e=>/\.0?/.test(e)?e.replace(/\.0*/g,`${t>0?`.`:``}${jt(Number(t||0))}`):/0[^0]?/.test(e)?e.replace(/0[^0]+|0$/,`0${t>0?`.`:``}${jt(Number(t||0))}`):e).join(`;`),Mt=e=>n.currencySymbols.find(t=>e.includes(t)),Nt=e=>!e,Pt=e=>!!Mt(e)&&e.startsWith(`_(`),Ft=e=>!!Mt(e)&&!e.startsWith(`_(`),It=()=>dt.map(e=>({label:e.label,value:e.suffix})),Lt=e=>{let t=n.numfmt.getFormatInfo(e);return It().map(e=>e.value).includes(e)||[`date`,`datetime`,`time`].includes(t.type)},Rt=()=>ft.map(e=>({label:e.label,value:e.suffix,color:e.color})),zt=e=>Rt().some(t=>(0,n.isPatternEqualWithoutDecimal)(t.value,e)),Bt=(t,n)=>{switch(n){case e.PivotTableFiledAreaEnum.Row:return t.rowFields.length;case e.PivotTableFiledAreaEnum.Column:return t.columnFields.length;case e.PivotTableFiledAreaEnum.Filter:return t.filterFields.length;case e.PivotTableFiledAreaEnum.Value:return t.valueFields.length;default:return 0}},Vt=(e,n,r)=>{for(let i of[`sheets-pivot-ui.averageName`,`sheets-pivot-ui.countName`,`sheets-pivot-ui.countNumsName`,`sheets-pivot-ui.maxName`,`sheets-pivot-ui.minName`,`sheets-pivot-ui.productName`,`sheets-pivot-ui.stdDevName`,`sheets-pivot-ui.stdDevpName`,`sheets-pivot-ui.sumName`,`sheets-pivot-ui.varName`,`sheets-pivot-ui.varpName`]){let a=r[i];if(a+(0,t.isolateTextForBidiContext)(e,a)===n||a+e===n)return!0}return!1},Ht=(n,r,i)=>{let a=``;switch(i){case e.PivotSubtotalTypeEnum.average:a=r[`sheets-pivot-ui.averageName`];break;case e.PivotSubtotalTypeEnum.count:a=r[`sheets-pivot-ui.countName`];break;case e.PivotSubtotalTypeEnum.countNums:a=r[`sheets-pivot-ui.countNumsName`];break;case e.PivotSubtotalTypeEnum.max:a=r[`sheets-pivot-ui.maxName`];break;case e.PivotSubtotalTypeEnum.min:a=r[`sheets-pivot-ui.minName`];break;case e.PivotSubtotalTypeEnum.product:a=r[`sheets-pivot-ui.productName`];break;case e.PivotSubtotalTypeEnum.stdDev:a=r[`sheets-pivot-ui.stdDevName`];break;case e.PivotSubtotalTypeEnum.stdDevp:a=r[`sheets-pivot-ui.stdDevpName`];break;case e.PivotSubtotalTypeEnum.sum:a=r[`sheets-pivot-ui.sumName`];break;case e.PivotSubtotalTypeEnum.var:a=r[`sheets-pivot-ui.varName`];break;case e.PivotSubtotalTypeEnum.varp:a=r[`sheets-pivot-ui.varpName`];break;default:return n}return a+(0,t.isolateTextForBidiContext)(n,a)};function Ut(t,n){if(n.conditionFilter&&(0,e.isPivotTableFilterRestrictive)(n.conditionFilter))return`HasFilter`;if(!t)return`NotFilter`;if((0,e.isPivotDateFilter)(t))return`HasFilter`;if((0,e.isManualPivotFilter)(t)){var r;return t.isAll||((r=n.items)==null?void 0:r.length)===t.list.length?`NotFilter`:`HasFilter`}return(0,e.isPivotTableFilterRestrictive)(t)?`HasFilter`:`NotFilter`}function Wt(e){return Pt(e)?`accounting`:Ft(e)?`currency`:Lt(e)?`date`:zt(e)?`thousandthPercentile`:`general`}function Gt(e){let{originPattern:t=``,onChange:r}=e,a=(0,i.useDependency)(n.LocaleService),[o,c]=(0,f.useState)(()=>Wt(t)),[l,u]=(0,f.useState)(()=>At(t,2)),[p,m]=(0,f.useState)(()=>Mt(t)||n.currencySymbols[0]),[h,g]=(0,f.useState)(t),_=(0,f.useMemo)(()=>[{value:`general`,label:a.t(`sheets-pivot-ui.dialog.numfmt.general`)},{value:`accounting`,label:a.t(`sheets-pivot-ui.dialog.numfmt.accounting`)},{value:`currency`,label:a.t(`sheets-pivot-ui.dialog.numfmt.currency`)},{value:`date`,label:a.t(`sheets-pivot-ui.dialog.numfmt.date`)},{value:`thousandthPercentile`,label:a.t(`sheets-pivot-ui.dialog.numfmt.thousandthPercentile`)}],[a]),v=n.currencySymbols.map(e=>({label:e,value:e}));function y(e){g(e),r(e)}function b(e,t){return J(`_("${e}"* #,##0${t>0?`.0`:``}_)`,t)}function x(e){c(e),y(e===`general`?``:e===`accounting`?b(p,l):e===`currency`?J(pt[0].suffix(p),l):e===`date`?dt[0].suffix:J(ft[0].suffix,l))}function S(e){m(e),y(o===`accounting`?b(e,l):J(pt[0].suffix(e),l))}function C(e){let t=e??0;u(t),y(o===`accounting`?b(p,t):J(h,t))}let w=pt.map(e=>({label:e.label(p),value:e.suffix(p),color:e.color})),T=ft.map(e=>({label:e.label,value:e.suffix,color:e.color})),E=dt.map(e=>({label:e.label,value:e.suffix}));return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:a.t(`sheets-pivot-ui.dialog.numfmt.formatTypes`)}),/* @__PURE__ */ (0,d.jsx)(s.MobileSelect,{className:`univer-w-full`,value:o,options:_,onChange:e=>x(e),size:`small`})]}),(o===`accounting`||o===`currency`)&&/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-grid univer-grid-cols-2 univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:a.t(`sheets-pivot-ui.dialog.numfmt.decimalPlaces`)}),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-w-full`,value:l,min:0,max:20,onChange:C,size:`small`,controls:!1})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:a.t(`sheets-pivot-ui.dialog.numfmt.currencyTypes`)}),/* @__PURE__ */ (0,d.jsx)(s.MobileSelect,{className:`univer-w-full`,value:p,options:v,onChange:S,size:`small`})]})]}),o===`thousandthPercentile`&&/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:a.t(`sheets-pivot-ui.dialog.numfmt.decimalPlaces`)}),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-w-full`,value:l,min:0,max:20,onChange:C,size:`small`,controls:!1})]}),o===`currency`&&/* @__PURE__ */ (0,d.jsx)(s.SelectList,{className:`univer-rounded-xl univer-p-2`,options:w,value:h,onChange:e=>y(J(e,l))}),o===`date`&&/* @__PURE__ */ (0,d.jsx)(s.SelectList,{className:`univer-rounded-xl univer-p-2`,options:E,value:h,onChange:e=>y(e)}),o===`thousandthPercentile`&&/* @__PURE__ */ (0,d.jsx)(s.SelectList,{className:`univer-rounded-xl univer-p-2`,options:T,value:h,onChange:e=>y(J(e,l))}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-text-gray-500`,children:a.t(`sheets-pivot-ui.dialog.numfmt.${o}Desc`)})]})}function Kt(e){let{unitId:r,subUnitId:a,pivotTableId:o,tableFieldId:c,originPattern:l,tableFieldInfo:u,dataFieldInfo:p}=e,m=(0,i.useDependency)(n.ICommandService),h=(0,i.useDependency)(i.IDialogService),g=(0,i.useDependency)(n.LocaleService),[_,v]=(0,f.useState)(l??``);async function y(){let e={format:_,tableFieldId:c,pivotTableId:o,unitId:r,subUnitId:a,tableFieldInfo:u,dataFieldInfo:p};await m.executeCommand(t.UpdatePivotFieldSettingCommand.id,e),h.close(w)}return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-5`,dir:g.getDirection(),children:[/* @__PURE__ */ (0,d.jsx)(Gt,{originPattern:l,onChange:v}),/* @__PURE__ */ (0,d.jsxs)(s.MobileActionRowGroup,{children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:()=>h.close(w),size:`middle`,children:g.t(`sheets-pivot-ui.dialog.numfmt.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,onClick:y,size:`middle`,children:g.t(`sheets-pivot-ui.dialog.numfmt.confirm`)})]})]})}function qt(e){let{unitId:r,subUnitId:a,pivotTableId:o,tableFieldId:c,tableFieldInfo:l,dataFieldInfo:p,closeFn:m,format:h,version:g}=e,_=(0,i.useDependency)(n.ICommandService),v=(0,i.useDependency)(t.SheetsPivotTableConfigModel),y=(0,i.useDependency)(n.LocaleService),[b,x]=(0,f.useState)(`field`),[S,C]=(0,f.useState)(l.displayName),[w,T]=(0,f.useState)(l.subTotalType),[E,D]=(0,f.useState)(h??l.format??``),[O,k]=(0,f.useState)(E);(0,f.useEffect)(()=>{x(`field`),C(l.displayName),T(l.subTotalType),D(h??l.format??``),k(h??l.format??``)},[h,l,g]);let A={...v.getTextInfo()};[`sheets-pivot-ui.averageName`,`sheets-pivot-ui.countName`,`sheets-pivot-ui.countNumsName`,`sheets-pivot-ui.maxName`,`sheets-pivot-ui.minName`,`sheets-pivot-ui.productName`,`sheets-pivot-ui.stdDevName`,`sheets-pivot-ui.stdDevpName`,`sheets-pivot-ui.sumName`,`sheets-pivot-ui.varName`,`sheets-pivot-ui.varpName`].forEach(e=>{A[e]=y.t(e)});function j(e){T(e),Vt(p.name,S,A)&&C(Ht(p.name,A,e))}async function M(){let e={displayName:l.displayName===S?void 0:S,subtotalType:l.subTotalType===w?void 0:w,format:l.format===E?void 0:E,tableFieldId:c,pivotTableId:o,unitId:r,subUnitId:a,tableFieldInfo:l,dataFieldInfo:p};await _.executeCommand(t.UpdatePivotFieldSettingCommand.id,e),m()}return b===`format`?/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-5`,dir:y.getDirection(),children:[/* @__PURE__ */ (0,d.jsxs)(`button`,{type:`button`,className:`univer-flex univer-h-10 univer-items-center univer-gap-2 univer-border-0 univer-bg-transparent univer-p-0 univer-text-base univer-font-semibold`,onClick:()=>x(`field`),children:[/* @__PURE__ */ (0,d.jsx)(u.ArrowLeftIcon,{}),y.t(`sheets-pivot-ui.dialog.numfmt.fieldSetting`)]}),/* @__PURE__ */ (0,d.jsx)(Gt,{originPattern:E,onChange:k}),/* @__PURE__ */ (0,d.jsxs)(s.MobileActionRowGroup,{children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:()=>x(`field`),size:`middle`,children:y.t(`sheets-pivot-ui.dialog.numfmt.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,onClick:()=>{D(O),x(`field`)},size:`middle`,children:y.t(`sheets-pivot-ui.dialog.numfmt.confirm`)})]})]}):/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,dir:y.getDirection(),children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm univer-font-medium`,children:y.t(`sheets-pivot-ui.dialog.fieldName`)}),/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-truncate univer-text-xs univer-text-gray-500`,children:gt(y.t(`sheets-pivot-ui.dialog.sourceName`),p.name)})]}),/* @__PURE__ */ (0,d.jsx)(s.Input,{className:`univer-mt-2 univer-w-full`,value:S,onChange:C,size:`small`})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:y.t(`sheets-pivot-ui.dialog.summarizeBy`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-max-h-[40vh] univer-overflow-y-auto univer-rounded-xl univer-bg-gray-50 univer-p-1 dark:!univer-bg-gray-800`,children:kt.map(e=>/* @__PURE__ */ (0,d.jsxs)(`button`,{type:`button`,className:(0,s.clsx)(`univer-flex univer-h-12 univer-w-full univer-items-center univer-gap-2 univer-rounded-xl univer-border-0 univer-bg-transparent univer-px-3 univer-text-left univer-text-sm`,{"univer-bg-primary-50 dark:!univer-bg-gray-700":w===e.type}),onClick:()=>j(e.type),children:[/* @__PURE__ */ (0,d.jsx)(u.CheckMarkIcon,{className:(0,s.clsx)(`univer-text-primary-600`,{"univer-invisible":w!==e.type})}),y.t(e.textKey)]},e.type))})]}),/* @__PURE__ */ (0,d.jsx)(s.Button,{className:`univer-w-full`,onClick:()=>{k(E),x(`format`)},size:`middle`,children:y.t(`sheets-pivot-ui.dialog.format`)}),/* @__PURE__ */ (0,d.jsxs)(s.MobileActionRowGroup,{children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:m,size:`middle`,children:y.t(`sheets-pivot-ui.dialog.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:!S,onClick:M,size:`middle`,children:y.t(`sheets-pivot-ui.dialog.ok`)})]})]})}function Jt(r){var a;let{filterItemInfo:o,unitId:c,subUnitId:l}=r,p=(0,i.useDependency)(n.ICommandService),m=(0,i.useDependency)(i.IDialogService),h=(0,i.useDependency)(n.LocaleService),g=(0,i.useDependency)(n.IUniverInstanceService),_=(0,i.useObservable)(h.direction$,h.getDirection()),[v,y]=(0,f.useState)(()=>{var e;return o.checkedItems??((e=o.filterInfo)==null?void 0:e.list)??o.items}),[x,S]=(0,f.useState)(()=>o.partialItems??[]),[C,w]=(0,f.useState)((a=o.sortInfo)==null?void 0:a.type),T=x.length===0&&v.length===o.items.length,E=Ut(o.filterInfo,{items:o.items,conditionFilter:o.conditionFilter});(0,f.useEffect)(()=>{let e=g.getCurrentUnitOfType(n.UniverInstanceType.UNIVER_SHEET);if(!e)return;let t=e.activeSheet$.subscribe(e=>{(e==null?void 0:e.getSheetId())!==l&&m.close(b)});return()=>t.unsubscribe()},[m,l,g]);function D(){m.close(b)}function O(e){x.includes(e)?(S(t=>t.filter(t=>t!==e)),y(t=>[...t,e])):y(t=>t.includes(e)?t.filter(t=>t!==e):[...t,e])}async function k(e,n,r,i=!1){await p.executeCommand(t.SetPivotFilterCommand.id,{unitId:c,subUnitId:l,pivotTableId:o.pivotTableId,tableFieldId:o.tableFieldId,items:e,partialItems:r,isAll:n,clearAllFilters:i}),D()}async function A(e){await p.executeCommand(t.SetPivotSortCommand.id,{unitId:c,subUnitId:l,pivotTableId:o.pivotTableId,tableFieldId:o.tableFieldId,info:{type:e}}),w(e),D()}return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,dir:_,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-base univer-font-semibold`,children:h.t(`sheets-pivot-ui.filterField`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-grid univer-grid-cols-2 univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsxs)(s.Button,{className:(0,s.clsx)(``,{"univer-bg-gray-100 dark:!univer-bg-gray-700":C===e.PivotDataFieldSortOperatorEnum.ascending}),onClick:()=>A(e.PivotDataFieldSortOperatorEnum.ascending),size:`middle`,children:[/* @__PURE__ */ (0,d.jsx)(u.AscendingIcon,{}),h.t(`sheets-pivot-ui.dialog.filter.ascending`)]}),/* @__PURE__ */ (0,d.jsxs)(s.Button,{className:(0,s.clsx)(``,{"univer-bg-gray-100 dark:!univer-bg-gray-700":C===e.PivotDataFieldSortOperatorEnum.descending}),onClick:()=>A(e.PivotDataFieldSortOperatorEnum.descending),size:`middle`,children:[/* @__PURE__ */ (0,d.jsx)(u.DescendingIcon,{}),h.t(`sheets-pivot-ui.dialog.filter.descending`)]})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-max-h-[40vh] univer-overflow-y-auto univer-rounded-xl univer-bg-gray-50 univer-p-2 dark:!univer-bg-gray-800`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-min-h-12 univer-items-center univer-px-2`,children:/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:T,indeterminate:!T&&(v.length>0||x.length>0),onChange:()=>{S([]),y(T?[]:o.items)},children:h.t(`sheets-pivot-ui.dialog.filter.selectAll`)})}),o.items.map((t,r)=>{var i;let a=o.itemTypes[r],c=(i=o.itemLabels)==null?void 0:i[t],l=ht(t,a),u=t;return l?u=h.t(l):c===void 0?a===e.PivotDataFieldDataTypeEnum.date&&o.format&&(u=n.numfmt.format(o.format,Number(t))):u=c,/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-min-h-12 univer-items-center univer-px-2`,children:/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:v.includes(t),indeterminate:x.includes(t),contentClassName:`univer-max-w-[70vw] univer-truncate`,onChange:()=>O(t),children:u})},t)})]}),/* @__PURE__ */ (0,d.jsx)(s.Button,{className:`univer-w-full`,variant:`link`,disabled:E===`NotFilter`,onClick:()=>k(o.items,!0,[],!0),size:`middle`,children:h.t(`sheets-pivot-ui.dialog.filter.clearFilter`)}),/* @__PURE__ */ (0,d.jsxs)(s.MobileActionRowGroup,{children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:D,size:`middle`,children:h.t(`sheets-pivot-ui.dialog.filter.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:!v.length&&!x.length,onClick:()=>k(v,T,x),size:`middle`,children:h.t(`sheets-pivot-ui.dialog.filter.confirm`)})]})]})}const Yt=[{label:`sheets-pivot-ui.config.pageDownThenOver`,value:`false`},{label:`sheets-pivot-ui.config.pageOverThenDown`,value:`true`}],Xt=[{label:`sheets-pivot-ui.config.layoutTabular`,value:String(e.PivotLayoutTypeEnum.tabular)},{label:`sheets-pivot-ui.config.layoutCompact`,value:String(e.PivotLayoutTypeEnum.compact)},{label:`sheets-pivot-ui.config.layoutOutline`,value:String(e.PivotLayoutTypeEnum.outline)}],Zt=[{label:`sheets-pivot-ui.config.rowSubtotalTop`,value:String(e.PivotSubtotalPositionEnum.top)},{label:`sheets-pivot-ui.config.rowSubtotalBottom`,value:String(e.PivotSubtotalPositionEnum.bottom)}],Qt=[{label:`sheets-pivot-ui.config.dateGroupingLegacy`,value:e.PivotDateGroupingMode.Legacy},{label:`sheets-pivot-ui.config.dateGroupingExcelLike`,value:e.PivotDateGroupingMode.ExcelLike}];function $t(r){let{unitId:a,subUnitId:c,pivotTableId:l}=r,u=(0,i.useDependency)(n.ICommandService),p=(0,i.useDependency)(n.IConfirmService),m=(0,i.useDependency)(n.ILogService),h=(0,i.useDependency)(n.LocaleService),g=(0,i.useDependency)(t.SheetsPivotTableConfigModel),_=(0,f.useMemo)(()=>g.watchPivotTableChanges(a,c,l).pipe((0,o.debounceTime)(0)),[g,a,c,l]);(0,i.useObservable)(_);let[v,y]=(0,f.useState)(!1),b=(0,f.useRef)(!1),x=g.getPivotTableConfig(a,c,l),S=(0,e.getPivotDateGroupingMode)(x==null?void 0:x.fieldsConfig.options),[C,w]=(0,f.useState)(()=>{var t,n;let r=g.getPivotTableConfig(a,c,l),i=(r==null||(t=r.fieldsConfig)==null?void 0:t.options)??{};return{showRowSubtotal:i.showRowSubtotal??!0,showRowGrandTotal:i.showRowGrandTotal??!0,showColSubtotal:i.showColSubtotal??!0,showColGrandTotal:i.showColGrandTotal??!0,pageWrap:i.pageWrap??1,pageOverThenDown:i.pageOverThenDown??!1,repeatRowLabels:i.repeatRowLabels??!1,repeatColLabels:i.repeatColLabels??!1,layout:(r==null||(n=r.fieldsConfig)==null?void 0:n.layout)??e.PivotLayoutTypeEnum.tabular,rowSubtotalPosition:i.rowSubtotalPosition??e.PivotSubtotalPositionEnum.top}});function T(e){let n={unitId:a,subUnitId:c,pivotTableId:l,options:e};return w(t=>({...t,...e})),u.executeCommand(t.SetPivotOptionCommand.id,n).catch(e=>m.error(`[MobilePivotConfigPanel]`,e))}function E(e){let n={unitId:a,subUnitId:c,pivotTableId:l,layout:e};return w(t=>({...t,layout:e})),u.executeCommand(t.SetPivotLayoutCommand.id,n).catch(e=>m.error(`[MobilePivotConfigPanel]`,e))}async function D(n){if(!(b.current||n===S)){b.current=!0,y(!0);try{let r=g.getPivotTableConfig(a,c,l),i=g.getCollection(a,l);if(!r||!i||(Object.values(i.fields).some(e.isDateGroupFieldJSON)||i.customFields.some(e.isDateGroupFieldJSON))&&!await p.confirm({id:`sheet-pivot.confirm.change-date-grouping-mode`,title:{title:h.t(`sheets-pivot-ui.config.dateGroupingSwitchTitle`)},children:{title:h.t(`sheets-pivot-ui.config.dateGroupingSwitchMessage`)},cancelText:h.t(`sheets-pivot-ui.dialog.cancel`),confirmText:h.t(`sheets-pivot-ui.dialog.ok`)})||g.getPivotTableConfig(a,c,l)!==r||g.getCollection(a,l)!==i||(0,e.getPivotDateGroupingMode)(r.fieldsConfig.options)===n)return;await u.executeCommand(t.SetPivotOptionCommand.id,{unitId:a,subUnitId:c,pivotTableId:l,options:{dateGroupingMode:n}})}catch(e){m.error(`[MobilePivotConfigPanel]`,e)}finally{b.current=!1,y(!1)}}}let O=e=>e.map(e=>({...e,label:h.t(e.label)}));return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-5 univer-py-4`,children:[/* @__PURE__ */ (0,d.jsxs)(`section`,{className:`univer-flex univer-flex-col univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.displaySection`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-h-10 univer-items-center univer-justify-between univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm`,children:h.t(`sheets-pivot-ui.config.dateGroupingMode`)}),/* @__PURE__ */ (0,d.jsx)(s.MobileSelect,{className:`univer-w-40`,value:S,options:O(Qt),disabled:v,onChange:e=>D(e),size:`small`})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-h-10 univer-items-center univer-justify-between univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm`,children:h.t(`sheets-pivot-ui.config.layout`)}),/* @__PURE__ */ (0,d.jsx)(s.MobileSelect,{className:`univer-w-40`,value:String(C.layout),options:O(Xt),onChange:e=>E(Number(e)),size:`small`})]}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.repeatRowLabels,disabled:C.layout===e.PivotLayoutTypeEnum.compact,onChange:e=>T({repeatRowLabels:!!e}),children:h.t(`sheets-pivot-ui.config.repeatRowLabels`)}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.repeatColLabels,onChange:e=>T({repeatColLabels:!!e}),children:h.t(`sheets-pivot-ui.config.repeatColLabels`)})]}),/* @__PURE__ */ (0,d.jsxs)(`section`,{className:`univer-flex univer-flex-col univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.totalsSection`)}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showRowSubtotal,onChange:e=>T({showRowSubtotal:!!e}),children:h.t(`sheets-pivot-ui.config.showRowSubtotal`)}),C.layout===e.PivotLayoutTypeEnum.outline&&/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-h-10 univer-items-center univer-justify-between univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm`,children:h.t(`sheets-pivot-ui.config.rowSubtotalPosition`)}),/* @__PURE__ */ (0,d.jsx)(s.MobileSelect,{className:`univer-w-40`,value:String(C.rowSubtotalPosition),options:O(Zt),onChange:e=>T({rowSubtotalPosition:Number(e)}),size:`small`})]}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showRowGrandTotal,onChange:e=>T({showRowGrandTotal:!!e}),children:h.t(`sheets-pivot-ui.config.showRowGrandTotal`)}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showColSubtotal,onChange:e=>T({showColSubtotal:!!e}),children:h.t(`sheets-pivot-ui.config.showColSubtotal`)}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showColGrandTotal,onChange:e=>T({showColGrandTotal:!!e}),children:h.t(`sheets-pivot-ui.config.showColGrandTotal`)})]}),/* @__PURE__ */ (0,d.jsxs)(`section`,{className:`univer-flex univer-flex-col univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.pageSection`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-h-10 univer-items-center univer-justify-between univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm`,children:h.t(`sheets-pivot-ui.config.pageWrap`)}),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-w-24`,value:C.pageWrap,min:1,max:20,onChange:e=>{let t=Number(e);return Number.isFinite(t)&&t>=1?T({pageWrap:t}):void 0},size:`small`,controls:!1})]}),/* @__PURE__ */ (0,d.jsx)(`p`,{className:`univer-text-xs univer-text-gray-600`,children:h.t(C.pageOverThenDown?`sheets-pivot-ui.config.pageWrapOverThenDownHelp`:`sheets-pivot-ui.config.pageWrapDownThenOverHelp`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-h-10 univer-items-center univer-justify-between univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm`,children:h.t(`sheets-pivot-ui.config.pageDirection`)}),/* @__PURE__ */ (0,d.jsx)(s.MobileSelect,{className:`univer-w-40`,value:String(C.pageOverThenDown),options:O(Yt),onChange:e=>T({pageOverThenDown:e===`true`}),size:`small`})]})]})]})}function en(e,t,n,r){let i=[];return t>0&&i.push(`moveUp`),t<n-1&&i.push(`moveDown`),r?(i.push(e===`column`?`moveToRow`:`moveToColumn`),i.push(`remove`),i):(e!==`filter`&&i.push(`moveToFilter`),e!==`column`&&i.push(`moveToColumn`),e!==`row`&&i.push(`moveToRow`),e===`value`?i.push(`settings`):(i.push(`moveToValue`),i.push(`filter`)),i.push(`remove`),i)}const tn=[`filter`,`column`,`row`,`value`];function nn(e,t){switch(t){case`filter`:return e.filterFields;case`column`:return e.columnFields;case`row`:return e.rowFields;case`value`:return e.valueFields;default:return[]}}function rn(e){switch(e){case`filter`:return`sheets-pivot-ui.filter`;case`column`:return`sheets-pivot-ui.column`;case`row`:return`sheets-pivot-ui.row`;default:return`sheets-pivot-ui.value`}}function an(e){switch(e){case`filter`:return`sheets-pivot-ui.mobile.filterDesc`;case`column`:return`sheets-pivot-ui.mobile.columnDesc`;case`row`:return`sheets-pivot-ui.mobile.rowDesc`;default:return`sheets-pivot-ui.valueDesc`}}function on(e){switch(e.area){case`filter`:return/* @__PURE__ */ (0,d.jsx)(u.FilterIcon,{});case`column`:return/* @__PURE__ */ (0,d.jsx)(u.RowIcon,{});case`row`:return/* @__PURE__ */ (0,d.jsx)(u.ColumnIcon,{});default:return/* @__PURE__ */ (0,d.jsx)(u.FunctionIcon,{})}}function sn(e){switch(e){case`moveUp`:return`sheets-pivot-ui.mobile.moveUp`;case`moveDown`:return`sheets-pivot-ui.mobile.moveDown`;case`moveToFilter`:return`sheets-pivot-ui.mobile.moveToFilter`;case`moveToColumn`:return`sheets-pivot-ui.mobile.moveToColumn`;case`moveToRow`:return`sheets-pivot-ui.mobile.moveToRow`;case`moveToValue`:return`sheets-pivot-ui.mobile.moveToValue`;case`filter`:return`sheets-pivot-ui.filterField`;case`settings`:return`sheets-pivot-ui.fieldSettings`;default:return`sheets-pivot-ui.removeField`}}function cn(e){switch(e){case`moveToFilter`:return`filter`;case`moveToColumn`:return`column`;case`moveToRow`:return`row`;case`moveToValue`:return`value`;default:return null}}function ln(e){let{area:t,field:r,index:a,fieldCount:o,info:c,pivotTableId:l,unitId:f,subUnitId:p,refresh:m}=e,h=(0,i.useDependency)(n.ICommandService),g=(0,i.useDependency)(n.LocaleService),_=r.tableFieldId===mt,v={pivotTableInfo:c,pivotTableId:l,commandService:h,unitId:f,subUnitId:p};async function y(e){if(e===`filter`){Dt(h,f,p,l,r.tableFieldId);return}if(e===`settings`){Ot(h,l,r.tableFieldId,f,p,c);return}if(e===`remove`){await bt(v,r.tableFieldId,t),await m();return}let n=cn(e)??t,i=nn(c,n).length;e===`moveUp`?i=a-1:e===`moveDown`&&(i=a+1),await Ct(v,r.tableFieldId,t,n,i),await m()}let b=en(t,a,o,_).map(e=>({type:`item`,children:g.t(sn(e)),variant:e===`remove`?`destructive`:`default`,onSelect:()=>y(e)}));return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-h-12 univer-items-center univer-gap-3 univer-rounded-xl univer-px-3`,children:[/* @__PURE__ */ (0,d.jsx)(`bdi`,{className:`univer-min-w-0 univer-flex-1 univer-truncate univer-text-sm`,dir:`auto`,children:_?g.t(`sheets-pivot-ui.multipleValue`):r.displayName}),/* @__PURE__ */ (0,d.jsx)(s.MobileDropdownMenu,{items:b,children:/* @__PURE__ */ (0,d.jsx)(`button`,{type:`button`,className:`univer-flex univer-size-10 univer-shrink-0 univer-items-center univer-justify-center univer-rounded-lg univer-border-0 univer-bg-transparent active:univer-bg-gray-100 dark:active:!univer-bg-gray-700`,children:/* @__PURE__ */ (0,d.jsx)(u.MoreHorizontalIcon,{})})})]})}function un(e){let t=(0,i.useDependency)(n.LocaleService);return/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,children:tn.map(n=>{let r=nn(e.info,n);return/* @__PURE__ */ (0,d.jsxs)(`section`,{children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-2 univer-flex univer-items-center univer-gap-2 univer-text-sm univer-font-medium`,children:[/* @__PURE__ */ (0,d.jsx)(on,{area:n}),t.t(rn(n))]}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-rounded-xl univer-bg-gray-50 univer-p-1 dark:!univer-bg-gray-800`,children:r.length?r.map((t,i)=>/* @__PURE__ */ (0,d.jsx)(ln,{...e,area:n,field:t,index:i,fieldCount:r.length},t.tableFieldId)):/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-p-4 univer-text-center univer-text-xs univer-text-gray-500`,children:t.t(an(n))})})]},n)})})}function dn(e,t){switch(t){case`filter`:return e.filterFields.length;case`column`:return e.columnFields.length;case`row`:return e.rowFields.length;case`value`:return e.valueFields.length;default:return 0}}function fn(t){let n=`univer-size-4 univer-shrink-0 univer-text-gray-500`;switch(t.type){case e.PivotDataFieldDataTypeEnum.number:return/* @__PURE__ */ (0,d.jsx)(u.NumberIcon,{className:n});case e.PivotDataFieldDataTypeEnum.date:return/* @__PURE__ */ (0,d.jsx)(u.CalendarIcon,{className:n});default:return/* @__PURE__ */ (0,d.jsx)(u.TextIcon,{className:n})}}function pn(t){let{field:r,info:a,pivotTableId:o,unitId:c,subUnitId:l,refresh:f}=t,p=(0,i.useDependency)(n.ICommandService),m=(0,i.useDependency)(n.LocaleService),h={pivotTableInfo:a,pivotTableId:o,commandService:p,unitId:c,subUnitId:l};async function g(t){if(t){let t=r.type===e.PivotDataFieldDataTypeEnum.number?`value`:`row`;await Ct(h,r.dataFieldId,`source`,t,dn(a,t))}else await bt(h,r.dataFieldId,`source`);await f()}async function _(e){await Ct(h,r.dataFieldId,`source`,e,dn(a,e)),await f()}let v=[{type:`item`,children:m.t(`sheets-pivot-ui.addToRow`),onSelect:()=>_(`row`)},{type:`item`,children:m.t(`sheets-pivot-ui.addToColumn`),onSelect:()=>_(`column`)},{type:`item`,children:m.t(`sheets-pivot-ui.addToValue`),onSelect:()=>_(`value`)},{type:`item`,children:m.t(`sheets-pivot-ui.addToFilter`),onSelect:()=>_(`filter`)}];return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-h-12 univer-items-center univer-gap-3 univer-rounded-xl univer-px-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:r.checked,onChange:e=>g(!!e)}),/* @__PURE__ */ (0,d.jsx)(fn,{type:r.type}),/* @__PURE__ */ (0,d.jsx)(`bdi`,{className:`univer-min-w-0 univer-flex-1 univer-truncate univer-text-sm`,dir:`auto`,children:r.name}),/* @__PURE__ */ (0,d.jsx)(s.MobileDropdownMenu,{items:v,children:/* @__PURE__ */ (0,d.jsx)(`button`,{type:`button`,className:`univer-flex univer-size-10 univer-shrink-0 univer-items-center univer-justify-center univer-rounded-lg univer-border-0 univer-bg-transparent active:univer-bg-gray-100 dark:active:!univer-bg-gray-700`,children:/* @__PURE__ */ (0,d.jsx)(u.MoreHorizontalIcon,{})})})]})}function mn(e){let{info:t}=e,r=(0,i.useDependency)(n.LocaleService),[a,o]=(0,f.useState)(``),c=a.trim().toLocaleLowerCase(),l=c?t.sourceList.filter(e=>e.name.toLocaleLowerCase().includes(c)):t.sourceList;return/* @__PURE__ */ (0,d.jsxs)(`section`,{className:`univer-flex univer-flex-col univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.sourceFieldTile`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-1 univer-text-xs univer-text-gray-500`,children:r.t(`sheets-pivot-ui.mobile.sourceFieldDescription`)})]}),/* @__PURE__ */ (0,d.jsx)(s.Input,{allowClear:!0,value:a,placeholder:r.t(`sheets-pivot-ui.searchFieldPlaceholder`),slot:/* @__PURE__ */ (0,d.jsx)(u.SearchIcon,{}),onChange:o,size:`small`}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-rounded-xl univer-bg-gray-50 univer-p-1 dark:!univer-bg-gray-800`,children:l.length?l.map(t=>/* @__PURE__ */ (0,d.jsx)(pn,{...e,field:t},t.id)):/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-p-4 univer-text-center univer-text-sm univer-text-gray-500`,children:r.t(`sheets-pivot-ui.noMatchingFields`)})})]})}function hn(e){var t;let{sourceRangeInfo:r,positionInfo:a,pivotTableId:o,refresh:s}=e,c=(0,i.useDependency)(n.ICommandService),l=(0,i.useDependency)(n.ILogService),u=(0,i.useDependency)(n.LocaleService),h=(0,i.useDependency)(n.IUniverInstanceService).getUnit(r.unitId),g=(h==null||(t=h.getSheetBySheetId(r.subUnitId))==null?void 0:t.getName())??r.sheetName,[_,v]=(0,f.useState)(r.range),[y,b]=(0,f.useState)(g),[x,S]=(0,f.useState)(``);(0,f.useEffect)(()=>{v(r.range),b(g)},[g,r]);async function C(e){let t=e.split(`,`).filter(Boolean).map(p.deserializeRangeWithSheet)[0];if(!t||!h){S(u.t(`sheets-pivot-ui.msg.emptySourceRange`));return}let n=t.sheetName||r.sheetName,i=h.getSheetBySheetName(n),l=yt(i,t.range);if(l!==2||!i){let e=l===0?`sheets-pivot-ui.msg.emptySourceRange`:`sheets-pivot-ui.msg.invalidSize`;S(u.t(e))}else S(``),v(t.range),b(n),await xt(c,o,{unitId:r.unitId,subUnitId:i.getSheetId(),range:t.range,sheetName:n},a.unitId,a.subUnitId),await s()}return/* @__PURE__ */ (0,d.jsxs)(`section`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:u.t(`sheets-pivot-ui.sourceRangeLabel`)}),/* @__PURE__ */ (0,d.jsx)(m.MobileRangeSelector,{unitId:r.unitId,subUnitId:r.subUnitId,initialValue:(0,p.serializeRangeWithSheet)(y,_),maxRangeCount:1,keepSheetReference:!0,supportAcrossSheet:!0,onChange:(e,t)=>C(t).catch(e=>{l.error(`[MobilePivotSourceRange]`,e)})}),x&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-2 univer-text-xs univer-text-red-500`,children:x})]})}function gn(e){let{pivotTableId:r,version:a,unitId:s,subUnitId:c,panelInfo:l}=e,u=(0,i.useDependency)(t.SheetsPivotTableConfigModel),p=(0,i.useDependency)(t.SheetsPivotRPCService),m=(0,i.useDependency)(n.ILogService),h=(0,f.useMemo)(()=>u.watchPivotTableChanges(s,c,r).pipe((0,o.debounceTime)(0)),[u,s,c,r]),g=(0,i.useObservable)(h),[_,v]=(0,f.useState)(l),y=(0,f.useCallback)(async()=>{if(!u.getPivotTableConfig(s,c,r))return;let e=await p.getPivotDisplayConfig({unitId:s,subUnitId:c,pivotTableId:r,params:{type:t.PivotDisplayDataType.Panel,panel:{pivotTableId:r}}});e.result.panel&&v(e.result.panel)},[u,r,p,c,s]);return(0,f.useEffect)(()=>{y().catch(e=>m.error(`[MobilePivotPanel]`,e))},[g,m,y,a]),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-5 univer-py-4`,children:[/* @__PURE__ */ (0,d.jsx)(hn,{sourceRangeInfo:_.sourceRange,positionInfo:_.positionInfo,pivotTableId:r,refresh:y}),/* @__PURE__ */ (0,d.jsx)(mn,{info:_,pivotTableId:r,unitId:s,subUnitId:c,refresh:y}),/* @__PURE__ */ (0,d.jsx)(un,{info:_,pivotTableId:r,unitId:s,subUnitId:c,refresh:y})]})}function _n(e){let{className:t,unitId:r,subUnitId:a,pivotTableId:o}=e,[c,l]=(0,f.useState)(`fields`),u=(0,i.useDependency)(n.LocaleService);return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:(0,s.clsx)(`univer-flex univer-size-full univer-min-h-0 univer-flex-col`,t),dir:u.getDirection(),children:[/* @__PURE__ */ (0,d.jsx)(s.Segmented,{className:`univer-sticky univer-top-0 univer-z-10 univer-my-3 univer-w-full univer-shrink-0`,items:[{label:u.t(`sheets-pivot-ui.fieldPanel`),value:`fields`},{label:u.t(`sheets-pivot-ui.configPanel`),value:`config`}],value:c,onChange:l}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-min-h-0 univer-flex-1 univer-overflow-y-auto`,children:c===`fields`?/* @__PURE__ */ (0,d.jsx)(gn,{...e}):/* @__PURE__ */ (0,d.jsx)($t,{unitId:r,subUnitId:a,pivotTableId:o})})]})}function vn(e,t){switch(e){case 1:return t.t(`sheets-pivot-ui.msg.invalidSize`);case 0:return t.t(`sheets-pivot-ui.msg.emptySourceRange`);default:return``}}function yn(e){let{unitId:r,subUnitId:a,sourceRange:o,positionRange:c,sourceSheetName:l,positionSheetName:u,onConfirm:h,onCancel:g}=e,_=(0,i.useDependency)(n.LocaleService),v=(0,i.useDependency)(n.IUniverInstanceService).getUnit(r),[y,b]=(0,f.useState)(o),[x,S]=(0,f.useState)(c),[C,w]=(0,f.useState)(l),[T,E]=(0,f.useState)(u),[D,O]=(0,f.useState)(t.PositionType.New),[k,A]=(0,f.useState)(``),[j,M]=(0,f.useState)(!0);(0,f.useEffect)(()=>{let e=yt(v.getSheetBySheetName(C),y);A(vn(e,_))},[_,y,C,v]);function N(e){let t=e.split(`,`).filter(Boolean).map(p.deserializeRangeWithSheet)[0];t?(b(t.range),w(t.sheetName||C)):A(_.t(`sheets-pivot-ui.msg.emptySourceRange`))}function P(e){let t=e.split(`,`).filter(Boolean).map(p.deserializeRangeWithSheet)[0];if(!t){S(null);return}let r=t.sheetName||l;S(t.range),E(r),M(r!==C||!n.Rectangle.intersects(t.range,y))}function F(){if(k)return;let e;if(D===t.PositionType.Existing){if(!x||!j)return;e={sourceRange:y,sourceSheetName:C,positionRange:x,positionSheetName:T,positionType:D}}else e={sourceRange:y,sourceSheetName:C,positionType:D};h(e)}return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-5`,dir:_.getDirection(),children:[/* @__PURE__ */ (0,d.jsxs)(`section`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:_.t(`sheets-pivot-ui.dialog.source.sourceData`)}),/* @__PURE__ */ (0,d.jsx)(m.MobileRangeSelector,{unitId:r,subUnitId:a,initialValue:(0,p.serializeRangeWithSheet)(C,y),maxRangeCount:1,supportAcrossSheet:!0,keepSheetReference:!0,onChange:(e,t)=>N(t)}),k&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-2 univer-text-xs univer-text-red-500`,children:k})]}),/* @__PURE__ */ (0,d.jsxs)(`section`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:_.t(`sheets-pivot-ui.dialog.source.position`)}),/* @__PURE__ */ (0,d.jsxs)(s.RadioGroup,{className:`univer-flex univer-flex-col univer-gap-3`,value:D,onChange:e=>O(e),children:[/* @__PURE__ */ (0,d.jsx)(s.Radio,{value:t.PositionType.New,children:_.t(`sheets-pivot-ui.dialog.source.newSheet`)}),/* @__PURE__ */ (0,d.jsx)(s.Radio,{value:t.PositionType.Existing,children:_.t(`sheets-pivot-ui.dialog.source.existingSheet`)})]}),D===t.PositionType.Existing&&/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mt-3`,children:[/* @__PURE__ */ (0,d.jsx)(m.MobileRangeSelector,{unitId:r,subUnitId:a,initialValue:x?(0,p.serializeRangeWithSheet)(T,x):``,maxRangeCount:1,supportAcrossSheet:!0,keepSheetReference:!0,onChange:(e,t)=>P(t)}),!j&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-2 univer-text-xs univer-text-red-500`,children:_.t(`sheets-pivot-ui.dialog.source.intersect`)})]})]}),/* @__PURE__ */ (0,d.jsxs)(s.MobileActionRowGroup,{children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:g,size:`middle`,children:_.t(`sheets-pivot-ui.dialog.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,onClick:F,size:`middle`,children:_.t(`sheets-pivot-ui.dialog.ok`)})]})]})}let bn=class extends n.Disposable{constructor(e,t){super(),[[A,yn],[_,_n],[y,Jt],[x,qt],[C,Kt],[j,ut]].forEach(([t,n])=>{this.disposeWithMe(e.register(t,n))}),this.disposeWithMe(t.register({PivotTableIcon:u.PivotTableIcon}))}};bn=K([G(0,(0,n.Inject)(i.ComponentManager)),G(1,(0,n.Inject)(i.IconManager))],bn);function xn(e){return{id:N.id,commandId:N.id,type:i.MenuItemType.BUTTON,icon:`PivotTableIcon`,title:`sheets-pivot-ui.title`,tooltip:`sheets-pivot-ui.title`,hidden$:(0,i.getMenuHiddenObservable)(e,n.UniverInstanceType.UNIVER_SHEET),disabled$:(0,a.getObservableWithExclusiveRange$)(e,(0,a.getCurrentRangeDisable$)(e,{workbookTypes:[r.WorkbookEditablePermission],worksheetTypes:[r.WorksheetViewPermission,r.WorksheetPivotTablePermission],rangeTypes:[r.RangeProtectionPermissionViewPoint]}))}}const Sn={[i.RibbonDataGroup.ORGANIZATION]:{[N.id]:{order:1,gridLayout:{row:2,column:1},menuItemFactory:xn}},[i.ContextMenuPosition.MAIN_AREA]:{[i.ContextMenuGroup.DATA]:{[N.id]:{order:1,menuItemFactory:xn}}}};let Cn=class extends n.Disposable{constructor(e){super(),e.mergeMenu(Sn)}};Cn=K([G(0,i.IMenuManagerService)],Cn);let wn=class extends n.Disposable{constructor(e,r,i,a){super(),this._sidebarService=i,this._dialogService=a,W(this,`_pivotTableId`,null),this.disposeWithMe(e.onCommandExecuted(e=>{e.id===it.id?this._pivotTableId=e.params.pivotTableId:e.id===t.RemovePivotTableMutation.id&&e.params.pivotTableId===this._pivotTableId&&this._closePanel()}));let s=r.getCurrentUnitOfType(n.UniverInstanceType.UNIVER_SHEET);s&&(this.disposeWithMe(s.activeSheet$.pipe((0,o.skip)(1)).subscribe(()=>this._closePanel())),this.disposeWithMe(s.sheetDisposed$.subscribe(()=>this._closePanel()))),this.disposeWithMe(this._sidebarService.sidebarOptions$.subscribe(e=>{e.id===`UNIVER_SHEET_PIVOT_PANEL_ID`&&e.visible===!1&&(this._pivotTableId=null,this._closeChildDialogs())}))}_closePanel(){this._pivotTableId=null,this._sidebarService.close(v),this._closeChildDialogs()}_closeChildDialogs(){this._dialogService.close(b),this._dialogService.close(S),this._dialogService.close(w)}};wn=K([G(0,n.ICommandService),G(1,n.IUniverInstanceService),G(2,i.ISidebarService),G(3,i.IDialogService)],wn),new Path2D(`M3.30363 3C2.79117 3 2.51457 3.60097 2.84788 3.99024L6.8 8.60593V12.5662C6.8 12.7184 6.8864 12.8575 7.02289 12.9249L8.76717 13.7863C8.96655 13.8847 9.2 13.7396 9.2 13.5173V8.60593L13.1521 3.99024C13.4854 3.60097 13.2088 3 12.6964 3H3.30363Z`);var Y=class{static drawNoSetting(e,t,n,r){e.save(),l.Rect.drawWith(e,{radius:2,width:16,height:16,fill:r}),e.lineCap=`square`,e.strokeStyle=n,e.scale(t/16,t/16),e.beginPath(),e.lineWidth=1,e.lineCap=`round`,e.moveTo(3,4),e.lineTo(13,4),e.moveTo(4.5,8),e.lineTo(11.5,8),e.moveTo(6,12),e.lineTo(10,12),e.stroke(),e.restore()}static drawExpand(e,t,n,r,i){let a=(i-t)/2;e.save(),e.fillStyle=r,e.strokeStyle=n,e.beginPath(),e.moveTo(6,a),e.lineTo(4+t-2,a),e.arcTo(4+t,a,4+t,a+2,2),e.lineTo(4+t,a+t-2),e.arcTo(4+t,a+t,4+t-2,a+t,2),e.lineTo(6,a+t),e.arcTo(4,a+t,4,a+t-2,2),e.lineTo(4,a+2),e.arcTo(4,a,6,a,2),e.closePath(),e.stroke(),e.fill(),e.beginPath(),e.moveTo(4+t/2,a+2),e.lineTo(4+t/2,a+t-2),e.moveTo(6,a+t/2),e.lineTo(4+t-2,a+t/2),e.stroke(),e.restore()}static drawCollapse(e,t,n,r,i){let a=(i-t)/2;e.save(),e.fillStyle=r,e.strokeStyle=n,e.beginPath(),e.moveTo(6,a),e.lineTo(4+t-2,a),e.arcTo(4+t,a,4+t,a+2,2),e.lineTo(4+t,a+t-2),e.arcTo(4+t,a+t,4+t-2,a+t,2),e.lineTo(6,a+t),e.arcTo(4,a+t,4,a+t-2,2),e.lineTo(4,a+2),e.arcTo(4,a,6,a,2),e.closePath(),e.fill(),e.stroke(),e.beginPath(),e.moveTo(6,a+t/2),e.lineTo(4+t-2,a+t/2),e.stroke(),e.restore()}static drawIconByPath(e,t,n,r){e.save(),e.strokeStyle=n,e.fillStyle=r,l.Rect.drawWith(e,{radius:2,width:16,height:16,fill:r}),t.forEach(t=>{let r=new Path2D(t);e.fillStyle=n,e.fill(r,`evenodd`)}),e.restore()}};const Tn=[`M3.30363 3C2.79117 3 2.51457 3.60097 2.84788 3.99024L6.8 8.60593V12.5662C6.8 12.7184 6.8864 12.8575 7.02289 12.9249L8.76717 13.7863C8.96655 13.8847 9.2 13.7396 9.2 13.5173V8.60593L13.1521 3.99024C13.4854 3.60097 13.2088 3 12.6964 3H3.30363Z`],En=[`M12.4008 13.1831C12.6907 13.1831 12.9258 12.9481 12.9258 12.6581V4.60873L14.013 5.69597C14.218 5.901 14.5505 5.901 14.7555 5.69597C14.9605 5.49094 14.9605 5.15853 14.7555 4.95351L12.7721 2.97017C12.5671 2.76515 12.2347 2.76515 12.0297 2.97017L10.0463 4.95351C9.84132 5.15853 9.84132 5.49094 10.0463 5.69597C10.2514 5.901 10.5838 5.901 10.7888 5.69597L11.8758 4.60901V12.6581C11.8758 12.9481 12.1108 13.1831 12.4008 13.1831Z`,`M1.28069 4.85447C0.842195 4.33439 1.21191 3.5391 1.89218 3.5391H8.59333C9.2736 3.5391 9.64331 4.33439 9.20482 4.85447L6.51052 8.0501V11.6601C6.51052 12.2245 5.94174 12.6114 5.41683 12.404L4.48092 12.0343C4.1756 11.9136 3.97498 11.6187 3.97498 11.2904V8.0501L1.28069 4.85447Z`],Dn=[`M12.4008 2.81641C12.6907 2.81641 12.9258 3.05146 12.9258 3.34141V11.3908L14.013 10.3036C14.218 10.0986 14.5505 10.0986 14.7555 10.3036C14.9605 10.5086 14.9605 10.841 14.7555 11.046L12.7721 13.0294C12.5671 13.2344 12.2347 13.2344 12.0297 13.0294L10.0463 11.046C9.84132 10.841 9.84132 10.5086 10.0463 10.3036C10.2514 10.0986 10.5838 10.0986 10.7888 10.3036L11.8758 11.3905V3.34141C11.8758 3.05146 12.1108 2.81641 12.4008 2.81641Z`,`M1.28069 4.85444C0.842195 4.33435 1.21191 3.53906 1.89218 3.53906H8.59333C9.2736 3.53906 9.64331 4.33435 9.20482 4.85443L6.51052 8.05006V11.6601C6.51052 12.2245 5.94174 12.6113 5.41683 12.404L4.48092 12.0342C4.1756 11.9136 3.97498 11.6186 3.97498 11.2903V8.05006L1.28069 4.85444Z`],On=[`M11.9003 13.7046C11.9003 13.9969 11.6633 14.2339 11.371 14.2339C11.0787 14.2339 10.8417 13.9969 10.8417 13.7046V3.57272L9.74577 4.66862C9.53906 4.87534 9.20391 4.87534 8.9972 4.66862C8.79048 4.46191 8.79048 4.12676 8.9972 3.92005L10.9969 1.92039C11.2036 1.71368 11.5387 1.71368 11.7454 1.92039L13.7451 3.92005C13.9518 4.12676 13.9518 4.46191 13.7451 4.66862C13.5384 4.87534 13.2032 4.87534 12.9965 4.66862L11.9003 3.57243V13.7046Z`,`M2.69779 10.0113C2.40546 10.0113 2.16847 9.77429 2.16847 9.48196C2.16847 9.18962 2.40546 8.95264 2.69779 8.95264H6.67804C6.89213 8.95264 7.08514 9.0816 7.16707 9.2794C7.249 9.47719 7.20371 9.70486 7.05233 9.85624L3.97569 12.9329H6.67804C6.97038 12.9329 7.20736 13.1699 7.20736 13.4622C7.20736 13.7545 6.97038 13.9915 6.67804 13.9915H2.69779C2.4837 13.9915 2.29069 13.8626 2.20876 13.6648C2.12684 13.467 2.17212 13.2393 2.32351 13.0879L5.40015 10.0113H2.69779Z`,`M5.51638 2.58693C5.23363 1.81542 4.14248 1.81543 3.85973 2.58693L2.13245 7.29995C2.03185 7.57443 2.17281 7.87849 2.4473 7.97909C2.72178 8.07969 3.02584 7.93872 3.12644 7.66424L3.64346 6.25351L3.64504 6.25351H5.73266L6.24968 7.66424C6.35027 7.93872 6.65433 8.07969 6.92882 7.97909C7.2033 7.87849 7.34426 7.57443 7.24367 7.29995L5.51638 2.58693ZM5.34467 5.19487L4.68806 3.40325L4.03144 5.19487H5.34467Z`],kn=[`M11.9003 2.29495C11.9003 2.00261 11.6633 1.76562 11.371 1.76562C11.0787 1.76562 10.8417 2.00261 10.8417 2.29495V12.4268L9.74577 11.3309C9.53906 11.1242 9.20391 11.1242 8.9972 11.3309C8.79048 11.5376 8.79048 11.8727 8.9972 12.0795L10.9969 14.0791C11.2036 14.2858 11.5387 14.2858 11.7454 14.0791L13.7451 12.0795C13.9518 11.8727 13.9518 11.5376 13.7451 11.3309C13.5384 11.1242 13.2032 11.1242 12.9965 11.3309L11.9003 12.4271V2.29495Z`,`M2.69792 10.0113C2.40558 10.0113 2.16859 9.77429 2.16859 9.48196C2.16859 9.18962 2.40558 8.95264 2.69792 8.95264H6.67816C6.89225 8.95264 7.08526 9.0816 7.16719 9.2794C7.24912 9.47719 7.20384 9.70486 7.05245 9.85624L3.97581 12.9329H6.67816C6.9705 12.9329 7.20749 13.1699 7.20749 13.4622C7.20749 13.7545 6.9705 13.9915 6.67816 13.9915H2.69792C2.48383 13.9915 2.29082 13.8626 2.20889 13.6648C2.12696 13.467 2.17224 13.2393 2.32363 13.0879L5.40027 10.0113H2.69792Z`,`M5.5165 2.58693C5.23375 1.81542 4.1426 1.81543 3.85985 2.58693L2.13257 7.29995C2.03197 7.57443 2.17294 7.8785 2.44742 7.97909C2.7219 8.07969 3.02596 7.93872 3.12656 7.66424L3.64358 6.25351L3.64516 6.25351H5.73278L6.2498 7.66424C6.35039 7.93872 6.65446 8.07969 6.92894 7.97909C7.20342 7.8785 7.34438 7.57443 7.24379 7.29995L5.5165 2.58693ZM5.34479 5.19487L4.68818 3.40325L4.03156 5.19487H5.34479Z`],An=/* @__PURE__ */ new Set([e.PivotCellStyleTypeEnum.FilterAll,e.PivotCellStyleTypeEnum.FilterPartial,e.PivotCellStyleTypeEnum.FilterSingle,e.PivotCellStyleTypeEnum.FilterNoneSortNone,e.PivotCellStyleTypeEnum.FilteredSortAsc,e.PivotCellStyleTypeEnum.FilteredSortDesc,e.PivotCellStyleTypeEnum.FilteredSortNone,e.PivotCellStyleTypeEnum.FilterNoneSortAsc,e.PivotCellStyleTypeEnum.FilterNoneSortDesc]),jn=/* @__PURE__ */ new Set([e.PivotCellStyleTypeEnum.Expand,e.PivotCellStyleTypeEnum.Collapse]);let Mn=class extends l.Shape{constructor(e,t,n,r){super(e,t),this._commandService=n,this._themeService=r,W(this,`_cellWidth`,0),W(this,`_cellHeight`,0),W(this,`_hovered`,!1),W(this,`_pivotParams`,void 0),this.setShapeProps(t),this.onPointerDown$.subscribeEvent(e=>this.onPointerDown(e)),this.onPointerEnter$.subscribeEvent(()=>this.onPointerEnter()),this.onPointerLeave$.subscribeEvent(()=>this.onPointerLeave())}setShapeProps(e){e.cellHeight!==void 0&&(this._cellHeight=e.cellHeight),e.cellWidth!==void 0&&(this._cellWidth=e.cellWidth),e.pivotParams!==void 0&&(this._pivotParams=e.pivotParams),this.transformByState({width:e.width,height:e.height})}onPointerDown(n){if(n.button===2)return;let{col:r,row:i,unitId:a,subUnitId:o,buttonType:s}=this._pivotParams;An.has(s)?setTimeout(()=>{this._commandService.executeCommand(tt.id,{row:i,col:r,unitId:a,subUnitId:o})},200):this._commandService.executeCommand(t.SetPivotCollapseCommand.id,{row:i,col:r,unitId:a,subUnitId:o,collapse:s===e.PivotCellStyleTypeEnum.Expand})}onPointerEnter(){this._hovered=!0,this.setCursor(l.CURSOR_TYPE.POINTER),this.makeDirty(!0)}onPointerLeave(){this._hovered=!1,this.setCursor(l.CURSOR_TYPE.DEFAULT),this.makeDirty(!0)}_draw(t){var n,r;if(!((n=this._pivotParams)!=null&&n.buttonType))return;let i=this._themeService.getColorFromTheme(`primary.600`),a=this._themeService.getColorFromTheme(`gray.50`);t.save();let o=new Path2D;o.rect(0,0,this._cellWidth,this._cellHeight),t.clip(o),((r=this._pivotParams)==null?void 0:r.buttonType)===e.PivotCellStyleTypeEnum.Collapse?Y.drawExpand(t,12,i,a,this._cellHeight):this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.Expand?Y.drawCollapse(t,12,i,a,this._cellHeight):this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilterAll||this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilterNoneSortNone?Y.drawNoSetting(t,16,i,a):this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilterPartial||this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilterSingle||this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilteredSortNone?Y.drawIconByPath(t,Tn,i,a):this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilterNoneSortAsc?Y.drawIconByPath(t,On,i,a):this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilterNoneSortDesc?Y.drawIconByPath(t,kn,i,a):this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilteredSortAsc?Y.drawIconByPath(t,En,i,a):this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilteredSortDesc&&Y.drawIconByPath(t,Dn,i,a),t.restore()}};Mn=K([G(2,n.ICommandService),G(3,(0,n.Inject)(n.ThemeService))],Mn);let Nn=class extends l.Shape{constructor(e,t,n,r){super(e,t),this._commandService=n,this._themeService=r,W(this,`_cellWidth`,void 0),W(this,`_cellHeight`,void 0),W(this,`_pivotParams`,void 0),this._cellWidth=t.cellWidth,this._cellHeight=t.cellHeight,this._pivotParams=t.pivotParams,this.transformByState({width:t.width,height:t.height}),this.onPointerUp$.subscribeEvent(e=>this._handlePointerUp(e))}_handlePointerUp(n){if(n.button===2)return;let{col:r,row:i,unitId:a,subUnitId:o,buttonType:s}=this._pivotParams;if(An.has(s))return this._commandService.executeCommand(tt.id,{row:i,col:r,unitId:a,subUnitId:o});if(jn.has(s))return this._commandService.executeCommand(t.SetPivotCollapseCommand.id,{row:i,col:r,unitId:a,subUnitId:o,collapse:s===e.PivotCellStyleTypeEnum.Expand})}_draw(t){let n=this._pivotParams.buttonType,r=this._themeService.getColorFromTheme(`primary.600`),i=this._themeService.getColorFromTheme(`gray.50`);t.save();let a=new Path2D;a.rect(0,0,this._cellWidth,this._cellHeight),t.clip(a),t.translate(6,6),n===e.PivotCellStyleTypeEnum.Collapse?Y.drawExpand(t,12,r,i,this._cellHeight):n===e.PivotCellStyleTypeEnum.Expand?Y.drawCollapse(t,12,r,i,this._cellHeight):n===e.PivotCellStyleTypeEnum.FilterAll||n===e.PivotCellStyleTypeEnum.FilterNoneSortNone?Y.drawNoSetting(t,16,r,i):n===e.PivotCellStyleTypeEnum.FilterPartial||n===e.PivotCellStyleTypeEnum.FilterSingle||n===e.PivotCellStyleTypeEnum.FilteredSortNone?Y.drawIconByPath(t,Tn,r,i):n===e.PivotCellStyleTypeEnum.FilterNoneSortAsc?Y.drawIconByPath(t,On,r,i):n===e.PivotCellStyleTypeEnum.FilterNoneSortDesc?Y.drawIconByPath(t,kn,r,i):n===e.PivotCellStyleTypeEnum.FilteredSortAsc?Y.drawIconByPath(t,En,r,i):n===e.PivotCellStyleTypeEnum.FilteredSortDesc&&Y.drawIconByPath(t,Dn,r,i),t.restore()}};Nn=K([G(2,n.ICommandService),G(3,(0,n.Inject)(n.ThemeService))],Nn);function Pn(e,t,n){let{startX:r,startY:i,endX:a,endY:o}=n;return{left:(t===`filter`?e!==`rtl`:e===`rtl`)?a-16-1:r+1,top:t===`filter`?o-16-1:i+1}}let Fn=class extends n.Disposable{constructor(e,t,n,r,i,a,o,s,c,l){super(),this._context=e,this._injector=t,this._adaptorModel=n,this._skeletonManagerService=r,this._configModel=i,this._permissionController=a,this._permissionService=o,this._localeService=s,this._dialogService=c,this._contextMenuController=l,W(this,`_buttonShapes`,[]),this._initContextMenu(),this._initButtonRender(),this._initSkeleton()}dispose(){this._clearButtons(),super.dispose()}_clearButtons(){this._buttonShapes.forEach(e=>e.dispose()),this._buttonShapes.length=0}_initContextMenu(){if(!this._contextMenuController)return;let e=this._contextMenuController.contextMenuInterceptor.intercept(a.MOBILE_CONTEXT_MENU,{handler:(e,t,n)=>{if(e||t.menuPosition!==i.ContextMenuPosition.MAIN_AREA)return n(e);let{col:r,row:a,subUnitId:o,unitId:s}=t;if(r==null||a==null)return n(e);let c=this._adaptorModel.getPivotTableIdByCell(s,o,a,r);return c?(this._openPivotActionPanel(s,o,c,a,r),!0):n(e)}});this.disposeWithMe((0,n.toDisposable)(e))}_openPivotActionPanel(e,t,n,i,a){let o=this._permissionController.permissionCheckWithRanges({workbookTypes:[r.WorkbookEditablePermission],worksheetTypes:[r.WorksheetPivotTablePermission],rangeTypes:[r.RangeProtectionPermissionEditPoint]},[{startRow:i,endRow:i,startColumn:a,endColumn:a}],e,t);this._dialogService.open({id:M,title:{title:this._localeService.t(`sheets-pivot-ui.title`)},children:{label:{name:j,props:{editable:o,pivotTableId:n,subUnitId:t,unitId:e}}},onClose:()=>this._dialogService.close(M)})}_initButtonRender(){this.disposeWithMe((0,o.merge)(this._adaptorModel.viewUpdate$.pipe((0,o.auditTime)(0)),this._skeletonManagerService.currentSkeleton$,this._configModel.pivotConfigChange$,this._permissionService.permissionPointUpdate$.pipe((0,o.auditTime)(100))).subscribe(()=>{let e=this._context.unit,t=this._skeletonManagerService.getCurrentSkeleton();if(!e||!t)return;let n=e.getActiveSheet(),i=e.getUnitId(),o=n.getSheetId(),s=this._localeService.getDirection(),{scene:c}=this._context;this._clearButtons(),this._adaptorModel.getSubUnitPivotMatrix(i,o).forValue((e,n,l)=>{if(!(l!=null&&l.pbt))return;let{startX:u,startY:d,endX:f,endY:p}=(0,a.getCoordByCell)(e,n,c,t),m=f-u,h=p-d;if(h<8||m<8||!this._permissionController.permissionCheckWithRanges({workbookTypes:[r.WorkbookEditablePermission],worksheetTypes:[r.WorksheetViewPermission,r.WorksheetEditPermission,r.WorksheetPivotTablePermission],rangeTypes:[r.RangeProtectionPermissionViewPoint,r.RangeProtectionPermissionEditPoint]},[{startRow:e,endRow:e,startColumn:n,endColumn:n}],i,o))return;let g;if(An.has(l.pbt))g=`filter`;else if(jn.has(l.pbt))g=`collapse`;else return;let _=Pn(s,g,{startX:u,startY:d,endX:f,endY:p}),v={left:_.left-6,top:_.top-6,width:28,height:28,zIndex:5e3,cellWidth:m,cellHeight:h,pivotParams:{row:e,col:n,unitId:i,subUnitId:o,buttonType:l.pbt}},y=`sheets-mobile-pivot-button-${e}-${n}-${i}-${o}`;this._buttonShapes.push(this._injector.createInstance(Nn,y,v))}),this._buttonShapes.length&&c.addObjects(this._buttonShapes),c.makeDirty()}))}_initSkeleton(){var e;this.disposeWithMe(this._adaptorModel.markDirty$.subscribe(({unitId:e,subUnitId:t})=>{if(e===this._context.unitId&&(this._skeletonManagerService.makeDirty({sheetId:t}),this._context.unit.getActiveSheet().getSheetId()===t)){var n;this._skeletonManagerService.reCalculate(),(n=this._context.mainComponent)==null||n.makeDirty()}}));let t=this._context.unitId;this._context.unit.getSheets().forEach(e=>{var n;let r=e.getSheetId();(n=this._adaptorModel.getSubUnitPivotRenderCache(t,r))!=null&&n.size&&this._skeletonManagerService.makeDirty({sheetId:r})}),this._skeletonManagerService.reCalculate(),(e=this._context.mainComponent)==null||e.makeDirty()}};Fn=K([G(1,(0,n.Inject)(n.Injector)),G(2,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),G(3,(0,n.Inject)(a.SheetSkeletonManagerService)),G(4,(0,n.Inject)(t.SheetsPivotTableConfigModel)),G(5,(0,n.Inject)(r.SheetPermissionCheckController)),G(6,(0,n.Inject)(n.IPermissionService)),G(7,(0,n.Inject)(n.LocaleService)),G(8,i.IDialogService),G(9,(0,n.Optional)(a.SheetContextMenuMobileRenderController))],Fn);let X=class extends n.Disposable{constructor(e,t,n){super(),this._univerInstanceService=e,this._sheetsPivotTableAdaptorModel=t,this._sheetClipboardService=n,this._initialize()}_initialize(){let e=this,r,i;this.disposeWithMe(this._sheetClipboardService.addClipboardHook({id:t.SHEET_PIVOT_TABLE_PLUGIN,onBeforeCopy(t,a){let o=e._univerInstanceService.getUnit(t,n.UniverInstanceType.UNIVER_SHEET);o&&(r=e._sheetsPivotTableAdaptorModel.getSubUnitPivotMatrix(t,a),i=o.getStyles())},onAfterCopy(){r=null,i=null},getCellValueBySpecialMatrix(e,t){if(!r)return;let n=r==null?void 0:r.getValue(e,t);if(n){var a;return{...n,s:i==null?void 0:i.get(n.s),displayV:((a=n.v)==null?void 0:a.toString())||``}}}}))}};X=K([G(0,(0,n.Inject)(n.IUniverInstanceService)),G(1,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),G(2,a.ISheetClipboardService)],X);const In=[r.InsertRowCommand.id,r.RemoveColCommand.id,r.RemoveRowCommand.id,r.InsertColCommand.id];let Ln=class extends n.Disposable{constructor(e,n,r,i,a,o,s,c,l){var u;super(),this._injector=e,this._univerInstanceService=n,this._sheetsPivotTableAdaptorModel=r,this._sheetInterceptorService=i,this._confirmService=a,this._localeService=o,this._sheetsPivotTableConfigModel=s,this._messageService=c,this._configService=l,W(this,`_defaultOverride`,!1),this._initMoveRangeConfirmCheck(),this._initRowColOperationCheck(),this._defaultOverride=((u=this._configService.getConfig(t.SHEETS_PIVOT_PLUGIN_CONFIG_KEY))==null?void 0:u.defaultOverride)??!1}_initMoveRangeConfirmCheck(){this.disposeWithMe(this._sheetInterceptorService.interceptBeforeCommand({performCheck:async e=>{if(e.id===r.MoveRangeCommand.id){let n=(0,r.getSheetCommandTarget)(this._univerInstanceService);if(!n)return Promise.resolve(!1);let{unitId:i,subUnitId:a}=n,{fromRange:o,toRange:s}=e.params,c=this._sheetsPivotTableConfigModel.getSubUnitPivotConfigs(i,a);if(!(c!=null&&c.size))return Promise.resolve(!0);for(let[e,n]of c){let{targetCellInfo:r}=n,{row:c,col:l}=r,u=c-o.startRow,d=l-o.startColumn,f={row:s.startRow+u,col:s.startColumn+d},p=f.row-r.row,m=f.col-r.col,h=this._sheetsPivotTableAdaptorModel.getPivotItemRenderInfoCache(i,a,e),g=h==null?void 0:h.rangesInfo;if(!g)return Promise.resolve(!0);let _=[];Object.keys(g).forEach(e=>{g[e].forEach(e=>{let t={...e};t.startRow=e.startRow+p,t.endRow=e.endRow+p,t.startColumn=e.startColumn+m,t.endColumn=e.endColumn+m,_.push(t)})});let v=this._sheetInterceptorService.generateMutationsByRanges({unitId:i,subUnitId:a,ranges:_}),y=(0,t.generateSetRangeValueMutations)(this._injector,i,a,_);if(v.redos.length||y.shouldClear){if(this._defaultOverride)return Promise.resolve(!0);{let e=await this._confirmService.confirm({id:`sheet-pivot.confirm.add-sheet-pivot`,title:{title:this._localeService.t(`sheets-pivot-ui.perform.tips`)},children:{title:this._localeService.t(`sheets-pivot-ui.perform.info`)},cancelText:this._localeService.t(`sheets-pivot-ui.perform.cancel`),confirmText:this._localeService.t(`sheets-pivot-ui.perform.confirm`)});return Promise.resolve(e)}}}}return Promise.resolve(!0)}}))}_initRowColOperationCheck(){this.disposeWithMe(this._sheetInterceptorService.interceptBeforeCommand({performCheck:async e=>{if(In.includes(e.id)){let t=e.params,{range:i}=t,a=(0,r.getSheetCommandTarget)(this._univerInstanceService),o=t.unitId||(a==null?void 0:a.unitId),c=t.subUnitId||(a==null?void 0:a.subUnitId);if(!o||!c)return Promise.resolve(!0);let l=this._sheetsPivotTableConfigModel.getSubUnitPivotConfigs(o,c);if(l!=null&&l.size)for(let[e,t]of l){let t=this._sheetsPivotTableAdaptorModel.getPivotItemRenderInfoCache(o,c,e);if(t!=null&&t.rangesInfo){for(let e of Object.keys(t.rangesInfo))if(t.rangesInfo[e].some(e=>n.Rectangle.intersects(e,i)))return this._messageService.show({type:s.MessageType.Error,content:this._localeService.t(`sheets-pivot-ui.perform.rowColError`)}),Promise.resolve(!1)}}}return Promise.resolve(!0)}}))}};Ln=K([G(0,(0,n.Inject)(n.Injector)),G(1,(0,n.Inject)(n.IUniverInstanceService)),G(2,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),G(3,(0,n.Inject)(r.SheetInterceptorService)),G(4,(0,n.Inject)(n.IConfirmService)),G(5,(0,n.Inject)(n.LocaleService)),G(6,(0,n.Inject)(t.SheetsPivotTableConfigModel)),G(7,(0,n.Inject)(i.IMessageService)),G(8,(0,n.Inject)(n.IConfigService))],Ln);const Rn=[h.InsertTextCommand.id,g.IMEInputCommand.id,a.SetCellEditVisibleOperation.id];let zn=class extends n.Disposable{constructor(e,t,n,r,i,a){super(),this._sheetsSelectionsService=e,this._univerInstanceService=t,this._sheetsPivotTableAdaptorModel=n,this._commandService=r,this._sheetsPivotTableConfigModel=i,this._localeService=a,this._initEditListener()}_initEditListener(){this.disposeWithMe(this._commandService.beforeCommandExecuted(e=>{if(Rn.includes(e.id)){var t;let i=(t=this._sheetsSelectionsService.getCurrentLastSelection())==null?void 0:t.primary;if(!i)return;let{actualRow:a,actualColumn:o}=i,s=(0,n.cellToRange)(a,o),c=(0,r.getSheetCommandTarget)(this._univerInstanceService,e.params);if(!c)return;let{unitId:l,subUnitId:u}=c,d=this._sheetsPivotTableConfigModel.getSubUnitPivotConfigs(l,u);if(d!=null&&d.size)for(let[e]of d){let t=this._sheetsPivotTableAdaptorModel.getPivotItemRenderInfoCache(l,u,e);if(t!=null&&t.rangesInfo){for(let e of Object.keys(t.rangesInfo))if(t.rangesInfo[e].some(e=>n.Rectangle.intersects(e,s)))throw new n.CustomCommandExecutionError(this._localeService.t(`sheets-pivot-ui.msg.editCellError`))}}}}))}};zn=K([G(0,(0,n.Inject)(r.SheetsSelectionsService)),G(1,(0,n.Inject)(n.IUniverInstanceService)),G(2,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),G(3,(0,n.Inject)(n.ICommandService)),G(4,(0,n.Inject)(t.SheetsPivotTableConfigModel)),G(5,(0,n.Inject)(n.LocaleService))],zn);let Bn=class extends n.Disposable{constructor(e,t,n,r){super(),this._sheetsPivotTableAdaptorModel=e,this._formatPainterService=t,this._messageService=n,this._localeService=r,this._initFormatPainterListener()}_initFormatPainterListener(){let e=this._sheetsPivotTableAdaptorModel,t=this._messageService,n=this._localeService;this._formatPainterService.addHook({id:`pivot-format-painter-hook`,onBeforeApply(i){let{redoMutationsInfo:a}=i;for(let i of a)if(i.id===r.AddWorksheetMergeMutation.id){let{unitId:r,subUnitId:a,ranges:o}=i.params;for(let i of o)if(e.getIntersectsPivotTable(r,a,i).length>0)return t.show({type:s.MessageType.Error,content:n.t(`sheets-pivot-ui.msg.mergeCellError`)}),!1}return!0}})}};Bn=K([G(0,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),G(1,(0,n.Inject)(a.IFormatPainterService)),G(2,(0,n.Inject)(i.IMessageService)),G(3,(0,n.Inject)(n.LocaleService))],Bn);let Vn=class extends n.Disposable{constructor(e,t,n){super(),this._sheetPrintInterceptorService=e,this._sheetsPivotTableAdaptorModel=t,this._sheetsPivotTableConfigModel=n,this._initPrintArea()}_initPrintArea(){this.disposeWithMe(this._sheetPrintInterceptorService.interceptor.intercept(this._sheetPrintInterceptorService.interceptor.getInterceptPoints().PRINTING_RANGE,{handler:(e,t,r)=>{let{unitId:i,subUnitId:a}=t,o=(e==null?void 0:e.startRow)??1/0,s=(e==null?void 0:e.endRow)??-1/0,c=(e==null?void 0:e.startColumn)??1/0,l=(e==null?void 0:e.endColumn)??-1/0,u=this._sheetsPivotTableConfigModel.getSubUnitPivotConfigs(i,a);if(!u)return r(e);for(let[e,t]of u){var d;let t=(d=this._sheetsPivotTableAdaptorModel.getPivotItemRenderInfoCache(i,a,e))==null?void 0:d.rangesInfo;t&&Object.values(t).forEach(e=>{e.forEach(e=>{let{startRow:t,endRow:n,startColumn:r,endColumn:i}=e;o=Math.min(t,o),s=Math.max(n,s),c=Math.min(r,c),l=Math.max(i,l)})})}let f={startRow:o,endRow:s,startColumn:c,endColumn:l};return(0,n.isValidRange)(f)?r(f):r(e)}}))}};Vn=K([G(0,(0,n.Inject)(a.SheetPrintInterceptorService)),G(1,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),G(2,(0,n.Inject)(t.SheetsPivotTableConfigModel))],Vn);let Hn=class extends n.Disposable{constructor(e,t,n){super(),this._mergeCellController=e,this._univerInstanceService=t,this._sheetsPivotTableConfigModel=n,this._initMenuInterceptor()}_initMenuInterceptor(){this.disposeWithMe(this._mergeCellController.interceptor.intercept(this._mergeCellController.interceptor.getInterceptPoints().MERGE_CELL_INTERCEPTOR_CHECK,{handler:(e,t)=>{let i=(0,r.getSheetCommandTarget)(this._univerInstanceService);if(!i)return e;let{unitId:a,subUnitId:o}=i,s=this._sheetsPivotTableConfigModel.getSubUnitPivotConfigs(a,o),c=[];return s&&s.forEach((e,t)=>{var n;let r=(n=this._sheetsPivotTableConfigModel.getPivotTableRangeInfo(a,o,t))==null?void 0:n.rangeInfo;r&&Object.keys(r).forEach(e=>{let t=r[e];Array.isArray(t)&&c.push(...t)})}),c.length&&t.some(e=>c.some(t=>n.Rectangle.intersects(t,e)))?!0:e}}))}};Hn=K([G(0,(0,n.Inject)(r.MergeCellController)),G(1,(0,n.Inject)(n.IUniverInstanceService)),G(2,(0,n.Inject)(t.SheetsPivotTableConfigModel))],Hn);let Z=class extends n.Plugin{constructor(e=ot,t,r,i,a){super(),this._config=e,this._injector=t,this._commandService=r,this._renderManagerService=i,this._configService=a;let{menu:o,...s}=(0,n.merge)({},ot,this._config);o&&this._configService.setConfig(`menu`,o,{merge:!0}),this._configService.setConfig(at,s),this._registerCommands()}onStarting(){[[bn],[Cn],[wn],[Hn],[Ln],[Vn],[zn],[Bn],[q],[X]].forEach(e=>this._injector.add(e)),(0,n.touchDependencies)(this._injector,[[bn],[Cn],[wn],[q]])}onRendered(){this._registerRenderModules(),(0,n.touchDependencies)(this._injector,[[Ln],[Vn],[zn],[Bn],[X]])}onSteady(){(0,n.touchDependencies)(this._injector,[[Hn]])}_registerCommands(){[it,N,tt,nt,F].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}_registerRenderModules(){this.disposeWithMe(this._renderManagerService.registerRenderModule(n.UniverInstanceType.UNIVER_SHEET,[Fn]))}};W(Z,`pluginName`,`${t.SHEET_PIVOT_TABLE_PLUGIN}_UI_PLUGIN`),W(Z,`packageName`,$e),W(Z,`version`,et),W(Z,`type`,n.UniverInstanceType.UNIVER_SHEET),Z=K([(0,n.DependentOn)(c.UniverLicensePlugin,l.UniverRenderEnginePlugin,r.UniverSheetsPlugin,t.UniverSheetsPivotTablePlugin,a.UniverSheetsMobileUIPlugin,i.UniverMobileUIPlugin),G(1,(0,n.Inject)(n.Injector)),G(2,n.ICommandService),G(3,l.IRenderManagerService),G(4,n.IConfigService)],Z);const Un=[e.PivotExcelDateGroupUnit.Year,e.PivotExcelDateGroupUnit.Quarter,e.PivotExcelDateGroupUnit.Month,e.PivotExcelDateGroupUnit.Day,e.PivotExcelDateGroupUnit.Hour,e.PivotExcelDateGroupUnit.Minute,e.PivotExcelDateGroupUnit.Second],Wn={[e.PivotExcelDateGroupUnit.Year]:`sheets-pivot-ui.dateGroup.year`,[e.PivotExcelDateGroupUnit.Quarter]:`sheets-pivot-ui.dateGroup.quarter`,[e.PivotExcelDateGroupUnit.Month]:`sheets-pivot-ui.dateGroup.month`,[e.PivotExcelDateGroupUnit.Day]:`sheets-pivot-ui.dateGroup.day`,[e.PivotExcelDateGroupUnit.Hour]:`sheets-pivot-ui.dateGroup.hour`,[e.PivotExcelDateGroupUnit.Minute]:`sheets-pivot-ui.dateGroup.minute`,[e.PivotExcelDateGroupUnit.Second]:`sheets-pivot-ui.dateGroup.second`},Gn=`univer-h-8 univer-rounded-md univer-border univer-border-gray-300 univer-bg-gray-0 univer-px-2 univer-text-sm univer-text-gray-900 dark:univer-bg-gray-700 dark:univer-text-gray-0`;function Kn({targetFieldName:t,dateSystem:r,grouping:a,onConfirm:o,onCancel:c}){let l=(0,i.useDependency)(n.LocaleService),u=(0,i.useObservable)(l.direction$,l.getDirection()),p=(e,t)=>{let i=e===void 0?null:(0,n.excelSerialToDateTimeParts)(e,{dateSystem:r});if(!i)return{date:``,time:t};let a=e=>String(e).padStart(2,`0`);return{date:`${i.year}-${a(i.month)}-${a(i.day)}`,time:`${a(i.hours)}:${a(i.minutes)}:${a(i.seconds)}`}},[m,h]=(0,f.useState)((a==null?void 0:a.units)??[e.PivotExcelDateGroupUnit.Day]),[g,_]=(0,f.useState)((a==null?void 0:a.autoStart)??!0),[v,y]=(0,f.useState)((a==null?void 0:a.autoEnd)??!0),[b,x]=(0,f.useState)(()=>p(a==null?void 0:a.start,`00:00:00`).date),[S,C]=(0,f.useState)(()=>p(a==null?void 0:a.end,`23:59:59`).date),[w,T]=(0,f.useState)(()=>p(a==null?void 0:a.start,`00:00:00`).time),[E,D]=(0,f.useState)(()=>p(a==null?void 0:a.end,`23:59:59`).time),[O,k]=(0,f.useState)((a==null?void 0:a.daysInterval)??1),[A,j]=(0,f.useState)(!1),M=(0,f.useRef)(!1),N=m.some(t=>t===e.PivotExcelDateGroupUnit.Hour||t===e.PivotExcelDateGroupUnit.Minute||t===e.PivotExcelDateGroupUnit.Second),P=m.length===1&&m[0]===e.PivotExcelDateGroupUnit.Day,F=(e,t,i)=>{let a=/^(\d{4})-(\d{2})-(\d{2})$/.exec(e),o=/^(\d{2}):(\d{2})(?::(\d{2}))?$/.exec(t);return!a||!o?null:(0,n.excelDateTimePartsToSerial)({year:Number(a[1]),month:Number(a[2]),day:Number(a[3]),hours:N?Number(o[1]):i?23:0,minutes:N?Number(o[2]):i?59:0,seconds:N?Number(o[3]??0):i?59:0,fractionalSecond:i?.999:0},{dateSystem:r})},I=g?void 0:F(b,w,!1),L=v?void 0:F(S,E,!0),R=m.length>0&&(g||I!==null)&&(v||L!==null)&&(I==null||L==null||I<=L)&&(!P||Number.isInteger(O)&&O>=1);return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,dir:u,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium`,children:t}),/* @__PURE__ */ (0,d.jsxs)(`fieldset`,{children:[/* @__PURE__ */ (0,d.jsx)(`legend`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:l.t(`sheets-pivot-ui.dateGroup.groupBy`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-grid univer-grid-cols-2 univer-gap-2`,children:Un.map(e=>/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:m.includes(e),onChange:t=>h(n=>t?Un.filter(t=>t===e||n.includes(t)):n.filter(t=>t!==e)),children:l.t(Wn[e])},e))})]}),P&&/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-items-center univer-justify-between univer-gap-3 univer-text-sm`,children:[l.t(`sheets-pivot-ui.dateGroup.daysInterval`),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-w-24`,value:O,min:1,onChange:e=>k(Number(e))})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:g,onChange:e=>_(!!e),children:l.t(`sheets-pivot-ui.dateGroup.autoStart`)}),!g&&/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-items-center univer-gap-2 univer-text-sm`,children:[l.t(`sheets-pivot-ui.dateGroup.start`),/* @__PURE__ */ (0,d.jsx)(s.Input,{type:`date`,value:b,onChange:x}),N&&/* @__PURE__ */ (0,d.jsx)(`input`,{className:Gn,type:`time`,step:1,value:w,onChange:e=>T(e.target.value)})]}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:v,onChange:e=>y(!!e),children:l.t(`sheets-pivot-ui.dateGroup.autoEnd`)}),!v&&/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-items-center univer-gap-2 univer-text-sm`,children:[l.t(`sheets-pivot-ui.dateGroup.end`),/* @__PURE__ */ (0,d.jsx)(s.Input,{type:`date`,value:S,onChange:C}),N&&/* @__PURE__ */ (0,d.jsx)(`input`,{className:Gn,type:`time`,step:1,value:E,onChange:e=>D(e.target.value)})]})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-justify-end univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:c,children:l.t(`sheets-pivot-ui.dialog.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:!R||A,onClick:async()=>{if(!R||M.current)return;let e={units:Un.filter(e=>m.includes(e)),autoStart:g,autoEnd:v};!g&&I!=null&&(e.start=I),!v&&L!=null&&(e.end=L),P&&O>1&&(e.daysInterval=O),M.current=!0,j(!0);try{await o(e)}catch{}finally{M.current=!1,j(!1)}},children:l.t(`sheets-pivot-ui.dialog.ok`)})]})]})}function qn(t){let n=t.operator===e.ST_PivotFilterOperatorEnum.dateBetween||t.operator===e.ST_PivotFilterOperatorEnum.dateNotBetween,r={type:e.PivotFilterTypeEnum.CustomFilter,operator:t.operator,expected:n?[t.lower,t.upper]:t.lower,useWholeDay:t.useWholeDay};try{return(0,e.compilePivotDateFilter)(r),r}catch{return}}function Jn(e,t){let r=(0,n.excelSerialToDateTimeParts)(e,{dateSystem:t});if(!r||r.day===0||r.year===1900&&r.month===2&&r.day===29)return;let i=new Date(2e3,0,1,12);return i.setFullYear(r.year,r.month-1,r.day),i}function Yn(e,t,r){let i=(0,n.excelDateTimePartsToSerial)({year:t.getFullYear(),month:t.getMonth()+1,day:t.getDate(),hours:0,minutes:0,seconds:0,fractionalSecond:0},{dateSystem:r});return i===null?void 0:i+(e===void 0?0:e-Math.floor(e))}function Xn(e,t){return e===void 0||!Number.isFinite(t.getTime())?e:Math.floor(e)+(t.getHours()*3600+t.getMinutes()*60+t.getSeconds()+t.getMilliseconds()/1e3)/86400}const Zn=[{value:e.ST_PivotFilterOperatorEnum.dateEqual,label:`sheets-pivot-ui.dateFilter.equal`},{value:e.ST_PivotFilterOperatorEnum.dateNotEqual,label:`sheets-pivot-ui.dateFilter.notEqual`},{value:e.ST_PivotFilterOperatorEnum.dateNewerThan,label:`sheets-pivot-ui.dateFilter.after`},{value:e.ST_PivotFilterOperatorEnum.dateNewerThanOrEqual,label:`sheets-pivot-ui.dateFilter.afterOrEqual`},{value:e.ST_PivotFilterOperatorEnum.dateOlderThan,label:`sheets-pivot-ui.dateFilter.before`},{value:e.ST_PivotFilterOperatorEnum.dateOlderThanOrEqual,label:`sheets-pivot-ui.dateFilter.beforeOrEqual`},{value:e.ST_PivotFilterOperatorEnum.dateBetween,label:`sheets-pivot-ui.dateFilter.between`},{value:e.ST_PivotFilterOperatorEnum.dateNotBetween,label:`sheets-pivot-ui.dateFilter.notBetween`}];function Qn({dateSystem:t,defaultDate:r,currentFilter:a,onConfirm:o,onCancel:c}){let l=(0,i.useDependency)(n.LocaleService);(0,i.useObservable)(l.currentLocale$);let u=(0,i.useObservable)(l.direction$,l.getDirection()),[p,m]=(0,f.useState)((a==null?void 0:a.operator)??e.ST_PivotFilterOperatorEnum.dateEqual),h=Array.isArray(a==null?void 0:a.expected)?a.expected[0]:a==null?void 0:a.expected,g=Array.isArray(a==null?void 0:a.expected)?a.expected[1]:void 0,[_,v]=(0,f.useState)(()=>h??r??Yn(void 0,/* @__PURE__ */ new Date,t)),[y,b]=(0,f.useState)(()=>g??h??r??Yn(void 0,/* @__PURE__ */ new Date,t)),[x,S]=(0,f.useState)((a==null?void 0:a.useWholeDay)===!0),[C,w]=(0,f.useState)(!1),[T,E]=(0,f.useState)(!1),D=(0,f.useRef)(!1),O=P(_),k=P(y),A=p===e.ST_PivotFilterOperatorEnum.dateBetween||p===e.ST_PivotFilterOperatorEnum.dateNotBetween,j=qn({operator:p,lower:O,upper:k,useWholeDay:x}),M=j!==void 0&&a!==void 0&&p===a.operator&&x===(a.useWholeDay===!0)&&n.Tools.diffValue(j.expected,a.expected);async function N(e){if(!D.current){D.current=!0,w(!0),E(!1);try{await o(e)||E(!0)}catch{E(!0)}finally{D.current=!1,w(!1)}}}function P(e){return typeof e==`string`?e.trim()===``?void 0:Number(e):e}function F(e,t){t?b(e):v(e)}function I(e,n){let r=Yn(n?k:O,e,t);n?b(r):v(r)}function L(e,t){let n=t?k:O;if(n===void 0)return;let r=Xn(n,e);t?b(r):v(r)}return/* @__PURE__ */ (0,d.jsxs)(`div`,{dir:u,className:`univer-flex univer-min-w-0 univer-flex-col univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium`,children:l.t(`sheets-pivot-ui.dateFilter.title`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-1`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-xs`,children:l.t(`sheets-pivot-ui.dateFilter.rule`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-full`,value:String(p),options:Zn.map(e=>({value:String(e.value),label:l.t(e.label)})),onChange:e=>m(Number(e))})]}),(A?[!1,!0]:[!1]).map(e=>{let i=e?k:O,a=i===void 0?void 0:Jn(i,t),o=(e?g:void 0)??h??r,c=o!==void 0&&!Jn(o,t),u=i===void 0?null:(0,n.excelSerialToDateTimeParts)(i,{dateSystem:t}),f=u?new Date(2e3,0,1,u.hours,u.minutes,u.seconds,Math.floor(u.fractionalSecond*1e3)):void 0,p=`sheets-pivot-ui.dateFilter.date`;return A&&(p=e?`sheets-pivot-ui.dateFilter.to`:`sheets-pivot-ui.dateFilter.from`),/* @__PURE__ */ (0,d.jsxs)(`div`,{role:`group`,"aria-label":l.t(p),className:`univer-flex univer-min-w-0 univer-flex-col univer-gap-1`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-xs`,children:l.t(p)}),c?/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-1`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-xs univer-text-gray-500`,children:l.t(`sheets-pivot-ui.dateFilter.serial`)}),/* @__PURE__ */ (0,d.jsx)(s.Input,{value:String((e?y:_)??``),onChange:t=>F(t,e)})]}):/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-wrap univer-items-center univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.DatePicker,{value:a,onValueChange:t=>I(t,e)}),!x&&i!==void 0&&/* @__PURE__ */ (0,d.jsx)(s.TimeInput,{value:f,onValueChange:t=>L(t,e)})]})]},String(e))}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:x,onChange:e=>S(e===!0),children:l.t(`sheets-pivot-ui.dateFilter.wholeDay`)}),T&&/* @__PURE__ */ (0,d.jsx)(`div`,{role:`alert`,className:`univer-text-xs univer-text-red-500`,children:l.t(`sheets-pivot-ui.dateFilter.failed`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-wrap univer-items-center univer-justify-between univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`link`,disabled:C||!a,onClick:()=>N(void 0),children:l.t(`sheets-pivot-ui.dialog.filter.clearFilter`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{disabled:C,onClick:c,children:l.t(`sheets-pivot-ui.dialog.filter.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:C||!j||M,onClick:()=>N(j),children:l.t(`sheets-pivot-ui.dialog.filter.confirm`)})]})]})]})}function $n(r){let{dialogId:a=b,filterItemInfo:o,onClose:c,onSetFilter:l,onSetSort:p,onSetDateFilter:m,unitId:h,subUnitId:g}=r,{format:_,filterInfo:v,sortInfo:y}=o,x=o.conditionFilter??v,S=(0,i.useDependency)(n.ICommandService),C=(0,i.useDependency)(n.LocaleService);(0,i.useObservable)(C.currentLocale$);let w=(0,i.useDependency)(i.IDialogService),T=(0,i.useDependency)(n.IUniverInstanceService),E=(0,i.useObservable)(C.direction$,C.getDirection()),D=(0,f.useRef)(!0),[O,k]=(0,f.useState)(!1);(0,f.useEffect)(()=>(D.current=!0,()=>{D.current=!1}),[]);let[A,j]=(0,f.useState)(()=>o?o.checkedItems??(v==null?void 0:v.list)??o.items:[]),[M,N]=(0,f.useState)(()=>o.partialItems??[]),P=M.length===0&&A.length===o.items.length,F=A.length===0&&M.length===0,[I,L]=(0,f.useState)(()=>{if(o)return y==null?void 0:y.type}),R=()=>{D.current&&(D.current=!1,c?c():w.close(a))};if((0,f.useEffect)(()=>{let e=T.getCurrentUnitOfType(n.UniverInstanceType.UNIVER_SHEET);if(!e)return;let t=e.activeSheet$.subscribe(e=>{e&&e.getSheetId()!==g&&D.current&&(D.current=!1,c?c():w.close(a))});return()=>t==null?void 0:t.unsubscribe()},[a,w,c,g,T]),!o)return;let z=()=>{N([]),j(P?[]:o.items)},B=e=>{M.includes(e)?(N(M.filter(t=>t!==e)),j([...A,e])):A.includes(e)?j(A.filter(t=>t!==e)):j([...A,e])},V=async()=>{let{tableFieldId:e,pivotTableId:n}=o;if(e&&n)try{(l?await l(A,P,M):await S.executeCommand(t.SetPivotFilterCommand.id,{unitId:h,subUnitId:g,pivotTableId:n,tableFieldId:e,items:A,partialItems:M,isAll:P}))&&D.current&&R()}catch{}},ee=()=>{R()},te=async()=>{let{tableFieldId:e,pivotTableId:n}=o;if(e&&n){j(o.items),N([]);try{(l?await l(o.items,!0,[],!0):await S.executeCommand(t.SetPivotFilterCommand.id,{unitId:h,subUnitId:g,pivotTableId:n,tableFieldId:e,items:o.items,partialItems:[],isAll:!0,clearAllFilters:!0}))&&D.current&&R()}catch{}}},H=async e=>{let{tableFieldId:n,pivotTableId:r}=o;if(!n||!r)return;let i={type:e};try{(p?await p(i):await S.executeCommand(t.SetPivotSortCommand.id,{unitId:h,subUnitId:g,pivotTableId:r,tableFieldId:n,info:i}))&&D.current&&(L(e),R())}catch{}},ne=Ut(v,{items:o.items,conditionFilter:o.conditionFilter});async function re(e){if(!D.current)return!1;let n=m?await m(e):await S.executeCommand(t.SetPivotDateFilterCommand.id,{unitId:h,subUnitId:g,pivotTableId:o.pivotTableId,tableFieldId:o.tableFieldId,filterInfo:e});return n&&D.current&&R(),n}if(O){let t=T.getUnit(h),r=o.itemTypes.findIndex(t=>t===e.PivotDataFieldDataTypeEnum.date),i=Number(o.items[r]);return/* @__PURE__ */ (0,d.jsx)(Qn,{dateSystem:(t==null?void 0:t.getDateSystem())??n.DateSystem.Date1900,defaultDate:Number.isFinite(i)?i:void 0,currentFilter:(0,e.isPivotDateFilter)(x)?x:void 0,onConfirm:re,onCancel:()=>k(!1)})}return/* @__PURE__ */ (0,d.jsxs)(`div`,{dir:E,children:[o.supportsDateFilter&&/* @__PURE__ */ (0,d.jsx)(s.Button,{className:`univer-mb-3 univer-w-full`,onClick:()=>k(!0),children:C.t(`sheets-pivot-ui.dateFilter.title`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:(0,s.clsx)(`univer-flex univer-h-8 univer-w-[140px] univer-flex-1 univer-cursor-pointer univer-items-center univer-justify-center univer-rounded-l-md univer-text-center univer-text-sm hover:univer-bg-gray-100 rtl:univer-rounded-l-none rtl:univer-rounded-r-md dark:hover:!univer-bg-gray-700`,s.borderClassName,{"univer-bg-gray-100 dark:!univer-bg-gray-700":I===e.PivotDataFieldSortOperatorEnum.ascending}),onClick:()=>H(e.PivotDataFieldSortOperatorEnum.ascending),children:[/* @__PURE__ */ (0,d.jsx)(u.AscendingIcon,{}),C.t(`sheets-pivot-ui.dialog.filter.ascending`)]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:(0,s.clsx)(`univer-flex univer-h-8 univer-w-[140px] univer-flex-1 univer-cursor-pointer univer-items-center univer-justify-center univer-rounded-r-md univer-text-center univer-text-sm hover:univer-bg-gray-100 rtl:univer-rounded-l-md rtl:univer-rounded-r-none dark:hover:!univer-bg-gray-700`,s.borderClassName,{"univer-bg-gray-100 dark:!univer-bg-gray-700":I===e.PivotDataFieldSortOperatorEnum.descending}),onClick:()=>H(e.PivotDataFieldSortOperatorEnum.descending),children:[/* @__PURE__ */ (0,d.jsx)(u.DescendingIcon,{}),C.t(`sheets-pivot-ui.dialog.filter.descending`)]})]}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-mx-0 univer-mb-4 univer-mt-3 univer-h-[300px] univer-rounded-lg univer-p-2`,s.borderClassName),children:/* @__PURE__ */ (0,d.jsxs)(`div`,{className:(0,s.clsx)(`univer-h-full univer-overflow-y-auto`,s.scrollbarClassName),children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-1 univer-box-border univer-h-7 univer-py-[5px] univer-pl-[22px] univer-pr-1 univer-text-sm rtl:univer-pl-1 rtl:univer-pr-[22px]`,children:/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:P,onChange:z,indeterminate:!P&&(A.length>0||M.length>0),children:C.t(`sheets-pivot-ui.dialog.filter.selectAll`)})}),o.items.map((t,r)=>{var i,a;let c=t,l=(i=o.itemLabels)==null?void 0:i[t],u=(a=o.itemTypes)==null?void 0:a[r],f=ht(t,u);return f?c=C.t(f):l===void 0?u===e.PivotDataFieldDataTypeEnum.date&&_&&(c=n.numfmt.format(_,Number(t))):c=l,/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-1 univer-box-border univer-h-7 univer-py-1 univer-pl-[22px] univer-pr-1 univer-text-sm rtl:univer-pl-1 rtl:univer-pr-[22px]`,children:/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:A.includes(t),indeterminate:M.includes(t),onChange:()=>B(t),contentClassName:`univer-max-w-[200px] univer-truncate`,children:c})},t)})]})}),/* @__PURE__ */ (0,d.jsxs)(s.ActionRow,{className:`univer-flex univer-items-center univer-justify-between`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-cursor-pointer univer-text-xs univer-text-primary-600`,children:/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`link`,disabled:ne===`NotFilter`,onClick:te,children:C.t(`sheets-pivot-ui.dialog.filter.clearFilter`)})}),/* @__PURE__ */ (0,d.jsxs)(s.ActionRow,{className:`univer-flex univer-flex-1 univer-items-center univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:ee,children:C.t(`sheets-pivot-ui.dialog.filter.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:F,onClick:V,children:C.t(`sheets-pivot-ui.dialog.filter.confirm`)})]})]})]})}function er({name:e,onConfirm:t,onCancel:r}){let a=(0,i.useDependency)(n.LocaleService),o=(0,i.useObservable)(a.direction$,a.getDirection()),[c,l]=(0,f.useState)(e),[u,p]=(0,f.useState)(!1),m=(0,f.useRef)(!1);return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,dir:o,children:[/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-flex-col univer-gap-2 univer-text-sm`,children:[a.t(`sheets-pivot-ui.manualGroup.name`),/* @__PURE__ */ (0,d.jsx)(s.Input,{value:c,onChange:l})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-justify-end univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:r,children:a.t(`sheets-pivot-ui.dialog.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:!c.trim()||u,onClick:async()=>{if(c.trim()&&!m.current){m.current=!0,p(!0);try{await t(c)}catch{}finally{m.current=!1,p(!1)}}},children:a.t(`sheets-pivot-ui.dialog.ok`)})]})]})}const tr=`univer-flex univer-items-center univer-justify-between univer-gap-3 univer-text-sm`;function nr({targetFieldName:e,grouping:t,defaults$:r,onConfirm:a,onCancel:o}){let c=(0,i.useDependency)(n.LocaleService),l=(0,i.useObservable)(c.direction$,c.getDirection()),u=(0,i.useObservable)(r),[p,m]=(0,f.useState)((t==null?void 0:t.autoStart)??!0),[h,g]=(0,f.useState)((t==null?void 0:t.autoEnd)??!0),[_,v]=(0,f.useState)((t==null?void 0:t.autoStart)===!1?String(t.start):void 0),[y,b]=(0,f.useState)((t==null?void 0:t.autoEnd)===!1?String(t.end):void 0),[x,S]=(0,f.useState)(t?String(t.interval):void 0),[C,w]=(0,f.useState)(!1),T=(0,f.useRef)(!1),E=e=>e===void 0||!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(e.trim())?NaN:Number(e),D={autoStart:p,autoEnd:h,start:p?u==null?void 0:u.start:E(_),end:h?u==null?void 0:u.end:E(y),interval:x===void 0||x.trim()===``?(u==null?void 0:u.interval)??0:E(x)},O=!!u&&se(D),k=e=>{!e&&_===void 0&&(u==null?void 0:u.start)!==void 0&&v(String(u.start)),m(e)},A=e=>{!e&&y===void 0&&(u==null?void 0:u.end)!==void 0&&b(String(u.end)),g(e)};return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,dir:l,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium`,children:e}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:p,onChange:e=>k(!!e),children:c.t(`sheets-pivot-ui.numberGroup.autoStart`)}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:tr,children:[c.t(`sheets-pivot-ui.numberGroup.start`),/* @__PURE__ */ (0,d.jsx)(s.Input,{className:`univer-w-32`,value:p?String((u==null?void 0:u.start)??``):_??``,disabled:p,onChange:v})]}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:h,onChange:e=>A(!!e),children:c.t(`sheets-pivot-ui.numberGroup.autoEnd`)}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:tr,children:[c.t(`sheets-pivot-ui.numberGroup.end`),/* @__PURE__ */ (0,d.jsx)(s.Input,{className:`univer-w-32`,value:h?String((u==null?void 0:u.end)??``):y??``,disabled:h,onChange:b})]}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:tr,children:[c.t(`sheets-pivot-ui.numberGroup.interval`),/* @__PURE__ */ (0,d.jsx)(s.Input,{className:`univer-w-32`,value:x??String((u==null?void 0:u.interval)??``),placeholder:String((u==null?void 0:u.interval)??``),onChange:S})]})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-justify-end univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:o,children:c.t(`sheets-pivot-ui.dialog.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:!O||C,onClick:async()=>{if(O&&!T.current){T.current=!0,w(!0);try{await a(D)}catch{}finally{T.current=!1,w(!1)}}},children:c.t(`sheets-pivot-ui.dialog.ok`)})]})]})}const rr=({handlePatternChange:e})=>{let t=(0,i.useDependency)(n.LocaleService);return(0,f.useEffect)(()=>{e(``)},[]),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-1.5 univer-text-sm univer-leading-5 univer-text-gray-600 dark:!univer-text-gray-200`,children:t.t(`sheets-pivot-ui.dialog.numfmt.generalDesc`)})},ir=({originPattern:e,handlePatternChange:t})=>{let r=(0,i.useDependency)(n.LocaleService),a=n.currencySymbols.map(e=>({label:e,value:e})),[o,c]=(0,f.useState)(()=>At(e||``,2)),[l,u]=(0,f.useState)(()=>Mt(e??``)||a[0].value);return(0,f.useEffect)(()=>{t(J(`_("${l}"* #,##0${o>0?`.0`:``}_)`,o))},[]),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mt-5`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-w-[136px]`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.decimalPlaces`)}),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-box-border univer-h-8`,value:o,max:20,min:0,onChange:e=>{let n=e??0;c(n),t(J(`_("${l}"* #,##0${n>0?`.0`:``}_)`,n))}})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-w-[136px]`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.currencyTypes`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{options:a,value:l,onChange:e=>{u(e),t(J(`_("${e}"* #,##0${o>0?`.0`:``}_)`,o))}})]})]}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-1.5 univer-text-sm univer-leading-5 univer-text-gray-600 dark:!univer-text-gray-200`,children:r.t(`sheets-pivot-ui.dialog.numfmt.accountingDesc`)})]})},ar=({originPattern:e,handlePatternChange:t})=>{let r=(0,i.useDependency)(n.LocaleService),a=n.currencySymbols.map(e=>({label:e,value:e})),[o,c]=(0,f.useState)(()=>At(e||``,2)),[l,u]=(0,f.useState)(()=>Mt(e??``)||a[0].value),p=pt.map(e=>({label:e.label(l),value:e.suffix(l),color:e.color})),[m,h]=(0,f.useState)(()=>{var t;return((t=p.find(t=>(0,n.isPatternEqualWithoutDecimal)(t.value,e??``)))==null?void 0:t.value)||p[0].value});return(0,f.useEffect)(()=>{t(J(m,o))},[]),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mt-5`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-w-[136px]`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.decimalPlaces`)}),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-box-border univer-h-8`,value:o,max:20,min:0,onChange:e=>{e&&(c(e),t(J(m,e)))}})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-w-[136px]`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.currencyTypes`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{options:a,value:l,onChange:e=>{u(e);let n=pt.map(t=>({label:t.label(e),value:t.suffix(e),color:t.color}));h(n[0].value),t(J(n[0].value,o))}})]})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mt-4`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.negativeNumberTypes`)}),/* @__PURE__ */ (0,d.jsx)(s.SelectList,{className:`univer-rounded-lg univer-p-2`,options:p,value:m,onChange:e=>{h(e),t(J(e,o))}}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-1.5 univer-text-sm univer-leading-5 univer-text-gray-600 dark:!univer-text-gray-200`,children:r.t(`sheets-pivot-ui.dialog.numfmt.currencyDesc`)})]})]})},or=({originPattern:e,handlePatternChange:t})=>{let r=(0,i.useDependency)(n.LocaleService),a=dt.map(e=>({label:e.label,value:e.suffix})),[o,c]=(0,f.useState)(()=>{if(e){let t=a.find(t=>t.value===e);if(t)return t.value}return a[0].value});return(0,f.useEffect)(()=>{t(o)},[]),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-5`,children:/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mt-5`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.dateTypes`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{children:/* @__PURE__ */ (0,d.jsx)(s.SelectList,{className:`univer-rounded-lg univer-p-2`,options:a,value:o,onChange:e=>{c(e),t(e)}})}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-1.5 univer-text-sm univer-leading-5 univer-text-gray-600 dark:!univer-text-gray-200`,children:r.t(`sheets-pivot-ui.dialog.numfmt.dateDesc`)})]})})},sr=({originPattern:e,handlePatternChange:t})=>{let r=(0,i.useDependency)(n.LocaleService),[a,o]=(0,f.useState)(()=>At(e||``,0)),c=ft.map(e=>({label:e.label,value:e.suffix,color:e.color})),[l,u]=(0,f.useState)(()=>{let t=c.find(t=>(0,n.isPatternEqualWithoutDecimal)(t.value,e||``));return(t==null?void 0:t.value)||c[0].value});return(0,f.useEffect)(()=>{t(J(l,Number(a||0)))},[]),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mt-5`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-items-center univer-justify-between`,children:/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-w-[136px]`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.decimalPlaces`)}),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-box-border univer-h-8`,value:a,max:20,min:0,onChange:e=>{e&&(o(e),t(J(l,Number(e))))}})]})}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mt-4`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.negativeNumberTypes`)}),/* @__PURE__ */ (0,d.jsx)(s.SelectList,{className:`univer-rounded-lg univer-p-2`,options:c,value:l,onChange:e=>{o(At(e,0)),u(e),t(e)}}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-1.5 univer-text-sm univer-leading-5 univer-text-gray-600 dark:!univer-text-gray-200`,children:r.t(`sheets-pivot-ui.dialog.numfmt.thousandthPercentileDesc`)})]})]})},cr=({unitId:e,subUnitId:t,pivotTableId:r,tableFieldId:a,originPattern:o,tableFieldInfo:c,dataFieldInfo:l})=>{let p=(0,i.useDependency)(n.LocaleService),m=(0,i.useDependency)(i.IDialogService),h=p.getDirection(),g=(0,f.useMemo)(()=>[{label:`sheets-pivot-ui.dialog.numfmt.general`,component:rr,key:`sheet.numfmt.general`},{label:`sheets-pivot-ui.dialog.numfmt.accounting`,component:ir,key:`sheet.numfmt.accounting`},{label:`sheets-pivot-ui.dialog.numfmt.currency`,component:ar,key:`sheet.numfmt.currency`},{label:`sheets-pivot-ui.dialog.numfmt.date`,component:or,key:`sheet.numfmt.date`},{label:`sheets-pivot-ui.dialog.numfmt.thousandthPercentile`,component:sr,key:`sheet.numfmt.thousandthPercentile`}].map(e=>({...e,label:p.t(e.label)})),[p]),_=g.map(e=>({label:e.label,value:e.key})),[v,y]=(0,f.useState)(()=>[Nt,Pt,Ft,Lt,zt].reduce((e,t,n)=>e||(t(o??``)?g[n].key:``),``)||g[0].key),[b,C]=(0,f.useState)(o),T=e=>C(e),E=e=>{y(e)},D=n=>({id:S,title:{title:p.t(`sheets-pivot-ui.fieldSettings`)},draggable:!0,mask:!1,maskClosable:!1,width:400,children:{label:x,tableFieldId:a,pivotTableId:r,unitId:e,subUnitId:t,tableFieldInfo:c,dataFieldInfo:l,key:`field-setting-${a||``}`,closeFn:()=>{m.close(S)},format:n?b:``},onClose:()=>{m.close(S)}}),O=()=>{m.close(w);let e=D(!1);m.open(e)};return/* @__PURE__ */ (0,d.jsxs)(`div`,{dir:h,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex`,children:/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-5 univer-flex univer-h-6 univer-cursor-pointer univer-items-center univer-text-base univer-font-medium univer-leading-6`,onClick:O,children:[/* @__PURE__ */ (0,d.jsx)(u.ArrowLeftIcon,{}),p.t(`sheets-pivot-ui.dialog.numfmt.fieldSetting`)]})}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-h-5 univer-text-sm univer-font-medium univer-leading-5`,children:p.t(`sheets-pivot-ui.dialog.numfmt.formatTypes`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{children:/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-full`,value:v,options:_,onChange:e=>E(e)})}),/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[v===`sheet.numfmt.general`&&/* @__PURE__ */ (0,d.jsx)(rr,{originPattern:o,handlePatternChange:T}),v===`sheet.numfmt.accounting`&&/* @__PURE__ */ (0,d.jsx)(ir,{originPattern:o,handlePatternChange:T}),v===`sheet.numfmt.currency`&&/* @__PURE__ */ (0,d.jsx)(ar,{originPattern:o,handlePatternChange:T}),v===`sheet.numfmt.date`&&/* @__PURE__ */ (0,d.jsx)(or,{originPattern:o,handlePatternChange:T}),v===`sheet.numfmt.thousandthPercentile`&&/* @__PURE__ */ (0,d.jsx)(sr,{originPattern:o,handlePatternChange:T})]}),/* @__PURE__ */ (0,d.jsxs)(s.ActionRow,{className:`univer-mt-2 univer-flex univer-justify-end`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:O,children:p.t(`sheets-pivot-ui.dialog.numfmt.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{className:`univer-ml-3 rtl:univer-ml-0 rtl:univer-mr-3`,variant:`primary`,onClick:()=>{m.close(w);let e=D(!0);m.open(e)},children:p.t(`sheets-pivot-ui.dialog.numfmt.confirm`)})]})]})};function lr(a){var o,c;let{scope:l,report:u,getCurrentReport:p,onChange:m}=a,h=(0,i.useDependency)(n.LocaleService),g=(0,i.useDependency)(n.IConfirmService),_=(0,i.useDependency)(t.SheetsPivotTableService),v=(0,i.useDependency)(n.IPermissionService),y=(0,i.useDependency)(n.ILogService);(0,i.useObservable)(v.permissionPointUpdate$);let[b,x]=(0,f.useState)(!1),S=(0,f.useRef)(!1),C=(0,f.useRef)(!0),w=JSON.stringify(l),T=(0,f.useRef)(w);T.current=w,(0,f.useEffect)(()=>(C.current=!0,()=>{C.current=!1}),[]);let E=b||!u||((o=v.getPermissionPoint(new r.WorkbookEditablePermission(l.unitId).id))==null?void 0:o.value)===!1||((c=v.getPermissionPoint(new r.WorksheetFilterPermission(l.unitId,l.subUnitId).id))==null?void 0:c.value)===!1,D=async r=>{if(S.current||E||!u||u.options.allowMultipleFilters===r)return;S.current=!0,x(!0);let i=n.Tools.deepClone(u);try{if(!r&&(0,e.hasPivotMultipleFilters)(u)){let e=await _.asyncInterceptor.fetchThroughAsyncInterceptors(t.PIVOT_OPTION_FILTER_COORDINATION)({redos:[],undos:[],groups:[],validate:()=>void 0},l);if(!C.current||T.current!==w||!await g.confirm({id:`sheet-pivot.confirm.disable-multiple-filters`,title:{title:h.t(`sheets-pivot-ui.config.multipleFiltersTitle`)},children:{title:h.t(`sheets-pivot-ui.config.multipleFiltersClearWarning`)},cancelText:h.t(`sheets-pivot-ui.dialog.cancel`),confirmText:h.t(`sheets-pivot-ui.dialog.ok`)}))return;e==null||e.validate()}if(!C.current||T.current!==w||!n.Tools.diffValue(i,p()))return;await m(r)}catch(e){y.error(`[PivotFilterOptions]`,e)}finally{S.current=!1,C.current&&x(!1)}};return/* @__PURE__ */ (0,d.jsxs)(`section`,{className:`univer-flex univer-flex-col univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.filteringSection`)}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-items-start univer-gap-2 univer-text-sm`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:(u==null?void 0:u.options.allowMultipleFilters)===!0,disabled:E,onChange:e=>D(!!e)}),/* @__PURE__ */ (0,d.jsx)(`span`,{children:h.t(`sheets-pivot-ui.config.allowMultipleFilters`)})]})]})}const ur=[{label:`sheets-pivot-ui.config.pageDownThenOver`,value:`false`},{label:`sheets-pivot-ui.config.pageOverThenDown`,value:`true`}],dr=[{label:`sheets-pivot-ui.config.layoutTabular`,value:String(e.PivotLayoutTypeEnum.tabular)},{label:`sheets-pivot-ui.config.layoutCompact`,value:String(e.PivotLayoutTypeEnum.compact)},{label:`sheets-pivot-ui.config.layoutOutline`,value:String(e.PivotLayoutTypeEnum.outline)}],fr=[{label:`sheets-pivot-ui.config.rowSubtotalTop`,value:String(e.PivotSubtotalPositionEnum.top)},{label:`sheets-pivot-ui.config.rowSubtotalBottom`,value:String(e.PivotSubtotalPositionEnum.bottom)}],pr=[{label:`sheets-pivot-ui.config.dateGroupingLegacy`,value:e.PivotDateGroupingMode.Legacy},{label:`sheets-pivot-ui.config.dateGroupingExcelLike`,value:e.PivotDateGroupingMode.ExcelLike}];function mr(r){let{unitId:a,subUnitId:c,pivotTableId:l}=r,u=(0,i.useDependency)(n.ICommandService),p=(0,i.useDependency)(n.IConfirmService),m=(0,i.useDependency)(n.ILogService),h=(0,i.useDependency)(n.LocaleService),g=(0,i.useDependency)(t.SheetsPivotTableConfigModel),_=(0,f.useMemo)(()=>g.watchPivotTableChanges(a,c,l).pipe((0,o.debounceTime)(0)),[g,a,c,l]);(0,i.useObservable)(_);let[v,y]=(0,f.useState)(!1),b=(0,f.useRef)(!1),x=g.getPivotTableConfig(a,c,l),S=(0,e.getPivotDateGroupingMode)(x==null?void 0:x.fieldsConfig.options),[C,w]=(0,f.useState)(()=>{var t,n;let r=g.getPivotTableConfig(a,c,l),i=(r==null||(t=r.fieldsConfig)==null?void 0:t.options)??{};return{showRowSubtotal:i.showRowSubtotal??!0,showRowGrandTotal:i.showRowGrandTotal??!0,showColSubtotal:i.showColSubtotal??!0,showColGrandTotal:i.showColGrandTotal??!0,pageWrap:i.pageWrap??1,pageOverThenDown:i.pageOverThenDown??!1,repeatRowLabels:i.repeatRowLabels??!1,repeatColLabels:i.repeatColLabels??!1,layout:(r==null||(n=r.fieldsConfig)==null?void 0:n.layout)??e.PivotLayoutTypeEnum.tabular,rowSubtotalPosition:i.rowSubtotalPosition??e.PivotSubtotalPositionEnum.top}}),T=e=>{let n={unitId:a,subUnitId:c,pivotTableId:l,options:e};u.executeCommand(t.SetPivotOptionCommand.id,n).catch(e=>m.error(`[PivotConfigPanel]`,e)),w(t=>({...t,...e}))},E=e=>{let n={unitId:a,subUnitId:c,pivotTableId:l,layout:e};u.executeCommand(t.SetPivotLayoutCommand.id,n).catch(e=>m.error(`[PivotConfigPanel]`,e)),w(t=>({...t,layout:e}))},D=async n=>{if(!(b.current||n===S)){b.current=!0,y(!0);try{let r=g.getPivotTableConfig(a,c,l),i=g.getCollection(a,l);if(!r||!i||(Object.values(i.fields).some(e.isDateGroupFieldJSON)||i.customFields.some(e.isDateGroupFieldJSON))&&!await p.confirm({id:`sheet-pivot.confirm.change-date-grouping-mode`,title:{title:h.t(`sheets-pivot-ui.config.dateGroupingSwitchTitle`)},children:{title:h.t(`sheets-pivot-ui.config.dateGroupingSwitchMessage`)},cancelText:h.t(`sheets-pivot-ui.dialog.cancel`),confirmText:h.t(`sheets-pivot-ui.dialog.ok`)})||g.getPivotTableConfig(a,c,l)!==r||g.getCollection(a,l)!==i||(0,e.getPivotDateGroupingMode)(r.fieldsConfig.options)===n)return;await u.executeCommand(t.SetPivotOptionCommand.id,{unitId:a,subUnitId:c,pivotTableId:l,options:{dateGroupingMode:n}})}catch(e){m.error(`[PivotConfigPanel]`,e)}finally{b.current=!1,y(!1)}}};return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4 univer-py-4`,children:[/* @__PURE__ */ (0,d.jsx)(lr,{scope:{unitId:a,subUnitId:c,pivotTableId:l},report:x==null?void 0:x.fieldsConfig,getCurrentReport:()=>{var e;return(e=g.getPivotTableConfig(a,c,l))==null?void 0:e.fieldsConfig},onChange:e=>u.executeCommand(t.SetPivotOptionCommand.id,{unitId:a,subUnitId:c,pivotTableId:l,options:{allowMultipleFilters:e}})}),/* @__PURE__ */ (0,d.jsxs)(`section`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.displaySection`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.dateGroupingMode`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-36`,value:S,options:pr.map(e=>({...e,label:h.t(e.label)})),disabled:v,onChange:e=>D(e)})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.layout`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-36`,value:String(C.layout),options:dr.map(e=>({...e,label:h.t(e.label)})),onChange:e=>E(Number(e))})]}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-gap-2 univer-text-sm univer-text-gray-600`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.repeatRowLabels,disabled:C.layout===e.PivotLayoutTypeEnum.compact,onChange:e=>T({repeatRowLabels:!!e})}),h.t(`sheets-pivot-ui.config.repeatRowLabels`)]}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-gap-2 univer-text-sm univer-text-gray-600`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.repeatColLabels,onChange:e=>T({repeatColLabels:!!e})}),h.t(`sheets-pivot-ui.config.repeatColLabels`)]})]})]}),/* @__PURE__ */ (0,d.jsxs)(`section`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-mb-2 univer-text-sm univer-font-medium univer-text-gray-600`),children:h.t(`sheets-pivot-ui.config.totalsSection`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-gap-2 univer-text-sm univer-text-gray-600`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showRowSubtotal,onChange:e=>T({showRowSubtotal:!!e})}),h.t(`sheets-pivot-ui.config.showRowSubtotal`)]}),C.layout===e.PivotLayoutTypeEnum.outline&&/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.rowSubtotalPosition`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-36`,value:String(C.rowSubtotalPosition),options:fr.map(e=>({...e,label:h.t(e.label)})),onChange:e=>T({rowSubtotalPosition:Number(e)})})]}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-gap-2 univer-text-sm univer-text-gray-600`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showRowGrandTotal,onChange:e=>T({showRowGrandTotal:!!e})}),h.t(`sheets-pivot-ui.config.showRowGrandTotal`)]}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-gap-2 univer-text-sm univer-text-gray-600`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showColSubtotal,onChange:e=>T({showColSubtotal:!!e})}),h.t(`sheets-pivot-ui.config.showColSubtotal`)]}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-gap-2 univer-text-sm univer-text-gray-600`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showColGrandTotal,onChange:e=>T({showColGrandTotal:!!e})}),h.t(`sheets-pivot-ui.config.showColGrandTotal`)]})]})]}),/* @__PURE__ */ (0,d.jsxs)(`section`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.pageSection`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.pageWrap`)}),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-w-24`,value:C.pageWrap,min:1,max:20,onChange:e=>{let t=Number(e);Number.isFinite(t)&&t>=1&&T({pageWrap:t})}})]}),/* @__PURE__ */ (0,d.jsx)(`p`,{className:`univer-text-xs univer-text-gray-600`,children:h.t(C.pageOverThenDown?`sheets-pivot-ui.config.pageWrapOverThenDownHelp`:`sheets-pivot-ui.config.pageWrapDownThenOverHelp`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.pageDirection`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-36`,value:String(C.pageOverThenDown),options:ur.map(e=>({...e,label:h.t(e.label)})),onChange:e=>T({pageOverThenDown:e===`true`})})]})]})]})]})}let Q=class extends n.Disposable{constructor(e,t,n,r,i,a,s,c){super(),this._sheetsSelectionsService=e,this._univerInstanceService=t,this._sheetsPivotTableAdaptorModel=n,this._commandService=r,this._sidebarService=i,this._dialogService=a,this._injector=s,this._permissionService=c,W(this,`_pivotTableId`,void 0),W(this,`_pivotTablePanelVisible$`,new o.Subject),W(this,`pivotTablePanelVisible$`,this._pivotTablePanelVisible$.asObservable()),W(this,`_isRangeSelectorFocus`,!1),W(this,`_isRangeSelector2Focus`,!1),this._initPanelOpen(),this._initSheetChange()}setIsRangeSelectorFocus(e){this._isRangeSelectorFocus=e}setIsRangeSelector2Focus(e){this._isRangeSelector2Focus=e}_setPivotTableId(e){this._pivotTableId=e}_closePivotPanel(){this._isRangeSelectorFocus||this._isRangeSelector2Focus||(this._setPivotTableId(void 0),this._sidebarService.close(v))}_initPanelOpen(){let e=this._univerInstanceService.getCurrentUnitOfType(n.UniverInstanceType.UNIVER_SHEET);if(e){let i=e.activeSheet$;this.disposeWithMe((0,o.merge)(i,this._sheetsSelectionsService.selectionMoveEnd$).subscribe(()=>{let e=this._sheetsSelectionsService.getCurrentSelections();if(!e)return;let i=e[e.length-1];if(!i)return;let a=i.primary;if(!a)return;let o=this._univerInstanceService.getCurrentUnitOfType(n.UniverInstanceType.UNIVER_SHEET),s=o==null?void 0:o.getActiveSheet();if(!o||!s)return;let c=o.getUnitId(),l=s.getSheetId(),u=this._sheetsPivotTableAdaptorModel.getPivotTableIdByCell(c,l,a.startRow,a.startColumn);if(u){var d,f;let e=(d=this._sheetsPivotTableAdaptorModel.getPivotItemRenderInfoCache(c,l,u))==null?void 0:d.rangesInfo,n=(0,t.unionPivotViewRange)(e),i=(0,r.checkRangesEditablePermission)(this._injector,c,l,n),a=(f=this._permissionService.getPermissionPoint(new r.WorksheetPivotTablePermission(c,l).id))==null?void 0:f.value;if(!i||!a)return;this._setPivotTableId(u),this._pivotTablePanelVisible$.next(!0),this._commandService.executeCommand(it.id,{pivotTableId:u})}else this._closePivotPanel()})),this.disposeWithMe(this._commandService.onCommandExecuted(e=>{e.id===t.RemovePivotTableMutation.id&&e.params.pivotTableId===this._pivotTableId&&this._sidebarService.close(v)}))}}_initSheetChange(){let e=this._univerInstanceService.getCurrentUnitOfType(n.UniverInstanceType.UNIVER_SHEET);e&&this.disposeWithMe(e.sheetDisposed$.subscribe(()=>{this._sidebarService.close()})),this.disposeWithMe(this._sidebarService.sidebarOptions$.subscribe(e=>{if(e.id===`UNIVER_SHEET_PIVOT_PANEL_ID`){let t=!!e.visible;this._pivotTablePanelVisible$.next(t),t||this._dialogService.close(S)}})),this.disposeWithMe(this._dialogService.getDialogs$().subscribe(e=>{let t=e.find(e=>e.id===A);t&&t.open===!1&&this._dialogService.close(A)}))}};Q=K([G(0,(0,n.Inject)(r.SheetsSelectionsService)),G(1,(0,n.Inject)(n.IUniverInstanceService)),G(2,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),G(3,(0,n.Inject)(n.ICommandService)),G(4,(0,n.Inject)(i.ISidebarService)),G(5,(0,n.Inject)(i.IDialogService)),G(6,(0,n.Inject)(n.Injector)),G(7,(0,n.Inject)(n.IPermissionService))],Q);const hr=(0,f.createContext)(null),gr=(0,f.createContext)(null);function _r(e,t){return e===t?!0:!e||!t?!1:e.droppableId===t.droppableId&&e.index===t.index}function vr(e){if(!(e instanceof Element))return null;let t=e.closest(`[data-univer-dnd-droppable-id]`);return t instanceof HTMLElement?t:null}function yr(e,t,n,r){let i=Array.from(e.querySelectorAll(`[data-univer-dnd-item="true"]`)).filter(e=>{let t=e.dataset.univerDndId;return t?t!==n.draggableId||n.source.droppableId!==r:!1});if(!i.length)return 0;for(let e=0;e<i.length-1;e++){let n=i[e].getBoundingClientRect();if(t<n.top+n.height/2)return e}let a=i.length-1,o=i[a].getBoundingClientRect();return t<o.top+o.height*.35?a:i.length}function br(){let e=(0,f.useContext)(hr);if(!e)throw Error(`Draggable/Droppable must be used inside DragDropContext`);return e}function xr(e){let{children:t,onDragEnd:n,onDragStart:r,onDragUpdate:i}=e,[a,o]=(0,f.useState)(null),s=(0,f.useRef)(null),c=(0,f.useCallback)((e,t)=>{let n={draggableId:e,source:t,destination:null};s.current=n,o(n),r==null||r({draggableId:e,source:t}),i==null||i({draggableId:e,source:t,destination:null})},[r,i]),l=(0,f.useCallback)(e=>{let t=s.current;if(!t||_r(t.destination,e))return;let n={...t,destination:e};s.current=n,o(n),i==null||i({draggableId:n.draggableId,source:n.source,destination:n.destination})},[i]),u=(0,f.useCallback)(async(e,t)=>{let r=s.current;r&&(s.current=null,o(null),await(n==null?void 0:n({draggableId:r.draggableId,source:r.source,destination:t===void 0?r.destination:t,reason:e})))},[n]);(0,f.useEffect)(()=>{if(!a)return;let e=e=>{let t=vr(e.target);t&&t.dataset.univerDndDropDisabled!==`true`||l(null)};return window.addEventListener(`dragover`,e,!0),()=>{window.removeEventListener(`dragover`,e,!0)}},[a,l]);let p=(0,f.useMemo)(()=>({activeDrag:a,startDrag:c,updateDestination:l,endDrag:u}),[a,c,l,u]);return/* @__PURE__ */ (0,d.jsx)(hr.Provider,{value:p,children:t})}function Sr(e){var t;let{children:n,droppableId:r,isDropDisabled:i=!1}=e,{activeDrag:a,updateDestination:o,endDrag:s}=br(),c=(0,f.useRef)(null),l={innerRef:(0,f.useCallback)(e=>{c.current=e},[]),droppableProps:{onDragOver:(0,f.useCallback)(e=>{if(!a||i||!c.current)return;e.preventDefault();let t=yr(c.current,e.clientY,a,r);o({droppableId:r,index:t})},[a,i,r,o]),onDrop:(0,f.useCallback)(e=>{var t;if(!a||i||!c.current)return;e.preventDefault();let n=(((t=a.destination)==null?void 0:t.droppableId)===r?a.destination:null)??{droppableId:r,index:yr(c.current,e.clientY,a,r)};return s(`DROP`,n)},[a,s,i,r]),"data-univer-dnd-droppable-id":r,"data-univer-dnd-drop-disabled":String(i)},placeholder:null},u={isDraggingOver:!!(a&&((t=a.destination)==null?void 0:t.droppableId)===r)};return/* @__PURE__ */ (0,d.jsx)(gr.Provider,{value:r,children:n(l,u)})}function Cr(e){var t;let{children:n,draggableId:r,index:i,isDragDisabled:a=!1}=e,{activeDrag:o,startDrag:s,updateDestination:c,endDrag:l}=br(),u=(0,f.useContext)(gr),d=(0,f.useRef)(null),p=(0,f.useRef)(null),m=(0,f.useRef)(!1),h=(0,f.useRef)(null),g=(0,f.useRef)(null),_=(0,f.useRef)(!1),v=(0,f.useRef)(null),y=(0,f.useRef)(null),b=(0,f.useRef)(null),x=(0,f.useCallback)(e=>{d.current=e},[]),S=(o==null?void 0:o.draggableId)===r,C=(0,f.useCallback)(()=>{g.current&&=(window.clearTimeout(g.current),null)},[]),w=(0,f.useCallback)(()=>{C(),b.current=null,h.current&&=(h.current(),null),v.current&&=(v.current.remove(),null),y.current=null,m.current=!1},[C]);(0,f.useEffect)(()=>w,[w]);let T=(0,f.useCallback)((e,t)=>{if(v.current)return;let n=d.current;if(!n)return;let r=n.getBoundingClientRect(),i=e>0?Math.max(0,e-r.left):12,a=t>0?Math.max(0,t-r.top):12,o=n.cloneNode(!0);if(!(o instanceof HTMLDivElement))return;let s=o;s.style.position=`fixed`,s.style.left=`0px`,s.style.top=`0px`,s.style.width=`${r.width}px`,s.style.height=`${r.height}px`,s.style.pointerEvents=`none`,s.style.opacity=`0.9`,s.style.zIndex=`2147483647`,s.style.boxSizing=`border-box`,s.style.transform=`translate(${Math.max(0,e-i)}px, ${Math.max(0,t-a)}px)`,document.body.appendChild(s),v.current=s,y.current={x:i,y:a}},[]),E=(0,f.useCallback)((e,t)=>{let n=v.current,r=y.current;n&&r&&(n.style.transform=`translate(${Math.max(0,e-r.x)}px, ${Math.max(0,t-r.y)}px)`)},[]),D=(0,f.useCallback)((e,t)=>{let n=vr(document.elementFromPoint(e,t));if(!n||n.dataset.univerDndDropDisabled===`true`)return null;let a=n.dataset.univerDndDroppableId;return a?{droppableId:a,index:yr(n,t,{draggableId:r,source:{droppableId:u??``,index:i},destination:null},a)}:null},[r,u,i]),O=(0,f.useCallback)((e,t,n,o)=>{if(m.current||_.current||a||!u)return;m.current=!0,C(),_.current=!1;try{var l;(l=d.current)==null||l.setPointerCapture(t)}catch{}T(n,o),s(r,{droppableId:u,index:i});let f=D(n,o);c(f);try{e.preventDefault()}catch{}},[C,r,u,i,a,T,D,s,c]),k=(0,f.useCallback)(e=>{if(a||!u||e.pointerType===`mouse`&&e.button!==0)return;_.current=!1;let t=e.pointerId;b.current={pointerId:t,pointerType:e.pointerType,startX:e.clientX,startY:e.clientY},C(),e.pointerType!==`mouse`&&(g.current=window.setTimeout(()=>{let n=b.current;n&&n.pointerId===t&&O(e.nativeEvent,n.pointerId,n.startX,n.startY)},150));let n=e=>{let t=b.current;if(!t||e.pointerId!==t.pointerId)return;if(!m.current){if(_.current){w();return}if(t.pointerType===`mouse`){let n=e.clientX-t.startX,r=e.clientY-t.startY;n*n+r*r>=16&&O(e,t.pointerId,e.clientX,e.clientY)}return}E(e.clientX,e.clientY);let n=D(e.clientX,e.clientY);c(n);try{e.preventDefault()}catch{}},r=e=>{let t=b.current;if(!t||e.pointerId!==t.pointerId)return;let n=m.current?D(e.clientX,e.clientY):null,r=m.current?l(n?`DROP`:`CANCEL`,n):Promise.resolve();return w(),r};window.addEventListener(`pointermove`,n,{capture:!0,passive:!1}),window.addEventListener(`pointerup`,r,{capture:!0}),window.addEventListener(`pointercancel`,r,{capture:!0}),h.current=()=>{window.removeEventListener(`pointermove`,n,!0),window.removeEventListener(`pointerup`,r,!0),window.removeEventListener(`pointercancel`,r,!0)}},[w,C,u,l,a,D,O,c,E]),A=(0,f.useCallback)(e=>{let t=e.dataTransfer;if(!t||typeof t.setDragImage!=`function`)return;let n=e.currentTarget,r=n.cloneNode(!0);if(!(r instanceof HTMLDivElement))return;let i=r;i.style.position=`fixed`,i.style.left=`-9999px`,i.style.top=`-9999px`,i.style.width=`${n.getBoundingClientRect().width}px`,i.style.pointerEvents=`none`,i.style.opacity=`0.95`,i.style.zIndex=`2147483647`,document.body.appendChild(i);let a=n.getBoundingClientRect(),o=e.clientX>0?Math.max(0,e.clientX-a.left):12,s=e.clientY>0?Math.max(0,e.clientY-a.top):12;t.setDragImage(i,o,s),p.current&&p.current!==i&&p.current.remove(),p.current=i},[]),j=(0,f.useCallback)(e=>{if(m.current||a||!u){e.preventDefault();return}_.current=!0,C(),b.current=null,h.current&&=(h.current(),null);let t=e.dataTransfer;if(t){try{t.effectAllowed=`move`}catch{}try{t.setData(`text/plain`,r)}catch{}}try{A(e)}catch{}s(r,{droppableId:u,index:i})},[A,C,r,u,i,a,s]),M=(0,f.useCallback)(()=>(p.current&&=(p.current.remove(),null),l(`CANCEL`,null)),[l]);return n({innerRef:x,draggableProps:{draggable:!a,onDragStart:j,onDragEnd:M,style:S?{opacity:1}:void 0,"data-univer-dnd-item":`true`,"data-univer-dnd-id":r,"data-univer-dnd-index":String(i)},dragHandleProps:{onPointerDown:k}},{isDragging:S,draggingOver:o==null||(t=o.destination)==null?void 0:t.droppableId})}function wr(e,t){switch(e){case`filter`:return t.t(`sheets-pivot-ui.filter`);case`row`:return t.t(`sheets-pivot-ui.row`);case`column`:return t.t(`sheets-pivot-ui.column`);case`value`:return t.t(`sheets-pivot-ui.value`)}}function Tr(e){switch(e){case`filter`:return/* @__PURE__ */ (0,d.jsx)(u.FilterIcon,{className:`univer-flex univer-flex-row univer-gap-4`});case`row`:return/* @__PURE__ */ (0,d.jsx)(u.ColumnIcon,{className:`univer-flex univer-flex-row univer-gap-4`});case`column`:return/* @__PURE__ */ (0,d.jsx)(u.RowIcon,{className:`univer-flex univer-flex-row univer-gap-4`});case`value`:return/* @__PURE__ */ (0,d.jsx)(u.FunctionIcon,{className:`univer-flex univer-flex-row univer-gap-4`})}}function Er(e){switch(e){case`filter`:return/* @__PURE__ */ (0,d.jsxs)(`svg`,{xmlns:`http://www.w3.org/2000/svg`,width:`32`,height:`32`,viewBox:`0 0 32 32`,fill:`none`,children:[/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M4.99957 1.875C2.84556 1.875 1.67491 4.3932 3.06336 6.04001L11.5946 16.1587V26.4187C11.5946 27.4582 12.2298 28.3922 13.1966 28.7741L16.1601 29.9449C17.8221 30.6015 19.6232 29.3766 19.6232 27.5895V16.1587L28.1544 6.04C29.5428 4.3932 28.3722 1.875 26.2182 1.875H4.99957Z`,fill:`#ECECEC`}),/* @__PURE__ */ (0,d.jsx)(`rect`,{x:`21.9404`,y:`17.8544`,width:`7.59766`,height:`2.2793`,rx:`1.13965`,fill:`#274FEE`}),/* @__PURE__ */ (0,d.jsx)(`rect`,{x:`21.9404`,y:`21.9067`,width:`7.59766`,height:`2.2793`,rx:`1.13965`,fill:`#274FEE`}),/* @__PURE__ */ (0,d.jsx)(`rect`,{x:`21.9404`,y:`25.959`,width:`7.59766`,height:`2.2793`,rx:`1.13965`,fill:`#274FEE`})]});case`row`:return/* @__PURE__ */ (0,d.jsxs)(`svg`,{xmlns:`http://www.w3.org/2000/svg`,width:`32`,height:`32`,viewBox:`0 0 32 32`,fill:`none`,children:[/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M2 12.3661C2 12.0347 2.26863 11.7661 2.6 11.7661H9.24705C9.57842 11.7661 9.84705 12.0347 9.84705 12.3661V29.278C9.84705 29.6093 9.57842 29.878 9.24705 29.878H4C2.89543 29.878 2 28.9825 2 27.878V12.3661Z`,fill:`#D8D8D8`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M2 4.0863C2 2.98173 2.89543 2.0863 4 2.0863H9.24705C9.57842 2.0863 9.84705 2.35493 9.84705 2.6863V8.73981C9.84705 9.07118 9.57842 9.33981 9.24705 9.33981H2.6C2.26863 9.33981 2 9.07118 2 8.73981V4.0863Z`,fill:`#D8D8D8`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M12.2983 12.4011C12.2982 12.0697 12.5669 11.8009 12.8983 11.8009L29.3931 11.8009C29.7243 11.8009 29.9929 12.0694 29.9931 12.4007L29.9993 27.9129C29.9997 29.0178 29.1042 29.9137 27.9993 29.9137H12.9051C12.5738 29.9137 12.3053 29.6452 12.3051 29.314L12.2983 12.4011Z`,fill:`#ECECEC`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M12.2517 2.6863C12.2517 2.35493 12.5203 2.0863 12.8517 2.0863H27.9582C29.0628 2.0863 29.9582 2.98173 29.9582 4.0863V8.73627C29.9582 9.06764 29.6896 9.33627 29.3582 9.33627H12.8517C12.5203 9.33627 12.2517 9.06764 12.2517 8.73627L12.2517 2.6863Z`,fill:`#274FEE`})]});case`column`:return/* @__PURE__ */ (0,d.jsxs)(`svg`,{xmlns:`http://www.w3.org/2000/svg`,width:`32`,height:`32`,viewBox:`0 0 32 32`,fill:`none`,children:[/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M2 12.3661C2 12.0347 2.26863 11.7661 2.6 11.7661H9.24705C9.57842 11.7661 9.84705 12.0347 9.84705 12.3661V29.278C9.84705 29.6093 9.57842 29.878 9.24705 29.878H4C2.89543 29.878 2 28.9825 2 27.878V12.3661Z`,fill:`#274FEE`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M2 4.0863C2 2.98173 2.89543 2.0863 4 2.0863H9.24705C9.57842 2.0863 9.84705 2.35493 9.84705 2.6863V8.73981C9.84705 9.07118 9.57842 9.33981 9.24705 9.33981H2.6C2.26863 9.33981 2 9.07118 2 8.73981V4.0863Z`,fill:`#D8D8D8`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M12.2637 2.6863C12.2637 2.35493 12.5323 2.0863 12.8637 2.0863H27.9702C29.0748 2.0863 29.9702 2.98173 29.9702 4.0863V8.73627C29.9702 9.06764 29.7016 9.33627 29.3702 9.33627H12.8637C12.5323 9.33627 12.2637 9.06764 12.2637 8.73627L12.2637 2.6863Z`,fill:`#D8D8D8`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M12.2982 12.4011C12.2981 12.0697 12.5667 11.8009 12.8982 11.8009L29.3929 11.8009C29.7242 11.8009 29.9928 12.0694 29.9929 12.4007L29.9992 27.9129C29.9996 29.0178 29.104 29.9137 27.9992 29.9137H12.905C12.5737 29.9137 12.3051 29.6452 12.305 29.314L12.2982 12.4011Z`,fill:`#ECECEC`})]});case`value`:return/* @__PURE__ */ (0,d.jsxs)(`svg`,{xmlns:`http://www.w3.org/2000/svg`,width:`32`,height:`32`,viewBox:`0 0 32 32`,fill:`none`,children:[/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M2 12.3661C2 12.0347 2.26863 11.7661 2.6 11.7661H9.24705C9.57842 11.7661 9.84705 12.0347 9.84705 12.3661V29.278C9.84705 29.6093 9.57842 29.878 9.24705 29.878H4C2.89543 29.878 2 28.9825 2 27.878V12.3661Z`,fill:`#D8D8D8`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M2 4.0863C2 2.98173 2.89543 2.0863 4 2.0863H9.24705C9.57842 2.0863 9.84705 2.35493 9.84705 2.6863V8.73981C9.84705 9.07118 9.57842 9.33981 9.24705 9.33981H2.6C2.26863 9.33981 2 9.07118 2 8.73981V4.0863Z`,fill:`#D8D8D8`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M12.2637 2.6863C12.2637 2.35493 12.5323 2.0863 12.8637 2.0863H27.9702C29.0748 2.0863 29.9702 2.98173 29.9702 4.0863V8.73627C29.9702 9.06764 29.7016 9.33627 29.3702 9.33627H12.8637C12.5323 9.33627 12.2637 9.06764 12.2637 8.73627L12.2637 2.6863Z`,fill:`#D8D8D8`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M12.2982 12.4011C12.2981 12.0697 12.5667 11.8009 12.8982 11.8009L29.3929 11.8009C29.7242 11.8009 29.9928 12.0694 29.9929 12.4007L29.9992 27.9129C29.9996 29.0178 29.104 29.9137 27.9992 29.9137H12.905C12.5737 29.9137 12.3051 29.6452 12.305 29.314L12.2982 12.4011Z`,fill:`#274FEE`})]})}}function Dr(e,t){switch(e){case`filter`:return t.t(`sheets-pivot-ui.filterDesc`);case`row`:return t.t(`sheets-pivot-ui.rowDesc`);case`column`:return t.t(`sheets-pivot-ui.columnDesc`);case`value`:return t.t(`sheets-pivot-ui.valueDesc`)}}function Or(e){let t=(0,i.useDependency)(n.LocaleService),{areaType:r}=e,a=Dr(r,t),o=Er(r);return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-h-[140px] univer-flex-col univer-items-center univer-justify-center univer-gap-1 univer-self-stretch univer-px-2`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-size-8`,children:o}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-center univer-text-xs univer-leading-4 univer-text-gray-600 dark:!univer-text-gray-200`,children:a})]})}function kr(e){let{areaType:t,items:r,positionInfo:a,pivotTableId:o,pivotTableInfo:c}=e,l=(0,i.useDependency)(n.LocaleService);return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-min-h-[200px] univer-flex-1 univer-p-1`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-2 univer-mt-0 univer-flex univer-w-full univer-text-sm univer-leading-4 univer-text-gray-900`,children:[Tr(t),/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-inline-block univer-align-top`,children:wr(t,l)})]}),/* @__PURE__ */ (0,d.jsx)(Sr,{droppableId:`${t}`,isCombineEnabled:!1,isDropDisabled:!1,ignoreContainerClipping:!1,direction:`vertical`,children:(e,n)=>/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-box-border univer-h-[161px] univer-w-full univer-flex-col univer-items-center univer-justify-center univer-overflow-x-hidden univer-rounded-lg univer-bg-gray-50 univer-py-2 dark:!univer-bg-gray-900`,s.borderClassName,{"univer-border-primary-600":n.isDraggingOver}),children:/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-h-full univer-overflow-y-auto`,s.scrollbarClassName),children:/* @__PURE__ */ (0,d.jsxs)(`div`,{ref:e.innerRef,className:`univer-box-border univer-h-full univer-px-2`,...e.droppableProps,children:[r.length>0&&r.map((e,n)=>/* @__PURE__ */ (0,d.jsx)(`div`,{children:/* @__PURE__ */ (0,d.jsx)(Cr,{isDragDisabled:!1,draggableId:e.tableFieldId,index:n,children:(n,r)=>/* @__PURE__ */ (0,d.jsx)(d.Fragment,{children:/* @__PURE__ */ (0,d.jsx)(`div`,{ref:n.innerRef,...n.draggableProps,...n.dragHandleProps,style:n.draggableProps.style,className:(0,s.clsx)(`univer-flex univer-items-center univer-gap-1 univer-self-stretch univer-border univer-border-transparent univer-p-0.5 univer-leading-4 hover:univer-bg-gray-100 dark:hover:!univer-bg-gray-700`,r.draggingOver&&`univer-border-primary-600`),children:/* @__PURE__ */ (0,d.jsx)(Ar,{pivotTableId:o,positionInfo:a,item:e,areaType:t,pivotTableInfo:c})})})},e.tableFieldId)},e.tableFieldId)),r.length===0&&/* @__PURE__ */ (0,d.jsx)(Or,{areaType:t})]})})})})]})}function Ar(t){let{areaType:r,item:a,positionInfo:o,pivotTableId:c,pivotTableInfo:l}=t,{unitId:f,subUnitId:p}=o,m=a.tableFieldId===mt,h=(0,i.useDependency)(n.ICommandService),g=(0,i.useDependency)(n.LocaleService),_=()=>{if(m){let e=(l.valueFields||[]).map(e=>e.tableFieldId);wt({pivotTableId:c,unitId:f,subUnitId:p,commandService:h},e)}else wt({pivotTableId:c,unitId:f,subUnitId:p,commandService:h},[a.tableFieldId])},v=()=>{f&&p&&Dt(h,f,p,c,a.tableFieldId)},y=()=>{f&&p&&Ot(h,c,a.tableFieldId,f,p,l)},b=t=>{let n=t===`column`?e.PivotTableValuePositionEnum.Row:e.PivotTableValuePositionEnum.Column,r=t===`column`?l.rowFields.length:l.columnFields.length;Tt({pivotTableId:c,unitId:f,subUnitId:p,commandService:h},n,r)},x=/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-inline-flex univer-flex-col univer-rounded-lg univer-border univer-border-gray-200 univer-bg-gray-0 univer-p-2 univer-shadow-lg`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 dark:!univer-text-gray-0`,onClick:()=>{_()},children:g.t(`sheets-pivot-ui.removeField`)}),r!==`value`&&!m&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 dark:!univer-text-gray-0`,onClick:()=>{v()},children:g.t(`sheets-pivot-ui.filterField`)}),m&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 dark:!univer-text-gray-0`,onClick:()=>{b(r)},children:r===`column`?g.t(`sheets-pivot-ui.moveToRow`):g.t(`sheets-pivot-ui.moveToColumn`)}),r===`value`&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 dark:!univer-text-gray-0`,onClick:()=>{y()},children:g.t(`sheets-pivot-ui.fieldSettings`)})]}),S=m?g.t(`sheets-pivot-ui.multipleValue`):a.displayName,C=/* @__PURE__ */ (0,d.jsx)(`bdi`,{dir:`auto`,children:S});return/* @__PURE__ */ (0,d.jsxs)(d.Fragment,{children:[/* @__PURE__ */ (0,d.jsx)(u.GripVerticalIcon,{}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-max-w-[66px] univer-flex-1 univer-shrink-0 univer-truncate univer-text-xs univer-leading-4 univer-text-gray-800 dark:!univer-text-gray-100`,children:/* @__PURE__ */ (0,d.jsx)(s.Tooltip,{title:C,children:C})}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-cursor-pointer`,children:/* @__PURE__ */ (0,d.jsx)(s.Dropdown,{align:`end`,overlay:x,children:/* @__PURE__ */ (0,d.jsx)(`span`,{children:/* @__PURE__ */ (0,d.jsx)(u.MoreHorizontalIcon,{})})})})]})}function jr(e){var t;let r=(0,i.useDependency)(n.IUniverInstanceService),a=(0,i.useDependency)(n.LocaleService),o=(0,i.useDependency)(n.ICommandService),s=(0,i.useDependency)(Q),{sourceRangeInfo:c,refreshHandler:l,pivotTableId:u,positionInfo:h}=e,{unitId:g,range:_,subUnitId:v}=c,y=r.getUnit(g),b=(y==null||(t=y.getSheetBySheetId(v))==null?void 0:t.getName())??``,[x,S]=(0,f.useState)(void 0),C=(0,f.useRef)(x);C.current=x;let[w,T]=(0,f.useState)(_),[E,D]=(0,f.useState)(b),[O,k]=(0,f.useState)(!1);(0,f.useEffect)(()=>{s.setIsRangeSelectorFocus(O)},[O,s]),(0,f.useEffect)(()=>{T(c.range),D(b)},[c,b]);let A=(0,f.useMemo)(()=>(0,n.debounce)(async e=>{let t=e.split(`,`).filter(e=>!!e).map(p.deserializeRangeWithSheet);if(!t[0])return;let n=t[0];if(!y||C.current)return;let r=n.sheetName||c.sheetName;T(n.range),D(r);let i={unitId:g,subUnitId:y.getSheetBySheetName(r).getSheetId(),range:{...n.range},sheetName:r};await xt(o,u,i,h.unitId,h.subUnitId),l()},300,{leading:!1,maxWait:1e3,trailing:!0}),[]);return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-4`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-2 univer-flex univer-h-5 univer-justify-between univer-text-sm univer-leading-5`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-justify univer-text-gray-900 dark:!univer-text-gray-0`,children:a.t(`sheets-pivot-ui.sourceRangeLabel`)}),/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-xs univer-text-gray-600 dark:!univer-text-gray-200`,children:a.t(`sheets-pivot-ui.sourceRangePlaceholder`)})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-relative`,children:[/* @__PURE__ */ (0,d.jsx)(m.RangeSelector,{unitId:g,subUnitId:v,initialValue:(0,p.serializeRangeWithSheet)(E,w),onChange:(e,t)=>{(0,p.serializeRangeWithSheet)(E,w)!==t&&A(t)},keepSheetReference:!0,supportAcrossSheet:!0,onVerify:(e,t)=>{let n=t.split(`,`).filter(e=>!!e);if(!n[0]){S(a.t(`sheets-pivot-ui.msg.emptySourceRange`));return}if(!e){S(a.t(`sheets-pivot-ui.msg.invalidSize`));return}let r=(0,p.deserializeRangeWithSheet)(n[0]),i=r.sheetName||c.sheetName;if(y)switch(yt(y.getSheetBySheetName(i),r.range)){case 2:S(void 0);break;case 0:S(a.t(`sheets-pivot-ui.msg.emptySourceRange`));break;case 1:S(a.t(`sheets-pivot-ui.msg.invalidSize`))}},maxRangeCount:1,onFocusChange:e=>{k(e)},onRangeSelectorDialogVisibleChange:e=>{k(!!e)}}),x?/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-my-1 univer-text-xs univer-text-red-500`,children:x}):null]})]})}function Mr(t){let{type:n}=t;switch(n){case e.PivotDataFieldDataTypeEnum.text:return/* @__PURE__ */ (0,d.jsx)(u.TextIcon,{className:`univer-mx-1.5 univer-my-0 univer-h-4 univer-text-xs univer-text-gray-200 dark:!univer-text-gray-600`});case e.PivotDataFieldDataTypeEnum.number:return/* @__PURE__ */ (0,d.jsx)(u.NumberIcon,{className:`univer-mx-1.5 univer-my-0 univer-h-4 univer-text-xs univer-text-gray-200 dark:!univer-text-gray-600`});case e.PivotDataFieldDataTypeEnum.date:return/* @__PURE__ */ (0,d.jsx)(u.CalendarIcon,{className:`univer-mx-1.5 univer-my-0 univer-h-4 univer-text-xs univer-text-gray-200 dark:!univer-text-gray-600`})}}function Nr(t){if(!t)return!1;if(t.type===e.PivotFilterTypeEnum.RecordFilter){var r;return t.dateRange!==void 0||(((r=t.selection)==null?void 0:r.mode)??n.FilterSelectionMode.All)!==n.FilterSelectionMode.All}return t.type!==e.PivotFilterTypeEnum.ManualFilter||t.isAll!==!0}function Pr(r){let{name:a,dataFieldId:o,checked:c,type:l,refreshHandler:p,pivotTableId:m,draggingId:h,pivotTableInfo:g,unitId:_,subUnitId:v}=r,[y,b]=(0,f.useState)(!1),x=(0,i.useDependency)(n.ICommandService),S=(0,i.useDependency)(n.LocaleService);if(!g)return null;let{rowFields:C,columnFields:w,filterFields:T,valueFields:E}=g,D=g.hiddenFields??[],O=[...C,...w,...T,...E,...D],k=D.find(e=>e.dataFieldId===o),A=async n=>{let r=!!n,i=!1;if(r){let n={pivotTableId:m,dataFieldId:o,fieldArea:l===e.PivotDataFieldDataTypeEnum.number?e.PivotTableFiledAreaEnum.Value:e.PivotTableFiledAreaEnum.Row,unitId:_,subUnitId:v,index:l===e.PivotDataFieldDataTypeEnum.number?E.length:C.length};i=await x.executeCommand(t.AddPivotFieldCommand.id,n)}else{let e=[];O.forEach(t=>{t.dataFieldId===o&&e.push(t.tableFieldId)}),i=await x.executeCommand(t.RemovePivotFieldCommand.id,{pivotTableId:m,fieldIds:e,unitId:_,subUnitId:v})}i&&p()},j=async n=>{let r=!1,i=``;if([...C,...w,...T,...D].forEach(e=>{e.dataFieldId===o&&(r=!0,i=e.tableFieldId)}),r&&n!==e.PivotTableFiledAreaEnum.Value){if(m){let e=Bt(g,n),r={pivotTableId:m,area:n,index:e,fieldId:i,unitId:_,subUnitId:v};await x.executeCommand(t.MovePivotFieldCommand.id,r)&&p()}}else if(m){let e={pivotTableId:m,dataFieldId:o,fieldArea:n,unitId:_,subUnitId:v,index:Bt(g,n)};await x.executeCommand(t.AddPivotFieldCommand.id,e)&&p()}};return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:(0,s.clsx)(`univer-group univer-relative univer-box-border univer-flex univer-h-8 univer-w-full univer-flex-row univer-items-center univer-rounded-md univer-px-1 univer-py-2 univer-leading-4 univer-transition-all hover:univer-bg-gray-100 dark:hover:!univer-bg-gray-700`,{"univer-flex univer-bg-gray-100 dark:!univer-bg-gray-700":h===o}),id:`source-${o}`,onContextMenu:e=>{e.preventDefault(),b(!0)},children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-mr-2 univer-w-4 rtl:univer-ml-2 rtl:univer-mr-0`,children:/* @__PURE__ */ (0,d.jsx)(u.GripVerticalIcon,{})}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{className:`univer-w-[14px] univer-text-sm univer-text-gray-200`,checked:c,onChange:A}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-w-0 univer-flex-1 univer-items-center univer-text-xs univer-leading-4 univer-text-gray-900 dark:!univer-text-gray-0`,children:[/* @__PURE__ */ (0,d.jsx)(Mr,{type:l}),/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-truncate`,children:a})]}),/* @__PURE__ */ (0,d.jsx)(s.Dropdown,{align:`end`,side:`bottom`,open:y,onOpenChange:b,overlay:/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[Nr(k==null?void 0:k.filterInfo)&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 hover:univer-bg-gray-100 dark:!univer-text-gray-0 dark:hover:!univer-bg-gray-700`,onClick:async()=>{k&&(b(!1),await x.executeCommand(t.SetPivotFilterCommand.id,{unitId:_,subUnitId:v,pivotTableId:m,tableFieldId:k.tableFieldId,items:[],partialItems:[],isAll:!0})&&p())},children:S.t(`sheets-pivot-ui.dialog.filter.clearFilter`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 hover:univer-bg-gray-100 dark:!univer-text-gray-0 dark:hover:!univer-bg-gray-700`,onClick:()=>{b(!1),j(e.PivotTableFiledAreaEnum.Row)},children:S.t(`sheets-pivot-ui.addToRow`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 hover:univer-bg-gray-100 dark:!univer-text-gray-0 dark:hover:!univer-bg-gray-700`,onClick:()=>{b(!1),j(e.PivotTableFiledAreaEnum.Column)},children:S.t(`sheets-pivot-ui.addToColumn`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 hover:univer-bg-gray-100 dark:!univer-text-gray-0 dark:hover:!univer-bg-gray-700`,onClick:()=>{b(!1),j(e.PivotTableFiledAreaEnum.Value)},children:S.t(`sheets-pivot-ui.addToValue`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 hover:univer-bg-gray-100 dark:!univer-text-gray-0 dark:hover:!univer-bg-gray-700`,onClick:()=>{b(!1),j(e.PivotTableFiledAreaEnum.Filter)},children:S.t(`sheets-pivot-ui.addToFilter`)})]}),children:/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-ml-1 univer-flex univer-size-5 univer-cursor-pointer univer-items-center univer-justify-center univer-rounded-md univer-transition-opacity hover:univer-bg-gray-200 rtl:univer-ml-0 rtl:univer-mr-1 dark:hover:!univer-bg-gray-700`,{"univer-opacity-100":y,"univer-opacity-0 group-hover:univer-opacity-100":!y}),onMouseDown:e=>e.stopPropagation(),onClick:e=>e.stopPropagation(),children:/* @__PURE__ */ (0,d.jsx)(u.MoreDownIcon,{className:`univer-text-gray-400 dark:!univer-text-gray-500`})})})]},o)}function Fr(e){let{sourceList:t,refreshHandler:r,pivotTableId:a,pivotTableInfo:o,unitId:c,draggingId:l,subUnitId:p}=e,m=(0,i.useDependency)(n.LocaleService),[h,g]=(0,f.useState)(``),_=h.trim().toLocaleLowerCase(),v=_?t.filter(e=>e.name.toLocaleLowerCase().includes(_)):t;return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-4 univer-flex univer-min-h-0 univer-flex-1 univer-flex-col`,children:[/* @__PURE__ */ (0,d.jsx)(`p`,{className:`univer-mb-1 univer-mt-0 univer-text-sm univer-font-medium univer-leading-5 univer-text-gray-900 dark:!univer-text-gray-0`,children:m.t(`sheets-pivot-ui.sourceFieldTile`)}),/* @__PURE__ */ (0,d.jsx)(`p`,{className:`univer-mb-2 univer-mt-0 univer-text-xs univer-leading-4 univer-text-gray-600 dark:!univer-text-gray-200`,children:m.t(`sheets-pivot-ui.sourceFieldDescription`)}),/* @__PURE__ */ (0,d.jsx)(s.Input,{allowClear:!0,className:`univer-mb-2`,placeholder:m.t(`sheets-pivot-ui.searchFieldPlaceholder`),slot:/* @__PURE__ */ (0,d.jsx)(u.SearchIcon,{}),value:h,onChange:g}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-box-border univer-min-h-0 univer-flex-1 univer-rounded-lg univer-bg-gray-0 univer-p-2 dark:!univer-bg-gray-900`,s.borderClassName),children:/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-relative univer-h-full univer-overflow-y-auto`,s.scrollbarClassName),"data-testid":`pivot-source-field-viewport`,children:/* @__PURE__ */ (0,d.jsx)(Sr,{droppableId:`source`,direction:`vertical`,isCombineEnabled:!1,ignoreContainerClipping:!1,isDropDisabled:!0,children:e=>/* @__PURE__ */ (0,d.jsxs)(`div`,{ref:e.innerRef,className:`univer-box-border univer-h-full univer-px-2`,...e.droppableProps,children:[v.map((e,t)=>/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-relative univer-flex univer-h-8 univer-w-full univer-items-center univer-gap-1 univer-self-stretch univer-border univer-border-transparent univer-p-0.5 univer-leading-4 hover:univer-bg-gray-100 dark:hover:!univer-bg-gray-700`,children:/* @__PURE__ */ (0,d.jsx)(Cr,{draggableId:e.id,isDragDisabled:!1,index:t,children:({innerRef:t,draggableProps:n,dragHandleProps:i})=>/* @__PURE__ */ (0,d.jsx)(`div`,{ref:t,...n,...i,className:`univer-h-8 univer-w-full`,style:n.style,children:/* @__PURE__ */ (0,d.jsx)(Pr,{draggingId:l,unitId:c,subUnitId:p,...e,refreshHandler:r,pivotTableId:a,pivotTableInfo:o})})},e.id)},e.id)),v.length===0&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-py-4 univer-text-center univer-text-xs univer-text-gray-500`,children:m.t(`sheets-pivot-ui.noMatchingFields`)})]})})})})]})}function Ir(e){let{pivotTableId:r,version:a,unitId:c,subUnitId:l,panelInfo:u}=e,p=(0,i.useDependency)(t.SheetsPivotTableConfigModel),m=(0,f.useMemo)(()=>p.watchPivotTableChanges(c,l,r).pipe((0,o.debounceTime)(0)),[p,c,l,r]),h=(0,i.useObservable)(m),g=(0,i.useDependency)(t.SheetsPivotRPCService),_=(0,i.useDependency)(Q),v=(0,i.useObservable)(_.pivotTablePanelVisible$,!0),y=(0,i.useDependency)(n.ICommandService),b=(0,i.useDependency)(n.ILogService),[x,S]=(0,f.useState)(u),[C,w]=(0,f.useState)(null),T=(0,f.useCallback)(async()=>{if(!p.getPivotTableConfig(c,l,r))return;let e=await g.getPivotDisplayConfig({unitId:c,subUnitId:l,pivotTableId:r,params:{type:t.PivotDisplayDataType.Panel,panel:{pivotTableId:r}}});e.result.panel&&S(e.result.panel)},[r,g,p,l,c]);(0,f.useEffect)(()=>{T().catch(e=>b.error(`[PivotPanel]`,e))},[b,h,r,T,a]);let E=async e=>{let{source:t,destination:n,draggableId:i}=e;w(null);let a={pivotTableInfo:x,pivotTableId:r,commandService:y,unitId:c,subUnitId:l},o=t.droppableId;if(n&&n.droppableId){let e=n.droppableId;await Ct(a,i,o,e,n.index),await T()}n===null&&(await bt(a,i,o),await T())},D=e=>{w({draggingId:e.draggableId})},O=e=>{let{draggableId:t,destination:n,source:r}=e;w({draggingId:t,destination:n,source:r})};if(!v||!x)return null;let{sourceList:k,rowFields:A,columnFields:j,filterFields:M,valueFields:N,sourceRange:P,positionInfo:F}=x;return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:(0,s.clsx)(`univer-mt-4 univer-flex univer-h-[calc(100%-16px)] univer-min-h-0 univer-w-full univer-flex-col univer-overflow-y-auto univer-overflow-x-hidden`,s.scrollbarClassName),children:[/* @__PURE__ */ (0,d.jsx)(jr,{sourceRangeInfo:P,pivotTableId:r,refreshHandler:T,positionInfo:F}),/* @__PURE__ */ (0,d.jsxs)(xr,{onDragEnd:E,onDragStart:D,onDragUpdate:O,children:[/* @__PURE__ */ (0,d.jsx)(Fr,{draggingId:(C==null?void 0:C.draggingId)??``,sourceList:k,refreshHandler:T,pivotTableId:r,pivotTableInfo:x,unitId:c,subUnitId:l}),/* @__PURE__ */ (0,d.jsx)(Lr,{rowFields:A,columnFields:j,filterFields:M,valueFields:N,refreshHandler:T,positionInfo:F,pivotTableId:r,pivotTableInfo:x})]})]})}function Lr(e){let{rowFields:t,columnFields:n,filterFields:r,valueFields:i,refreshHandler:a,pivotTableInfo:o,positionInfo:s,pivotTableId:c}=e;return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-shrink-0`,"data-testid":`pivot-field-areas`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-row univer-gap-4`,children:[/* @__PURE__ */ (0,d.jsx)(kr,{areaType:`filter`,items:r,positionInfo:s,pivotTableId:c,pivotTableInfo:o,refreshHandler:a}),/* @__PURE__ */ (0,d.jsx)(kr,{areaType:`column`,items:n,positionInfo:s,pivotTableId:c,pivotTableInfo:o,refreshHandler:a})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-row univer-gap-4`,children:[/* @__PURE__ */ (0,d.jsx)(kr,{areaType:`row`,items:t,positionInfo:s,pivotTableId:c,pivotTableInfo:o,refreshHandler:a}),/* @__PURE__ */ (0,d.jsx)(kr,{areaType:`value`,items:i,positionInfo:s,pivotTableId:c,pivotTableInfo:o,refreshHandler:a})]})]})}function Rr(e){let{className:t,unitId:r,subUnitId:a,pivotTableId:o}=e,[c,l]=(0,f.useState)(0),u=(0,i.useDependency)(n.LocaleService),p=u.getDirection();return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:(0,s.clsx)(`univer-flex univer-size-full univer-min-h-0 univer-flex-col`,t),dir:p,children:[/* @__PURE__ */ (0,d.jsx)(s.Segmented,{className:`univer-my-3 univer-w-full univer-shrink-0`,items:[{label:u.t(`sheets-pivot-ui.fieldPanel`),value:0},{label:u.t(`sheets-pivot-ui.configPanel`),value:1}],value:c,onChange:l}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-min-h-0 univer-flex-1`,c===0?`univer-overflow-hidden`:`univer-overflow-auto`),children:c===0?/* @__PURE__ */ (0,d.jsx)(Ir,{...e}):/* @__PURE__ */ (0,d.jsx)(mr,{unitId:r,subUnitId:a,pivotTableId:o})})]})}function zr(e){let{unitId:t,subUnitId:n,pivotTableId:r,tableFieldId:i,type:a}=e,o=JSON.stringify([t,n,r,i,a]);return/* @__PURE__ */ (0,d.jsx)(Br,{...e},o)}function Br({unitId:r,subUnitId:a,pivotTableId:o,tableFieldId:c,type:l,baseItems:u,onConfirm:p,onCancel:m}){var h;let g=(0,i.useDependency)(t.SheetsPivotTableConfigModel),_=(0,i.useDependency)(n.LocaleService),v=(0,i.useObservable)(_.direction$,_.getDirection()),y=de.find(e=>e.type===l),b=g.getPivotTableConfig(r,a,o),x=b==null||(h=b.fieldsConfig.measure[c])==null?void 0:h.showDataAs,S=()=>{var t;if((x==null?void 0:x.type)!==l||!x.baseFieldId)return``;if(x.baseItemType===e.PivotShowAsBaseItemTypeEnum.previous)return`relative:previous`;if(x.baseItemType===e.PivotShowAsBaseItemTypeEnum.next)return`relative:next`;let n=((t=u[x.baseFieldId])==null?void 0:t.items.indexOf(x.baseItem))??-1;return n<0?``:`item:${n}`},[C,w]=(0,f.useState)((x==null?void 0:x.type)===l?x.baseFieldId:``),[T,E]=(0,f.useState)(S),D=(0,f.useRef)(!1),[O,k]=(0,f.useState)(!1),A=g.getPivotTableConfig(r,a,o),j=g.getCollection(r,o),M=A==null?void 0:A.fieldsConfig.measure[c],N=!!(M&&j&&(j.fields[M.dataFieldId]||j.customFields.find(e=>e.id===M.dataFieldId))),P=(A&&j&&y?pe(A,j,y):[]).filter(e=>{let t=u[e];return(y==null?void 0:y.baseItemMode)===`none`||!!(t&&((y==null?void 0:y.baseItemMode)===`item-or-relative`||t.items.length>0))}),F=P.map(e=>({label:A.fieldsConfig.dimension[e].displayName,value:e})),I=A==null?void 0:A.fieldsConfig.dimension[C],L=I&&j&&(j.fields[I.dataFieldId]??j.customFields.find(e=>e.id===I.dataFieldId)),R=u[C],z=(R==null?void 0:R.items.map((t,r)=>{var i;let a=R.itemTypes[r],o=(i=R.itemLabels)==null?void 0:i[t],s=ht(t,a),c=t;return s?c=_.t(s):o===void 0?a===e.PivotDataFieldDataTypeEnum.other?c=_.t(`sheets-pivot-ui.showValuesAs.other`):(a===e.PivotDataFieldDataTypeEnum.date||a===e.PivotDataFieldDataTypeEnum.number)&&R.format&&(c=n.numfmt.format(R.format,Number(t))):c=o,{label:c,value:`item:${r}`,itemKey:t}}))??[];(y==null?void 0:y.baseItemMode)===`item-or-relative`&&z.push({label:_.t(`sheets-pivot-ui.showValuesAs.previous`),value:`relative:previous`,itemKey:``,itemType:e.PivotShowAsBaseItemTypeEnum.previous},{label:_.t(`sheets-pivot-ui.showValuesAs.next`),value:`relative:next`,itemKey:``,itemType:e.PivotShowAsBaseItemTypeEnum.next});let B=z.find(e=>e.value===T),V=!!(y!=null&&y.baseItemMode&&y.baseItemMode!==`none`),ee=!!(y&&A&&j&&N&&A.fieldsConfig.valueFields.includes(c)&&P.includes(C)&&L&&(!V||B));return/* @__PURE__ */ (0,d.jsxs)(`div`,{dir:v,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-4`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:_.t(`sheets-pivot-ui.showValuesAs.baseField`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-full`,value:C,options:F,onChange:e=>{w(e),E(``)}})]}),V&&/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-4`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:_.t(`sheets-pivot-ui.showValuesAs.baseItem`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-full`,value:T,options:z,disabled:!C,onChange:E})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-justify-end univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:m,children:_.t(`sheets-pivot-ui.showValuesAs.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:!ee||O,onClick:async()=>{let e=g.getPivotTableConfig(r,a,o),t=g.getCollection(r,o),n=e==null?void 0:e.fieldsConfig.measure[c];if(!y||!e||!t||!n||D.current||!e.fieldsConfig.valueFields.includes(c)||!(t.fields[n.dataFieldId]||t.customFields.find(e=>e.id===n.dataFieldId))||!pe(e,t,y).includes(C)||V&&!B)return;let i={type:l,baseFieldId:C,baseItem:(B==null?void 0:B.itemKey)??``};(B==null?void 0:B.itemType)!==void 0&&(i.baseItemType=B.itemType),D.current=!0,k(!0);try{await p(i)}catch{}finally{D.current=!1,k(!1)}},children:_.t(`sheets-pivot-ui.showValuesAs.ok`)})]})]})}const Vr=e=>{let[t,n]=(0,f.useState)(!1),r=(0,i.useDependency)(Q);return(0,f.useEffect)(()=>{r.setIsRangeSelector2Focus(t)},[t,r]),/* @__PURE__ */ (0,d.jsx)(m.RangeSelector,{...e,onFocusChange:t=>{var r;(r=e.onFocusChange)==null||r.call(e,t),n(t)}})},Hr=(e,t,n,r,i,a)=>/* @__PURE__ */ (0,d.jsx)(Vr,{unitId:e,subUnitId:t,initialValue:r,onChange:(e,t)=>n(t),maxRangeCount:1,supportAcrossSheet:!0,keepSheetReference:!0}),Ur=(e,t)=>{switch(e){case 1:return t.t(`sheets-pivot-ui.msg.invalidSize`);case 0:return t.t(`sheets-pivot-ui.msg.emptySourceRange`);default:return``}};function Wr(e){let{unitId:r,sourceRange:a,positionRange:o,sourceSheetName:c,positionSheetName:l,onConfirm:u,onCancel:m}=e,h=(0,i.useDependency)(n.LocaleService),g=(0,i.useDependency)(n.IUniverInstanceService),_=h.getDirection(),v=g.getUnit(r),y=(0,f.useMemo)(()=>v.getActiveSheet().getSheetId(),[v]),[b,x]=(0,f.useState)(a),[S,C]=(0,f.useState)(o),[w,T]=(0,f.useState)(c),[E,D]=(0,f.useState)(l),O=(0,f.useRef)(void 0),[k,A]=(0,f.useState)(),[j,M]=(0,f.useState)(t.PositionType.New),[N,P]=(0,f.useState)(!0),[F,I]=(0,f.useState)(!0),[L,R]=(0,f.useState)(void 0);(0,f.useEffect)(()=>{let e=yt(v.getSheetBySheetName(w),b);I(e===2),R(Ur(e,h))},[b,w,v,h]);let z=e=>{if((0,p.serializeRangeWithSheet)(w,b)===e)return;let t=e.split(`,`).filter(e=>!!e).map(p.deserializeRangeWithSheet);if(Array.isArray(t)&&t.length>0){let{range:e,sheetName:n}=t[0];x(e),T(n)}t[0]||R(h.t(`sheets-pivot-ui.msg.emptySourceRange`))},B=Hr(r,y,e=>{if(S&&(0,p.serializeRangeWithSheet)(E,S)===e)return;let t=e.split(`,`).filter(e=>!!e).map(p.deserializeRangeWithSheet);if(t.length===0){C(null);return}let r=t[0];C(r.range),D(r.sheetName||c),r.sheetName===c&&n.Rectangle.intersects(r.range,b)?P(!1):P(!0)},S?(0,p.serializeRangeWithSheet)(E,S):``,!1,k),V=()=>!!(b&&S&&w&&E&&j===t.PositionType.Existing&&N&&F)||!!(b&&w&&j===t.PositionType.New&&F);return(0,f.useEffect)(()=>{A(O.current)},[]),/* @__PURE__ */ (0,d.jsxs)(`div`,{ref:O,dir:_,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-h-5 univer-text-sm univer-font-medium`,children:h.t(`sheets-pivot-ui.dialog.source.sourceData`)}),Hr(r,y,z,(0,p.serializeRangeWithSheet)(w,b),!0,k),L&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-2 univer-h-5 univer-text-xs univer-text-red-400`,children:L}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-mt-5 univer-h-5 univer-text-sm univer-font-medium`,children:h.t(`sheets-pivot-ui.dialog.source.position`)}),/* @__PURE__ */ (0,d.jsxs)(s.RadioGroup,{value:j,onChange:e=>M(e),className:`univer-flex univer-flex-col univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Radio,{value:t.PositionType.New,children:h.t(`sheets-pivot-ui.dialog.source.newSheet`)}),/* @__PURE__ */ (0,d.jsx)(s.Radio,{value:t.PositionType.Existing,children:h.t(`sheets-pivot-ui.dialog.source.existingSheet`)})]}),j===t.PositionType.Existing&&/* @__PURE__ */ (0,d.jsxs)(d.Fragment,{children:[B,!N&&/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-h-5 univer-text-sm univer-text-red-400`,children:h.t(`sheets-pivot-ui.dialog.source.intersect`)})]}),/* @__PURE__ */ (0,d.jsx)(`footer`,{className:`univer-mt-5`,children:/* @__PURE__ */ (0,d.jsxs)(s.ActionRow,{className:`univer-flex univer-justify-end univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:()=>m(),children:h.t(`sheets-pivot-ui.dialog.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,onClick:()=>{V()&&(j===t.PositionType.Existing?u({sourceRange:b,sourceSheetName:w,positionRange:S,positionSheetName:E,positionType:j}):u({sourceRange:b,sourceSheetName:w,positionType:j}))},children:h.t(`sheets-pivot-ui.dialog.ok`)})]})})]})}Wr.componentKey=A;const Gr=[{id:`equal`,localeKey:`sheets-pivot-ui.valueFilter.equal`,operator:e.ST_PivotFilterOperatorEnum.valueEqual},{id:`notEqual`,localeKey:`sheets-pivot-ui.valueFilter.notEqual`,operator:e.ST_PivotFilterOperatorEnum.valueNotEqual},{id:`greaterThan`,localeKey:`sheets-pivot-ui.valueFilter.greaterThan`,operator:e.ST_PivotFilterOperatorEnum.valueGreaterThan},{id:`greaterThanOrEqual`,localeKey:`sheets-pivot-ui.valueFilter.greaterThanOrEqual`,operator:e.ST_PivotFilterOperatorEnum.valueGreaterThanOrEqual},{id:`lessThan`,localeKey:`sheets-pivot-ui.valueFilter.lessThan`,operator:e.ST_PivotFilterOperatorEnum.valueLessThan},{id:`lessThanOrEqual`,localeKey:`sheets-pivot-ui.valueFilter.lessThanOrEqual`,operator:e.ST_PivotFilterOperatorEnum.valueLessThanOrEqual},{id:`between`,localeKey:`sheets-pivot-ui.valueFilter.between`,operator:e.ST_PivotFilterOperatorEnum.valueBetween,isBetween:!0},{id:`notBetween`,localeKey:`sheets-pivot-ui.valueFilter.notBetween`,operator:e.ST_PivotFilterOperatorEnum.valueNotBetween,isBetween:!0},{id:`topCount`,localeKey:`sheets-pivot-ui.valueFilter.topCount`,operator:e.ST_PivotFilterOperatorEnum.count,isCount:!0},{id:`bottomCount`,localeKey:`sheets-pivot-ui.valueFilter.bottomCount`,operator:e.ST_PivotFilterOperatorEnum.count,isBottom:!0,isCount:!0},{id:`topPercent`,localeKey:`sheets-pivot-ui.valueFilter.topPercent`,operator:e.ST_PivotFilterOperatorEnum.percent,isPercent:!0},{id:`bottomPercent`,localeKey:`sheets-pivot-ui.valueFilter.bottomPercent`,operator:e.ST_PivotFilterOperatorEnum.percent,isBottom:!0,isPercent:!0}];function Kr({targetFieldId:t,targetFieldName:r,valueFields:a,defaultValueFieldId:o,currentFilter:c,onConfirm:l,onClear:u,onCancel:p}){function m(t){var n;return(t==null?void 0:t.operator)===e.ST_PivotFilterOperatorEnum.count?t.isBottom?`bottomCount`:`topCount`:(t==null?void 0:t.operator)===e.ST_PivotFilterOperatorEnum.percent?t.isBottom?`bottomPercent`:`topPercent`:((n=Gr.find(e=>e.operator===(t==null?void 0:t.operator)))==null?void 0:n.id)??`greaterThan`}let h=(0,i.useDependency)(n.LocaleService),g=(0,i.useObservable)(h.direction$,h.getDirection()),_=c==null?void 0:c.expected,[v,y]=(0,f.useState)(()=>{let e=c==null?void 0:c.valueFieldId;return e&&a.some(t=>t.id===e)?e:o&&a.some(e=>e.id===o)?o:a.length===1?a[0].id:``}),[b,x]=(0,f.useState)(()=>m(c)),[S,C]=(0,f.useState)(()=>String(Array.isArray(_)?_[0]:_??0)),[w,T]=(0,f.useState)(()=>String(Array.isArray(_)?_[1]:0)),E=(0,f.useRef)(!1),[D,O]=(0,f.useState)(!1),k=Gr.find(e=>e.id===b),A=S.trim()!==``&&Number.isFinite(Number(S)),j=!k.isBetween||w.trim()!==``&&Number.isFinite(Number(w)),M=!k.isCount||Number.isInteger(Number(S))&&Number(S)>=1,N=!k.isPercent||Number(S)>=0&&Number(S)<=100,P=!!(a.some(e=>e.id===v)&&A&&j&&M&&N),F=async e=>{if(!E.current){E.current=!0,O(!0);try{await e()}catch{}finally{E.current=!1,O(!1)}}},I=()=>F(async()=>{if(!P)return;let e={operator:k.operator,expected:k.isBetween?[Number(S),Number(w)]:Number(S),valueFieldId:v};k.isBottom&&(e.isBottom=!0),await l(e)}),L=()=>F(u);return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,"data-target-field-id":t,dir:g,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium`,children:/* @__PURE__ */ (0,d.jsx)(`bdi`,{dir:`auto`,children:r})}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-flex-col univer-gap-2 univer-text-sm`,children:[h.t(`sheets-pivot-ui.valueFilter.valueField`),/* @__PURE__ */ (0,d.jsx)(s.Select,{"aria-label":h.t(`sheets-pivot-ui.valueFilter.valueField`),"data-testid":`pivot-value-filter-value-field`,value:v,options:[{value:``,label:h.t(`sheets-pivot-ui.valueFilter.selectValueField`)},...a.map(e=>({value:e.id,label:e.label}))],onChange:y})]}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-flex-col univer-gap-2 univer-text-sm`,children:[h.t(`sheets-pivot-ui.valueFilter.condition`),/* @__PURE__ */ (0,d.jsx)(s.Select,{"aria-label":h.t(`sheets-pivot-ui.valueFilter.condition`),"data-testid":`pivot-value-filter-rule`,value:b,options:Gr.map(e=>({value:e.id,label:h.t(e.localeKey)})),onChange:e=>x(e)})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Input,{"aria-label":h.t(`sheets-pivot-ui.valueFilter.condition`),"aria-invalid":!A||!M||!N,className:`univer-min-w-0 univer-flex-1`,"data-testid":`pivot-value-filter-expected`,inputMode:`decimal`,value:S,onChange:C}),k.isBetween&&/* @__PURE__ */ (0,d.jsx)(s.Input,{"aria-label":h.t(`sheets-pivot-ui.valueFilter.condition`),"aria-invalid":!j,className:`univer-min-w-0 univer-flex-1`,"data-testid":`pivot-value-filter-expected`,inputMode:`decimal`,value:w,onChange:T})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-justify-between univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{"data-testid":`pivot-value-filter-clear`,disabled:D,onClick:L,children:h.t(`sheets-pivot-ui.valueFilter.clear`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{disabled:D,onClick:p,children:h.t(`sheets-pivot-ui.valueFilter.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{"data-testid":`pivot-value-filter-confirm`,variant:`primary`,disabled:!P||D,onClick:I,children:h.t(`sheets-pivot-ui.valueFilter.ok`)})]})]})]})}function qr(e){let{pivotTableId:r,tableFieldId:a,closeFn:o,unitId:c,subUnitId:l,format:p,tableFieldInfo:m,dataFieldInfo:h,version:g}=e,[_,v]=(0,f.useState)(m.displayName),[y,b]=(0,f.useState)(m.subTotalType),x=(0,i.useDependency)(n.LocaleService),w=(0,i.useDependency)(n.ICommandService),T=(0,i.useDependency)(i.IDialogService),E={...(0,i.useDependency)(t.SheetsPivotTableConfigModel).getTextInfo()};for(let e of[`sheets-pivot-ui.averageName`,`sheets-pivot-ui.countName`,`sheets-pivot-ui.countNumsName`,`sheets-pivot-ui.maxName`,`sheets-pivot-ui.minName`,`sheets-pivot-ui.productName`,`sheets-pivot-ui.stdDevName`,`sheets-pivot-ui.stdDevpName`,`sheets-pivot-ui.sumName`,`sheets-pivot-ui.varName`,`sheets-pivot-ui.varpName`])E[e]=x.t(e);if((0,f.useEffect)(()=>{v(m.displayName),b(m.subTotalType)},[g]),!m||!h)return;let D=h.name,O=x.getDirection(),k=p??m.format,A=kt.map(e=>({value:`${e.type}`,type:e.type,label:x.t(e.textKey)})),j=e=>{let t=e;if(Array.isArray(e)&&(t=e[0]),t!==void 0){let e=Number(t);b(e),Vt(D,_,E)&&v(Ht(D,E,e))}};return/* @__PURE__ */ (0,d.jsxs)(`div`,{dir:O,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between univer-self-stretch univer-leading-5`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm univer-font-medium univer-leading-5 univer-text-gray-900`,children:x.t(`sheets-pivot-ui.dialog.fieldName`)}),/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-xs univer-leading-4 univer-text-gray-600`,children:gt(x.t(`sheets-pivot-ui.dialog.sourceName`),D)})]}),/* @__PURE__ */ (0,d.jsx)(`div`,{children:/* @__PURE__ */ (0,d.jsx)(s.Input,{className:`univer-mb-5 univer-mt-2 univer-box-border univer-h-8 univer-w-full univer-rounded-md univer-py-1.5 univer-text-sm univer-leading-5`,value:_,onChange:e=>v(e)})}),/* @__PURE__ */ (0,d.jsx)(`label`,{className:`univer-mb-2 univer-text-sm univer-font-medium univer-text-gray-900 dark:!univer-text-gray-0`,children:x.t(`sheets-pivot-ui.dialog.summarizeBy`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-h-40 univer-flex-1 univer-overflow-y-auto univer-rounded-lg`,s.borderClassName,s.scrollbarClassName),children:A.map(e=>/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-gap-1.5 univer-self-stretch univer-rounded-md univer-px-2 univer-py-1.5 univer-text-sm univer-capitalize univer-leading-5`,onClick:()=>j(e.value),children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-w-4 univer-text-base univer-text-primary-600`,children:/* @__PURE__ */ (0,d.jsx)(u.CheckMarkIcon,{className:(0,s.clsx)({"univer-hidden":y!==e.type})})}),/* @__PURE__ */ (0,d.jsx)(`span`,{children:e.label})]},e.type))}),/* @__PURE__ */ (0,d.jsx)(`footer`,{className:`univer-mt-5`,children:/* @__PURE__ */ (0,d.jsxs)(s.ActionRow,{className:`univer-flex univer-items-center univer-justify-between univer-self-stretch`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{className:`univer-flex univer-h-8 univer-cursor-pointer univer-items-center univer-justify-center univer-gap-1.5 univer-rounded univer-border-none univer-bg-gray-0 univer-px-3 univer-text-primary-600`,onClick:()=>{let e={id:S,draggable:!0,mask:!1,maskClosable:!1,width:400,children:{label:C,unitId:c,subUnitId:l,pivotTableId:r,tableFieldId:a,originPattern:k,tableFieldInfo:m,dataFieldInfo:h},dialogStyles:{header:{padding:`20px 24px 0`,height:0},body:{padding:`0 24px 20px`}},closable:!1,onClose:()=>{T.close(S)}};T.open(e)},children:x.t(`sheets-pivot-ui.dialog.format`)}),/* @__PURE__ */ (0,d.jsxs)(s.ActionRow,{className:`univer-flex univer-flex-1 univer-gap-2 univer-text-sm rtl:univer-flex-row-reverse`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:()=>{o&&o()},children:x.t(`sheets-pivot-ui.dialog.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:_===``,onClick:()=>{let e=m.displayName,n=m.subTotalType,i=m.format,s=e===_?void 0:_,u=n===y?void 0:y,d=i===k?void 0:k;if(e!==_||n!==y||i!==d){let e={displayName:s,subtotalType:u,format:d,tableFieldId:a,pivotTableId:r,unitId:c,subUnitId:l,tableFieldInfo:m,dataFieldInfo:h};w.executeCommand(t.UpdatePivotFieldSettingCommand.id,e)}o&&o()},children:x.t(`sheets-pivot-ui.dialog.ok`)})]})]})})]})}let Jr=class extends n.Disposable{constructor(e,t){super(),this._componentManager=e,this._iconManager=t,this._registerComponents(),this._registerIcons()}_registerComponents(){[[A,Wr],[_,Rr],[y,$n],[x,qr],[C,cr],[T,zr],[E,Kr],[D,Kn],[O,nr],[k,er]].forEach(([e,t])=>{this.disposeWithMe(this._componentManager.register(e,t))})}_registerIcons(){this.disposeWithMe(this._iconManager.register({PivotTableIcon:u.PivotTableIcon}))}};Jr=K([G(0,(0,n.Inject)(i.ComponentManager)),G(1,(0,n.Inject)(i.IconManager))],Jr);let Yr=class extends n.Disposable{constructor(e){super(),this._menuManagerService=e,this._initMenu()}_initMenu(){this._menuManagerService.mergeMenu(Qe)}};Yr=K([G(0,i.IMenuManagerService)],Yr);let Xr=class extends n.Disposable{constructor(e,t,n,r,i,a,o,s,c,l,u){super(),this._context=e,this._injector=t,this._sheetsPivotTableAdaptorModel=n,this._sheetSkeletonManagerService=r,this._sheetsPivotTableConfigModel=i,this._commandService=a,this._sheetPermissionCheckController=o,this._permissionService=s,this._localeService=c,this._selectionsService=l,this._cellAlerts=u,W(this,`_pivotButtonShapesArr`,[]),this._initButtonRender(),this._initSkeleton(),this._initListener(),this._initBlockedPivotAlert()}_initBlockedPivotAlert(){let e=`SHEET_PIVOT_BLOCKED:${this._context.unitId}`;this.disposeWithMe((0,o.merge)(this._selectionsService.selectionChanged$,this._context.unit.activeSheet$,this._sheetsPivotTableAdaptorModel.viewUpdate$).pipe((0,o.auditTime)(0)).subscribe(()=>{var t,n,r;let{unitId:i,unit:o}=this._context,s=this._selectionsService.currentSelectionParam,c=(t=o.getActiveSheet())==null?void 0:t.getSheetId(),l=(n=this._selectionsService.getCurrentSelections())==null||(n=n.slice(-1)[0])==null?void 0:n.primary;if(!c||!l||(s==null?void 0:s.unitId)!==i||s.sheetId!==c){this._cellAlerts.removeAlert(e);return}let{actualRow:u,actualColumn:d}=l,f=this._sheetsPivotTableAdaptorModel,m=f.getPivotTableIdByCell(i,c,u,d),h=m&&((r=f.getPivotItemRenderInfoCache(i,c,m))==null?void 0:r.blockedRange);if(!h){this._cellAlerts.removeAlert(e);return}let g=`sheets-pivot-ui.perform.blocked`;h.reason===`pivot`?g=`sheets-pivot-ui.perform.blockedByPivot`:h.reason===`merge`&&(g=`sheets-pivot-ui.perform.blockedByMerge`),this._cellAlerts.showAlert({key:e,type:a.CellAlertType.ERROR,title:this._localeService.t(`sheets-pivot-ui.perform.tips`),message:this._localeService.t(g,(0,p.serializeRange)(h)),location:{unitId:i,subUnitId:c,row:u,col:d},width:260,height:96})})),this.disposeWithMe(()=>this._cellAlerts.removeAlert(e))}_initButtonRender(){this.disposeWithMe((0,o.merge)(this._sheetsPivotTableAdaptorModel.viewUpdate$.pipe((0,o.auditTime)(0)),this._sheetSkeletonManagerService.currentSkeleton$,this._sheetsPivotTableConfigModel.pivotConfigChange$,this._permissionService.permissionPointUpdate$.pipe((0,o.auditTime)(100))).subscribe(()=>{if(!this._context.unit)return;let e=this._context.unitId,t=this._context.unit.getActiveSheet().getSheetId(),{scene:n}=this._context,i=this._localeService.getDirection(),o=this._sheetSkeletonManagerService.getCurrentSkeleton();if(!o)return;let s=this._pivotButtonShapesArr;s.forEach(e=>e.dispose()),s.splice(0,s.length),this._sheetsPivotTableAdaptorModel.getSubUnitPivotMatrix(e,t).forValue((c,l,u)=>{if(u!=null&&u.pbt){let d=`sheets-pivot-button-${c}-${l}-${e}-${t}`,{startX:f,startY:p,endX:m,endY:h}=(0,a.getCoordByCell)(c,l,n,o),g=m-f,_=h-p;if(_<8||g<8||this._sheetPermissionCheckController.permissionCheckWithRanges({workbookTypes:[r.WorkbookEditablePermission],worksheetTypes:[r.WorksheetViewPermission,r.WorksheetEditPermission,r.WorksheetPivotTablePermission],rangeTypes:[r.RangeProtectionPermissionViewPoint,r.RangeProtectionPermissionEditPoint]},[{startRow:c,endRow:c,startColumn:l,endColumn:l}],e,t)===!1)return;let v;if(An.has(u.pbt)){let{left:n,top:r}=Pn(i,`filter`,{startX:f,startY:p,endX:m,endY:h});v={left:n,top:r,height:16,width:16,zIndex:5e3,cellWidth:g,cellHeight:_,pivotParams:{col:l,row:c,unitId:e,subUnitId:t,buttonType:u.pbt}}}else if(jn.has(u.pbt)){let{left:n,top:r}=Pn(i,`collapse`,{startX:f,startY:p,endX:m,endY:h});v={left:n,top:r,height:16,width:16,zIndex:5e3,cellWidth:g,cellHeight:_,pivotParams:{col:l,row:c,unitId:e,subUnitId:t,buttonType:u.pbt}}}else return;let y=this._injector.createInstance(Mn,d,v);s.push(y)}}),s.length&&n.addObjects(s),n.makeDirty()}))}_initSkeleton(){var e;this.disposeWithMe(this._sheetsPivotTableAdaptorModel.markDirty$.subscribe(({unitId:e,subUnitId:t})=>{if(e===this._context.unitId&&(this._sheetSkeletonManagerService.makeDirty({sheetId:t}),this._context.unit.getActiveSheet().getSheetId()===t)){var n;this._sheetSkeletonManagerService.reCalculate(),(n=this._context.mainComponent)==null||n.makeDirty()}}));let t=this._context.unitId;this._context.unit.getSheets().forEach(e=>{var n;let r=e.getSheetId();(n=this._sheetsPivotTableAdaptorModel.getSubUnitPivotRenderCache(t,r))!=null&&n.size&&this._sheetSkeletonManagerService.makeDirty({sheetId:r})}),this._sheetSkeletonManagerService.reCalculate(),(e=this._context.mainComponent)==null||e.makeDirty()}_initListener(){let e=new n.DisposableCollection,{spreadsheet:r}=(0,a.getSheetObject)(this._context.unit,this._context);e.add(r.onDblclick$.subscribeEvent(e=>{if(e.button===2)return;let n=this._sheetSkeletonManagerService.getCurrentSkeleton();if(n==null)return;let r=this._context.scene,{row:i,column:o}=(0,a.getCoordByOffset)(e.offsetX,e.offsetY,r,n);this._commandService.executeCommand(t.PivotDrillDownCommand.id,{row:i,col:o})}))}};Xr=K([G(1,(0,n.Inject)(n.Injector)),G(2,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),G(3,(0,n.Inject)(a.SheetSkeletonManagerService)),G(4,(0,n.Inject)(t.SheetsPivotTableConfigModel)),G(5,(0,n.Inject)(n.ICommandService)),G(6,(0,n.Inject)(r.SheetPermissionCheckController)),G(7,(0,n.Inject)(n.IPermissionService)),G(8,(0,n.Inject)(n.LocaleService)),G(9,(0,n.Inject)(r.SheetsSelectionsService)),G(10,(0,n.Inject)(a.CellAlertManagerService))],Xr);var Zr=class e extends l.Shape{constructor(e){super(`SHEET_PIVOT_MOVE_KEY`),W(this,`_moveType`,void 0),this._moveType=e}_draw(t){this.width!==0&&this.height!==0&&e.drawWith(t,this)}static drawWith(e,t){let n=t._moveType;if(e.save(),e.strokeStyle=`#8bc34a`,e.lineWidth=2,n===`row`){let n=t.width;if(!n)return;e.beginPath(),e.moveTo(0,0),e.lineTo(0+n,0),e.moveTo(0,-10),e.lineTo(0,10),e.moveTo(0+n,-10),e.lineTo(0+n,10)}else{let n=t.height;if(!n)return;e.moveTo(0,0),e.lineTo(0,0+n),e.moveTo(-10,0),e.lineTo(10,0),e.moveTo(-10,0+n),e.lineTo(10,0+n)}e.stroke(),e.restore()}};let Qr=class extends n.Disposable{constructor(e,t,n,r,i,a){super(),this._context=e,this._sheetsPivotTableAdaptorModel=t,this._sheetSkeletonManagerService=n,this._commandService=r,this._selectionRenderService=i,this._sheetsSelectionsService=a,W(this,`_pivotSelectionMoveShape`,void 0),W(this,`_scenePointerMoveSubscribe`,void 0),W(this,`_scenePointerUpSubscribe`,void 0),W(this,`_selectionControl`,void 0),W(this,`_moveStartRange`,void 0),W(this,`_moveStartCellIndex`,void 0),W(this,`_moveRowOrCol`,`row`),W(this,`_targetRange`,void 0),W(this,`_movePivotId`,void 0),this._initControl()}_controlMoving(e,t){let n=this._context.scene,r=n.getScrollXYInfoByViewport(l.Vector2.FromArray([e,t])),{scaleX:i,scaleY:a}=n.getAncestorScale(),o=this._sheetSkeletonManagerService.getCurrentSkeleton();if(!o||!this._moveStartRange||!this._moveStartCellIndex)return;let{row:s,column:c}=o.getCellIndexByOffset(e,t,i,a,r);if(this._moveRowOrCol===`row`){var u;let e=0,t=0,n=this._moveStartRange.endColumn-this._moveStartRange.startColumn+1;if(this._moveStartCellIndex.column===this._moveStartRange.startColumn)e=c,t=c+n-1;else if(this._moveStartCellIndex.column===this._moveStartRange.endColumn)t=c,e=c-n+1;else{let r=this._moveStartCellIndex.column-this._moveStartRange.startColumn;e=c-r,t=c-r+n-1}let r=o.getCellWithCoordByIndex(s,e),i=o.getCellWithCoordByIndex(s,t),{startX:a,endY:l}=r,{endX:d}=i;this._targetRange={startRow:s,endRow:s,startColumn:e,endColumn:t},(u=this._pivotSelectionMoveShape)==null||u.transformByState({left:a,top:l,width:d-a,height:20})}else{var d;let e=0,t=0,n=this._moveStartRange.endRow-this._moveStartRange.startRow+1;if(this._moveStartCellIndex.row===this._moveStartRange.startRow)e=s,t=s+n-1;else if(this._moveStartCellIndex.row===this._moveStartRange.endRow)t=s,e=s-n+1;else{let r=this._moveStartCellIndex.row-this._moveStartRange.startRow;e=s-r,t=s-r+n-1}let r=o.getCellWithCoordByIndex(e,c),i=o.getCellWithCoordByIndex(t,c),{startX:a,startY:l}=r,{endY:u}=i;this._targetRange={startColumn:c,endColumn:c,startRow:e,endRow:t},(d=this._pivotSelectionMoveShape)==null||d.transformByState({left:a,top:l,width:20,height:u-l})}}_controlPointerDownHandler(e){var t,r,i;(t=this._selectionControl)==null||t.setControlExtensionDisable(!1),this._context.scene.setCursor(l.CURSOR_TYPE.DEFAULT);let a=this._sheetsSelectionsService.getCurrentSelections();if(!a.length||a.length>1)return;let o=a[0].range,s=this._context.unit,c=s.getActiveSheet(),u=s.getUnitId(),d=c.getSheetId(),f=this._sheetsPivotTableAdaptorModel.getSubUnitPivotRenderCache(u,d);if(!(f!=null&&f.size))return;let p=Array.from(f).find(([e,t])=>{let{rangesInfo:r}=t;if(!r)return!1;let{rowRanges:i,colRanges:a}=r,s=i[0],c=a[0],l=!1;return s&&(l=n.Rectangle.realContain(s,o),this._moveRowOrCol=`row`),!l&&c&&(l=n.Rectangle.realContain(c,o),this._moveRowOrCol=`col`),l});if(!p)return;this._movePivotId=p[0],this._pivotSelectionMoveShape||(this._pivotSelectionMoveShape=new Zr(this._moveRowOrCol),this._context.scene.addObject(this._pivotSelectionMoveShape));let m=this._context.scene,h=this._sheetSkeletonManagerService.getCurrentSkeleton();if(!h)return;let{offsetX:g,offsetY:_}=e,v=m.getCoordRelativeToViewport(l.Vector2.FromArray([g,_])),{x:y,y:b}=v,x=m.getScrollXYInfoByViewport(v),{scaleX:S,scaleY:C}=m.getAncestorScale(),{row:w,column:T}=h.getCellIndexByOffset(y,b,S,C,x),E=(r=this._selectionControl)==null?void 0:r.model.getRange();if(this._moveStartRange=E,!E)return;let D=0;D=w<E.startRow?E.startRow:w>E.endRow?E.endRow:w;let O=0;O=T<E.startColumn?E.startColumn:T>E.endColumn?E.endColumn:T,this._moveStartCellIndex={row:D,column:O},(i=this._selectionControl)==null||i.setControlExtensionDisable(!0),this._scenePointerMoveSubscribe=m.onPointerMove$.subscribeEvent(e=>{let{offsetX:t,offsetY:n}=e,{x:r,y:i}=m.getCoordRelativeToViewport(l.Vector2.FromArray([t,n]));this._controlMoving(r,i),m.setCursor(l.CURSOR_TYPE.MOVE)}),this._scenePointerUpSubscribe=m.onPointerUp$.subscribeEvent(e=>{var t,n,r;(t=this._scenePointerMoveSubscribe)==null||t.unsubscribe(),this._scenePointerMoveSubscribe=null,(n=this._selectionControl)==null||n.setControlExtensionDisable(!1),this._selectionControl=null,m.setCursor(l.CURSOR_TYPE.DEFAULT),m.removeObject(this._pivotSelectionMoveShape),this._pivotSelectionMoveShape=null,this._updatePivotSort(),(r=this._scenePointerUpSubscribe)==null||r.unsubscribe(),this._scenePointerUpSubscribe=null})}_updatePivotSort(){if(!this._moveStartRange||!this._targetRange||!this._movePivotId)return;let n=this._context.unit.getUnitId(),r=this._context.unit.getActiveSheet().getSheetId();if(this._moveRowOrCol===`row`){var i;let o=(i=this._sheetsPivotTableAdaptorModel.getSubUnitPivotRenderCache(n,r))==null?void 0:i.get(this._movePivotId);if(!o)return;let s=this._sheetsPivotTableAdaptorModel.getSubUnitPivotMatrix(n,r);if(!s)return;let c=[],l=``,{headerMap:u,rangesInfo:d}=o;for(let t=this._moveStartRange.startColumn;t<=this._moveStartRange.endColumn;t++){var a;let n=u==null||(a=u.rowHeaderMap)==null?void 0:a[t];if(!n)continue;l=n.tableFieldId;let r=d==null?void 0:d.rowRanges,i=r==null?void 0:r[0];if(!i)continue;let o={...i,startColumn:t,endColumn:t};for(let n=o.startRow;n<=o.endRow;n++){let r=s.getValue(n,t);if(!r)continue;let{v:i,pbt:a}=r;i&&a!==e.PivotCellStyleTypeEnum.Subtotal&&a!==e.PivotCellStyleTypeEnum.GrandTotal&&(typeof i==`string`?c[n]=i:typeof i==`number`&&(c[n]=i.toString()))}break}if(!l)return;let f=this._moveStartRange.startRow,p=this._moveStartRange.endRow-this._moveStartRange.startRow+1,m=this._targetRange.startRow;if(f<m){let e=c.splice(f,p);c.splice(m-p,0,...e)}else{let e=c.splice(f,p);c.splice(m,0,...e)}let h=c.filter(e=>e!==void 0);this._commandService.executeCommand(t.SetPivotSortCommand.id,{unitId:n,subUnitId:r,pivotTableId:this._movePivotId,tableFieldId:l,info:{type:e.PivotDataFieldSortOperatorEnum.custom,customOrder:h}})}else{var o;let i=(o=this._sheetsPivotTableAdaptorModel.getSubUnitPivotRenderCache(n,r))==null?void 0:o.get(this._movePivotId);if(!i)return;let a=this._sheetsPivotTableAdaptorModel.getSubUnitPivotMatrix(n,r);if(!a)return;let c=[],l=``,{headerMap:u,rangesInfo:d}=i;for(let n=this._moveStartRange.startRow;n<=this._moveStartRange.endRow;n++){var s;let r=u==null||(s=u.colHeaderMap)==null?void 0:s[n];if(!r)continue;l=r.tableFieldId;let o=d==null?void 0:d.colRanges,f=o==null?void 0:o[0];if(!f)continue;let p={...f,startRow:n,endRow:n};for(let r=p.startColumn;r<=p.endColumn;r++){let o=a.getValue(n,r);if(!o)continue;let{v:s,pbt:l}=o;if(s&&l!==e.PivotCellStyleTypeEnum.Subtotal&&l!==e.PivotCellStyleTypeEnum.GrandTotal){if(typeof s==`string`){let e=i.projection?(0,t.mapPivotSheetColumn)(i.projection,r):r;c[e]=s}else if(typeof s==`number`){let e=i.projection?(0,t.mapPivotSheetColumn)(i.projection,r):r;c[e]=s.toString()}}}break}if(!l)return;let f=i.projection?(0,t.mapPivotSheetRange)(i.projection,this._moveStartRange):this._moveStartRange,p=i.projection?(0,t.mapPivotSheetRange)(i.projection,this._targetRange):this._targetRange,m=f.startColumn,h=this._moveStartRange.endColumn-this._moveStartRange.startColumn+1,g=p.startColumn;if(m<g){let e=c.splice(m,h);c.splice(g-h,0,...e)}else{let e=c.splice(m,h);c.splice(g,0,...e)}let _=c.filter(e=>e!==void 0);this._commandService.executeCommand(t.SetPivotSortCommand.id,{unitId:n,subUnitId:r,pivotTableId:this._movePivotId,tableFieldId:l,info:{type:e.PivotDataFieldSortOperatorEnum.custom,customOrder:_}})}}_initControl(){this.disposeWithMe(this._sheetsSelectionsService.selectionMoveEnd$.subscribe(()=>{let e=this._selectionRenderService.getSelectionControls();if(this._selectionControl=e[e.length-1],this._selectionControl&&this._selectionControl.setControlExtensionDisable(!1),!this._selectionControl)return;let{leftControl:t,rightControl:n,topControl:r,bottomControl:i}=this._selectionControl;[t,n,r,i].forEach(e=>{e.onPointerDown$.subscribeEvent(this._controlPointerDownHandler.bind(this))})}))}dispose(){var e,t;(e=this._scenePointerMoveSubscribe)==null||e.unsubscribe(),this._scenePointerMoveSubscribe=null,(t=this._scenePointerUpSubscribe)==null||t.unsubscribe(),this._scenePointerUpSubscribe=null,this._selectionControl=null,this._moveStartRange=null,this._moveStartCellIndex=null,this._targetRange=null,super.dispose()}};Qr=K([G(1,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),G(2,(0,n.Inject)(a.SheetSkeletonManagerService)),G(3,(0,n.Inject)(n.ICommandService)),G(4,a.ISheetSelectionRenderService),G(5,(0,n.Inject)(r.SheetsSelectionsService))],Qr);let $=class extends n.Plugin{constructor(e=ot,t,r,i,a){super(),this._config=e,this._injector=t,this._commandService=r,this._renderManagerService=i,this._configService=a;let{menu:o,...s}=(0,n.merge)({},ot,this._config);o&&this._configService.setConfig(`menu`,o,{merge:!0}),this._configService.setConfig(at,s),this._initRegisterCommand()}onStarting(){this._injector.add([Jr]),this._injector.get(Jr),[[Yr],[Hn],[Ln],[Q],[Vn],[zn],[Bn],[q],[X]].forEach(e=>this._injector.add(e)),this._injector.get(q)}onReady(){this._registerRenderModules(),this._injector.get(Yr)}onRendered(){this._injector.get(Ln),this._injector.get(Q),this._injector.get(Vn),this._injector.get(zn),this._injector.get(Bn),this._injector.get(X)}onSteady(){this._injector.get(Hn)}_registerRenderModules(){[[Xr],[Qr]].forEach(e=>{this.disposeWithMe(this._renderManagerService.registerRenderModule(n.UniverInstanceType.UNIVER_SHEET,e))})}_initRegisterCommand(){[it,N,tt,nt,F,z,ie,ae,oe,ue,fe,xe].forEach(e=>this._commandService.registerCommand(e))}};W($,`pluginName`,`${t.SHEET_PIVOT_TABLE_PLUGIN}_UI_PLUGIN`),W($,`packageName`,$e),W($,`version`,et),W($,`type`,n.UniverInstanceType.UNIVER_SHEET),$=K([(0,n.DependentOn)(c.UniverLicensePlugin,l.UniverRenderEnginePlugin,r.UniverSheetsPlugin,t.UniverSheetsPivotTablePlugin,a.UniverSheetsUIPlugin),G(1,(0,n.Inject)(n.Injector)),G(2,(0,n.Inject)(n.ICommandService)),G(3,l.IRenderManagerService),G(4,n.IConfigService)],$),exports.DragDropContext=xr,exports.Draggable=Cr,exports.Droppable=Sr,exports.OpenPivotTableRangeSelectorPanelOperation=N,exports.PivotFilterOptions=lr,exports.PivotFilterPanel=$n,exports.SheetsPivotTableUIMenuSchema=Qe,Object.defineProperty(exports,"UniverSheetsPivotTableMobileUIPlugin",{enumerable:!0,get:function(){return Z}}),Object.defineProperty(exports,"UniverSheetsPivotTableUIPlugin",{enumerable:!0,get:function(){return $}});
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@univerjs-pro/engine-pivot"),t=require("@univerjs-pro/sheets-pivot"),n=require("@univerjs/core"),r=require("@univerjs/sheets"),i=require("@univerjs/ui"),a=require("@univerjs/sheets-ui"),o=require("rxjs"),s=require("@univerjs/design"),c=require("@univerjs-pro/license"),l=require("@univerjs/engine-render"),u=require("@univerjs/icons"),d=require("react/jsx-runtime"),f=require("react"),p=require("@univerjs/engine-formula"),m=require("@univerjs/sheets-formula-ui"),h=require("@univerjs/icons-svg"),g=require("@univerjs/docs"),_=require("@univerjs/docs-ui");const v=`UNIVER_SHEET_PIVOT_PANEL`,y=`UNIVER_SHEET_PIVOT_PANEL_ID`,b=`UNIVER_SHEET_PIVOT_FILTER_PANEL`,x=`UNIVER_SHEET_PIVOT_FILTER_PANEL_ID`,S=`UNIVER_SHEET_PIVOT_FIELD_SETTING`,C=`UNIVER_SHEET_PIVOT_FIELD_SETTING_ID`,w=`UNIVER_SHEET_PIVOT_FIELD_NUMFMT`,T=`UNIVER_SHEET_PIVOT_FIELD_NUMFMT_ID`,E=`UNIVER_SHEET_PIVOT_SHOW_DATA_AS_DIALOG`,D=`UNIVER_SHEET_PIVOT_VALUE_FILTER_DIALOG`,O=`UNIVER_SHEET_PIVOT_EXCEL_DATE_GROUP_DIALOG`,k=`UNIVER_SHEET_PIVOT_NUMBER_GROUP_DIALOG`,A=`UNIVER_SHEET_PIVOT_MANUAL_GROUP_DIALOG`,j=`univer.sheet.pivot-table-range-selector`,M=`UNIVER_SHEET_PIVOT_MOBILE_ACTION_PANEL`,N=`UNIVER_SHEET_PIVOT_MOBILE_ACTION_PANEL_ID`,P={type:n.CommandType.OPERATION,id:`sheet.operation.open-pivot-table-range-selector-panel`,async handler(i){let a=i.get(n.IUniverInstanceService),o=i.get(n.ICommandService),s=(0,r.getSheetCommandTarget)(a),c=i.get(n.LocaleService);if(!s)return!1;let{unitId:l,subUnitId:u,worksheet:d,workbook:f}=s,p=i.get(r.SheetsSelectionsService).getCurrentLastSelection(),m=(p==null?void 0:p.range)??{startRow:0,endRow:0,startColumn:0,endColumn:0,rangeType:n.RANGE_TYPE.NORMAL},h=await F(i,l,u,(0,r.isSingleCellSelection)(p)?(0,r.expandToContinuousRange)(m,{up:!0,left:!0,right:!0,down:!0},d):m,d.getName());if(!h)return!1;let g=f.getSheets().find(e=>e.getName()===h.sourceSheetName);if(!g)return!1;let _;if(h.positionType===t.PositionType.New)_={row:0,col:0,unitId:l,subUnitId:(0,n.generateRandomId)(),sheetName:f.generateNewSheetName(`${c.t(`sheets-pivot-ui.sheet`)}`)};else{if(!h.positionRange||!h.positionSheetName)return!1;let e=f.getSheets().find(e=>e.getName()===h.positionSheetName);if(!e)return!1;_={row:h.positionRange.startRow,col:h.positionRange.startColumn,unitId:l,subUnitId:e.getSheetId(),sheetName:e.getName()}}let v=(0,e.generateHexNumber)(8);return await o.executeCommand(t.AddPivotTableCommand.id,{pivotTableId:v,pivotTableConfig:{targetCellInfo:_,sourceRangeInfo:{range:h.sourceRange,unitId:l,sheetName:h.sourceSheetName,subUnitId:g.getSheetId()}},positionType:h.positionType})?{unitId:_.unitId,subUnitId:_.subUnitId,pivotTableId:v}:!1}};async function F(e,t,r,a,o){let s=e.get(i.IDialogService),c=e.get(n.LocaleService);return new Promise(e=>{let n,i={unitId:t,subUnitId:r,sourceRange:a,positionRange:null,sourceSheetName:o,positionSheetName:o,onConfirm:t=>{e(t),n==null||n.dispose()},onCancel:()=>{e(null),n==null||n.dispose()}};n=s.open({id:j,title:{title:c.t(`sheets-pivot-ui.dialog.source.insertPivotTable`)},draggable:!0,mask:!1,maskClosable:!1,children:{label:{name:j,props:i}},width:400,onClose:()=>{n.dispose()}})})}const I={type:n.CommandType.OPERATION,id:`sheet.operation.set-pivot-date-group`,async handler(e,r){if(!r)return!1;let i=L(e);return i?e.get(n.ICommandService).executeCommand(t.SetPivotDateGroupCommand.id,{unitId:i.unitId,subUnitId:i.subUnitId,pivotTableId:i.pivotTableId,tableFieldId:i.tableFieldId,dateType:r.dateType}):!1}};function L(i,a=e.PivotDateGroupingMode.Legacy){let o=i.get(n.IUniverInstanceService),s=(0,r.getSheetCommandTarget)(o);if(!s)return;let c=i.get(r.SheetsSelectionsService).getCurrentLastSelection(),l=c==null?void 0:c.primary;if(!l)return;let{unitId:u,subUnitId:d}=s,f=l.startRow,p=l.startColumn,m=i.get(t.SheetsPivotTableAdaptorModel).getPivotTableIdByCell(u,d,f,p);if(!m)return;let h=R(i,u,d,m,f,p);if(!h)return;let g=i.get(t.SheetsPivotTableConfigModel),_=g.getPivotTableConfig(u,d,m);if((0,e.getPivotDateGroupingMode)(_==null?void 0:_.fieldsConfig.options)!==a)return;let v=g.getCollection(u,m),y=(0,t.getPivotDateGroupContext)(_==null?void 0:_.fieldsConfig,v,h);if(y)return{unitId:u,subUnitId:d,pivotTableId:m,tableFieldId:h,row:f,col:p,dateGroupContext:y}}function R(e,n,r,i,a,o){let s=e.get(t.SheetsPivotTableAdaptorModel).getPivotItemRenderInfoCache(n,r,i);if(!s)return;let c=e.get(t.SheetsPivotTableAdaptorModel).getPivotTableCellData(n,r,a,o),l=(0,t.resolvePivotSheetCell)(a,o,c,s);return l==null?void 0:l.tableFieldId}function z(n){let r=L(n,e.PivotDateGroupingMode.ExcelLike);if(!r)return;let i=n.get(t.SheetsPivotTableConfigModel),a=i.getPivotTableConfig(r.unitId,r.subUnitId,r.pivotTableId),o=a==null?void 0:a.fieldsConfig.dimension[r.tableFieldId];if(!o)return;let s=i.getCollection(r.unitId,r.pivotTableId),c=(r.dateGroupContext.area===e.PivotTableFiledAreaEnum.Row?a.fieldsConfig.rowFields:a.fieldsConfig.columnFields).flatMap(t=>{let n=a.fieldsConfig.dimension[t],i=n?s==null?void 0:s.fields[n.dataFieldId]:void 0;return i&&(0,e.isDateGroupFieldJSON)(i)&&i.originalFieldId===r.dateGroupContext.originalDataFieldId&&i.excelLike?[i.excelLike]:[]}),l=c[0],u=l?{units:c.map(e=>e.unit),autoStart:l.autoStart,autoEnd:l.autoEnd,...l.start===void 0?{}:{start:l.start},...l.end===void 0?{}:{end:l.end},...l.daysInterval===void 0?{}:{daysInterval:l.daysInterval}}:void 0;return{...r,targetFieldName:o.displayName,grouping:u}}const B={id:`sheet.operation.set-pivot-excel-date-group`,type:n.CommandType.OPERATION,async handler(e){let r=z(e);if(!r)return!1;let a=e.get(t.SheetsPivotTableConfigModel),o=a.getPivotTableConfig(r.unitId,r.subUnitId,r.pivotTableId),s=a.getCollection(r.unitId,r.pivotTableId),c=e.get(i.IDialogService),l=e.get(n.LocaleService),u=e.get(n.IUniverInstanceService).getUnit(r.unitId),d=(u==null?void 0:u.getDateSystem())??n.DateSystem.Date1900,f=null,p=!1,m=!1,h=()=>{p||(p=!0,f==null||f.dispose(),f=null)},g=async i=>{if(!(m||p)){m=!0;try{let c=z(e);if(!c||c.unitId!==r.unitId||c.subUnitId!==r.subUnitId||c.pivotTableId!==r.pivotTableId||c.tableFieldId!==r.tableFieldId||a.getPivotTableConfig(r.unitId,r.subUnitId,r.pivotTableId)!==o||a.getCollection(r.unitId,r.pivotTableId)!==s)return;await e.get(n.ICommandService).executeCommand(t.SetPivotExcelDateGroupCommand.id,{unitId:r.unitId,subUnitId:r.subUnitId,pivotTableId:r.pivotTableId,tableFieldId:r.tableFieldId,grouping:{...i,units:i.units.concat()}})}catch{}finally{h()}}};try{f=c.open({id:`UNIVER_SHEET_PIVOT_EXCEL_DATE_GROUP_DIALOG_ID`,title:{title:l.t(`sheets-pivot-ui.dateGroup.excelLikeTitle`)},draggable:!0,mask:!0,maskClosable:!1,width:440,children:{label:{name:O,props:{targetFieldName:r.targetFieldName,dateSystem:d,grouping:r.grouping,onConfirm:g,onCancel:h}}},onClose:h})}catch{return h(),!1}return!0}};function V(e){return e.map(e=>({...e,members:e.members.map(e=>({...e}))}))}function ee(t){let r;try{r=JSON.parse(t)}catch{return}if(!(!Array.isArray(r)||r.length<1||typeof r[0]!=`string`))return r[0]===n.RecordValueType.Blank?e.GlobalConfig.BLANK_PLACEHOLDER:String(r[1])}function te(i,a){var o;let s=()=>({error:`invalidSelection`}),c=(0,r.getSheetCommandTarget)(i.get(n.IUniverInstanceService)),l=i.get(r.SheetsSelectionsService).getCurrentSelections();if(!c||!l.length)return s();let{unitId:u,subUnitId:d}=c,f=l[0].primary;if(!f)return s();let p=i.get(t.SheetsPivotTableAdaptorModel),m=p.getPivotTableIdByCell(u,d,f.startRow,f.startColumn),h=i.get(t.SheetsPivotTableConfigModel),g=m&&h.getPivotTableConfig(u,d,m),_=m&&h.getCollection(u,m),v=m&&p.getPivotItemRenderInfoCache(u,d,m);if(!m||!g||!_||!v)return s();let y=(0,t.resolvePivotSheetCell)(f.startRow,f.startColumn,p.getPivotTableCellData(u,d,f.startRow,f.startColumn),v),b=y==null?void 0:y.tableFieldId,x=b&&g.fieldsConfig.dimension[b];if(!b||!x||!(g.fieldsConfig.rowFields.includes(b)||g.fieldsConfig.columnFields.includes(b)))return s();let S=_.fields[x.dataFieldId];if(!S)return s();let C=S.dataFieldType===e.PivotDataFieldTypeEnum.textGroupField?S:void 0;if(!C&&S.dataFieldType!==e.PivotDataFieldTypeEnum.defaultNormalField)return s();let w=(C==null?void 0:C.originalFieldId)??S.id;if(((o=_.fields[w])==null?void 0:o.dataFieldType)!==e.PivotDataFieldTypeEnum.defaultNormalField)return s();let T=V((C==null?void 0:C.grouping.groups)??[]),E=/* @__PURE__ */ new Map,D=/* @__PURE__ */ new Set,O=0;for(let n of l){let{startRow:r,endRow:i,startColumn:a,endColumn:o}=n.range;if(O+=(i-r+1)*(o-a+1),O>512)return s();for(let n=r;n<=i;n++)for(let r=a;r<=o;r++){if(p.getPivotTableIdByCell(u,d,n,r)!==m)return s();let i=(0,t.resolvePivotSheetCell)(n,r,p.getPivotTableCellData(u,d,n,r),v),a=i==null?void 0:i.info;if(!i||i.area!==`rowRanges`&&i.area!==`colRanges`||i.tableFieldId!==b||!a||a.isValue||a.isSubtotal||a.isBottomTotal||a.isTopTotal||a.rowType&&a.rowType!==`item`||a.level<1)return s();let o=a.paths[a.level-1];if(o===void 0)return s();if(C){let e;try{e=JSON.parse(o)}catch{return s()}if(!Array.isArray(e)||e.length!==2||typeof e[1]!=`string`)return s();if(e[0]===`group`){if(!T.some(t=>t.id===e[1]))return s();D.add(e[1]);continue}if(e[0]!==`member`)return s();let t=ee(e[1]);if(t===void 0)return s();E.set(e[1],{path:t,memberKey:e[1]})}else{var k;let t=(k=p.getPivotTableCellData(u,d,n,r))==null?void 0:k.v,i=t==null||t===``?e.GlobalConfig.BLANK_PLACEHOLDER:String(t);E.set(o,{path:i,tupleKey:o})}}}return a===`group`&&(E.size<2||D.size>0)||a!==`group`&&(E.size>0||D.size!==1)?s():{context:{unitId:u,subUnitId:d,pivotTableId:m,tableFieldId:b,sourceFieldId:w,targetFieldName:x.displayName,groups:T,memberPaths:Array.from(E.values()),selectedGroupId:Array.from(D)[0]}}}function H(e,t){return te(e,t).context}function U(e,t){e.get(i.IMessageService).show({type:s.MessageType.Error,content:e.get(n.LocaleService).t(`sheets-pivot-ui.manualGroup.${t}`)})}function ne(e,t,n){let r=H(e,t);return!!r&&r.unitId===n.unitId&&r.subUnitId===n.subUnitId&&r.pivotTableId===n.pivotTableId&&r.tableFieldId===n.tableFieldId&&r.sourceFieldId===n.sourceFieldId&&r.selectedGroupId===n.selectedGroupId&&JSON.stringify(r.memberPaths)===JSON.stringify(n.memberPaths)&&JSON.stringify(r.groups)===JSON.stringify(n.groups)}async function re(e,r){let a=te(e,r),o=a.context;if(!o)return U(e,a.error),!1;let s=e.get(t.SheetsPivotTableConfigModel),c=s.getPivotTableConfig(o.unitId,o.subUnitId,o.pivotTableId),l=s.getCollection(o.unitId,o.pivotTableId),u=[];if(r===`group`){try{let n=(await e.get(t.SheetsPivotRPCService).getPivotDisplayConfig({unitId:o.unitId,subUnitId:o.subUnitId,pivotTableId:o.pivotTableId,params:{type:t.PivotDisplayDataType.ManualGroupMembers,manualGroupMembers:{sourceFieldId:o.sourceFieldId,paths:o.memberPaths}}})).result.manualGroupMembers;if(!n||n.error||n.members.length<2)return U(e,(n==null?void 0:n.error)??`invalidSelection`),!1;u=n.members}catch{return U(e,`invalidSelection`),!1}if(!ne(e,r,o)||s.getPivotTableConfig(o.unitId,o.subUnitId,o.pivotTableId)!==c||s.getCollection(o.unitId,o.pivotTableId)!==l)return!1}let d=e.get(i.IDialogService),f=e.get(n.LocaleService),p=o.groups.find(e=>e.id===o.selectedGroupId),m=(p==null?void 0:p.name)??f.t(`sheets-pivot-ui.manualGroup.defaultName`,String(o.groups.length+1));if(!p){let e=o.groups.length+1;for(;o.groups.some(e=>e.name===m);)e++,m=f.t(`sheets-pivot-ui.manualGroup.defaultName`,String(e))}let h=null,g=!1,_=!1,v=()=>{g||(g=!0,h==null||h.dispose(),h=null)},y=async i=>{if(_||g)return;let a=i.trim();if(!a||o.groups.some(e=>e.name===a&&e.id!==o.selectedGroupId))U(e,`invalidSelection`);else{_=!0;try{if(!ne(e,r,o)||s.getPivotTableConfig(o.unitId,o.subUnitId,o.pivotTableId)!==c||s.getCollection(o.unitId,o.pivotTableId)!==l)return;let i=r===`group`?o.groups.concat({id:(0,n.generateRandomId)(),name:a,members:u.map(e=>({...e}))}):o.groups.map(e=>e.id===o.selectedGroupId?{...e,name:a}:e);await e.get(n.ICommandService).executeCommand(t.SetPivotManualGroupsCommand.id,{unitId:o.unitId,subUnitId:o.subUnitId,pivotTableId:o.pivotTableId,sourceFieldId:o.sourceFieldId,groups:i})}catch{}finally{v()}}};try{h=d.open({id:`UNIVER_SHEET_PIVOT_MANUAL_GROUP_DIALOG_ID`,title:{title:f.t(r===`group`?`sheets-pivot-ui.manualGroup.groupTitle`:`sheets-pivot-ui.manualGroup.renameTitle`)},draggable:!0,mask:!0,maskClosable:!1,width:400,children:{label:{name:A,props:{name:m,onConfirm:y,onCancel:v}}},onClose:v})}catch{return v(),!1}return!0}const ie={id:`sheet.operation.set-pivot-manual-group`,type:n.CommandType.OPERATION,handler:e=>re(e,`group`)},ae={id:`sheet.operation.rename-pivot-manual-group`,type:n.CommandType.OPERATION,handler:e=>re(e,`rename`)},oe={id:`sheet.operation.ungroup-pivot-manual-group`,type:n.CommandType.OPERATION,async handler(e){let r=te(e,`ungroup`),i=r.context;if(!i)return U(e,r.error),!1;let a=e.get(t.SheetsPivotTableConfigModel),o=a.getPivotTableConfig(i.unitId,i.subUnitId,i.pivotTableId),s=a.getCollection(i.unitId,i.pivotTableId);return!ne(e,`ungroup`,i)||a.getPivotTableConfig(i.unitId,i.subUnitId,i.pivotTableId)!==o||a.getCollection(i.unitId,i.pivotTableId)!==s?!1:e.get(n.ICommandService).executeCommand(t.SetPivotManualGroupsCommand.id,{unitId:i.unitId,subUnitId:i.subUnitId,pivotTableId:i.pivotTableId,sourceFieldId:i.sourceFieldId,groups:i.groups.filter(e=>e.id!==i.selectedGroupId)})}};function se(e){return typeof e.autoStart==`boolean`&&typeof e.autoEnd==`boolean`&&Number.isFinite(e.interval)&&e.interval>0&&(e.start===void 0||Number.isFinite(e.start))&&(e.end===void 0||Number.isFinite(e.end))&&(e.autoStart||e.start!==void 0)&&(e.autoEnd||e.end!==void 0)&&(e.start===void 0||e.end===void 0||e.start<=e.end)}function ce(t,n){if(t.dataFieldType===e.PivotDataFieldTypeEnum.numberGroupField){let r=t,i=n.fields[r.originalFieldId];return!i||i.dataFieldType!==e.PivotDataFieldTypeEnum.defaultNormalField||i.fieldDataType!==e.PivotDataFieldDataTypeEnum.number?void 0:{sourceFieldId:r.originalFieldId,grouping:{...r.grouping}}}if(t.dataFieldType===e.PivotDataFieldTypeEnum.defaultNormalField&&t.fieldDataType===e.PivotDataFieldDataTypeEnum.number)return{sourceFieldId:t.id}}function le(e){var i;let a=(0,r.getSheetCommandTarget)(e.get(n.IUniverInstanceService)),o=(i=e.get(r.SheetsSelectionsService).getCurrentLastSelection())==null?void 0:i.primary;if(!a||!o)return;let{unitId:s,subUnitId:c}=a,l=o.startRow,u=o.startColumn,d=e.get(t.SheetsPivotTableAdaptorModel),f=d.getPivotTableIdByCell(s,c,l,u);if(!f)return;let p=d.getPivotItemRenderInfoCache(s,c,f);if(!p)return;let m=(0,t.resolvePivotSheetCell)(l,u,d.getPivotTableCellData(s,c,l,u),p),h=m==null?void 0:m.tableFieldId;if(!h||m.area!==`rowRanges`&&m.area!==`colRanges`)return;let g=e.get(t.SheetsPivotTableConfigModel),_=g.getPivotTableConfig(s,c,f),v=g.getCollection(s,f),y=_==null?void 0:_.fieldsConfig.dimension[h];if(!y||!v||!(_.fieldsConfig.rowFields.includes(h)||_.fieldsConfig.columnFields.includes(h)))return;let b=v.fields[y.dataFieldId];if(!b)return;let x=ce(b,v);if(x)return{unitId:s,subUnitId:c,pivotTableId:f,tableFieldId:h,targetFieldName:y.displayName,...x}}const ue={id:`sheet.operation.set-pivot-number-group`,type:n.CommandType.OPERATION,async handler(e){let r=le(e);if(!r)return!1;let a=e.get(t.SheetsPivotTableConfigModel),s=a.getCollection(r.unitId,r.pivotTableId),c=a.getPivotTableConfig(r.unitId,r.subUnitId,r.pivotTableId),l=e.get(n.IUniverInstanceService),u=()=>{let t=le(e);return!!t&&t.unitId===r.unitId&&t.subUnitId===r.subUnitId&&t.pivotTableId===r.pivotTableId&&t.tableFieldId===r.tableFieldId&&t.sourceFieldId===r.sourceFieldId&&JSON.stringify(t.grouping)===JSON.stringify(r.grouping)},d=e.get(i.IDialogService),f=e.get(n.LocaleService),p=null,m=!1,h=!1,g=new o.Subject,_=async()=>{if(!m&&u())try{let n=(await e.get(t.SheetsPivotRPCService).getPivotDisplayConfig({unitId:r.unitId,subUnitId:r.subUnitId,pivotTableId:r.pivotTableId,params:{type:t.PivotDisplayDataType.NumberGroupDefaults,numberGroupDefaults:{tableFieldId:r.tableFieldId}}})).result.numberGroupDefaults;return!m&&u()&&n&&se(n)?n:void 0}catch{return}},v=()=>{m||(m=!0,g.next(),g.complete(),p==null||p.dispose(),p=null)},y=await _();if(!y)return v(),!1;let b=l.unitDisposed$.pipe((0,o.filter)(e=>e.getUnitId()===r.unitId),(0,o.tap)(v)),x=(0,o.defer)(()=>{if(m||!l.getUnit(r.unitId))return v(),o.EMPTY;let e=a.watchPivotTableChanges(r.unitId,r.subUnitId,r.pivotTableId).pipe((0,o.debounceTime)(0),(0,o.map)(()=>void 0));return(0,o.merge)((0,o.of)(y),e).pipe((0,o.switchMap)(e=>e&&a.getCollection(r.unitId,r.pivotTableId)===s&&a.getPivotTableConfig(r.unitId,r.subUnitId,r.pivotTableId)===c?(0,o.of)(e):(0,o.concat)((0,o.of)(void 0),_())),(0,o.takeUntil)((0,o.merge)(g,b)))}),S=async i=>{if(!(h||m)){h=!0;try{if(!se(i)||!u())return;await e.get(n.ICommandService).executeCommand(t.SetPivotNumberGroupCommand.id,{unitId:r.unitId,subUnitId:r.subUnitId,pivotTableId:r.pivotTableId,tableFieldId:r.tableFieldId,grouping:{...i}})}catch{}finally{v()}}};try{p=d.open({id:`UNIVER_SHEET_PIVOT_NUMBER_GROUP_DIALOG_ID`,title:{title:f.t(`sheets-pivot-ui.numberGroup.title`)},draggable:!0,mask:!0,maskClosable:!1,width:400,children:{label:{name:k,props:{targetFieldName:r.targetFieldName,grouping:r.grouping,defaults$:x,onConfirm:S,onCancel:v}}},onClose:v})}catch{return v(),!1}return!0}},de=[{type:e.PivotShowAsTypeEnum.normal,localeKey:`sheets-pivot-ui.showValuesAs.normal`,requiresParameters:!1,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.percentOfGrandTotal,localeKey:`sheets-pivot-ui.showValuesAs.percentOfGrandTotal`,requiresParameters:!1,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.percentOfRowTotal,localeKey:`sheets-pivot-ui.showValuesAs.percentOfRowTotal`,requiresParameters:!1,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.percentOfColumnTotal,localeKey:`sheets-pivot-ui.showValuesAs.percentOfColumnTotal`,requiresParameters:!1,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.percentOfParentRowTotal,localeKey:`sheets-pivot-ui.showValuesAs.percentOfParentRowTotal`,requiresParameters:!1,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.percentOfParentColumnTotal,localeKey:`sheets-pivot-ui.showValuesAs.percentOfParentColumnTotal`,requiresParameters:!1,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.index,localeKey:`sheets-pivot-ui.showValuesAs.index`,requiresParameters:!1,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.percentOfParent,localeKey:`sheets-pivot-ui.showValuesAs.percentOfParent`,requiresParameters:!0,baseItemMode:`none`,parentFieldOnly:!0},{type:e.PivotShowAsTypeEnum.percentOf,localeKey:`sheets-pivot-ui.showValuesAs.percentOf`,requiresParameters:!0,baseItemMode:`item`},{type:e.PivotShowAsTypeEnum.differenceFrom,localeKey:`sheets-pivot-ui.showValuesAs.differenceFrom`,requiresParameters:!0,baseItemMode:`item-or-relative`},{type:e.PivotShowAsTypeEnum.percentDifferenceFrom,localeKey:`sheets-pivot-ui.showValuesAs.percentDifferenceFrom`,requiresParameters:!0,baseItemMode:`item-or-relative`},{type:e.PivotShowAsTypeEnum.runningTotal,localeKey:`sheets-pivot-ui.showValuesAs.runningTotal`,requiresParameters:!0,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.percentRunningTotal,localeKey:`sheets-pivot-ui.showValuesAs.percentRunningTotal`,requiresParameters:!0,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.rankAscending,localeKey:`sheets-pivot-ui.showValuesAs.rankAscending`,requiresParameters:!0,baseItemMode:`none`},{type:e.PivotShowAsTypeEnum.rankDescending,localeKey:`sheets-pivot-ui.showValuesAs.rankDescending`,requiresParameters:!0,baseItemMode:`none`}],fe={type:n.CommandType.OPERATION,id:`sheet.operation.set-pivot-show-data-as`,async handler(e,r){let a=r&&de.find(e=>e.type===r.type);if(!a)return!1;let o=me(e);if(!o)return!1;if(!a.requiresParameters)return e.get(n.ICommandService).executeCommand(t.UpdatePivotFieldSettingCommand.id,{unitId:o.unitId,subUnitId:o.subUnitId,pivotTableId:o.pivotTableId,tableFieldId:o.tableFieldId,showDataAs:{type:a.type,baseFieldId:``,baseItem:``},tableFieldInfo:o.tableFieldInfo,dataFieldInfo:o.dataFieldInfo});let s=he(e,o);if(!s)return!1;let c=pe(s.config,s.collection,a);if(c.length===0)return!1;let l={};if(a.baseItemMode!==`none`){try{l=await ge(e,s.config,o.pivotTableId,c)}catch{return!1}if(!c.some(e=>l[e]&&(a.baseItemMode===`item-or-relative`||l[e].items.length>0)))return!1}let u=e.get(i.IDialogService),d=e.get(n.LocaleService),f=null,p=!1,m=!1,h=()=>{p||(p=!0,f==null||f.dispose(),f=null)};try{f=u.open({id:`UNIVER_SHEET_PIVOT_SHOW_DATA_AS_DIALOG_ID`,title:{title:d.t(`sheets-pivot-ui.showValuesAs.dialogTitle`)},draggable:!0,mask:!0,maskClosable:!1,width:400,children:{label:{name:E,props:{unitId:o.unitId,subUnitId:o.subUnitId,pivotTableId:o.pivotTableId,tableFieldId:o.tableFieldId,type:a.type,baseItems:l,onCancel:h,onConfirm:async r=>{if(!m){m=!0;try{let s=he(e,o);if(!s||!_e(s.config,s.collection,a,r))return;if(a.baseItemMode!==`none`&&r.baseItemType===void 0){var i;if(!((i=(await ge(e,s.config,s.context.pivotTableId,[r.baseFieldId]))[r.baseFieldId])!=null&&i.items.includes(r.baseItem)))return}await e.get(n.ICommandService).executeCommand(t.UpdatePivotFieldSettingCommand.id,{unitId:s.context.unitId,subUnitId:s.context.subUnitId,pivotTableId:s.context.pivotTableId,tableFieldId:s.context.tableFieldId,showDataAs:r,tableFieldInfo:s.context.tableFieldInfo,dataFieldInfo:s.context.dataFieldInfo})}catch{}finally{h()}}}}}},onClose:h})}catch{return h(),!1}return!0}};function pe(e,t,n){let{rowFields:r,columnFields:i,dimension:a}=e.fieldsConfig;return(n.parentFieldOnly?[...r.slice(0,-1),...i.slice(0,-1)]:[...r,...i]).filter(e=>{let n=a[e];return!!(n&&(t.fields[n.dataFieldId]||t.customFields.find(e=>e.id===n.dataFieldId)))})}function me(e){var i,a;let o=(0,r.getSheetCommandTarget)(e.get(n.IUniverInstanceService)),s=(i=e.get(r.SheetsSelectionsService).getCurrentLastSelection())==null?void 0:i.primary;if(!o||!s)return;let{unitId:c,subUnitId:l}=o,u=s.startRow,d=s.startColumn,f=e.get(t.SheetsPivotTableAdaptorModel),p=f.getPivotTableIdByCell(c,l,u,d);if(!p)return;let m=e.get(t.SheetsPivotTableConfigModel).getPivotTableConfig(c,l,p),h=f.getPivotItemRenderInfoCache(c,l,p),g=f.getPivotTableCellData(c,l,u,d);if(!m||!h)return;let _=(0,t.resolvePivotSheetCell)(u,d,g,h);if(!_)return;let v=(0,t.resolvePivotValueFieldId)(_,m.fieldsConfig.valueFields);if(v)return(a=he(e,{unitId:c,subUnitId:l,pivotTableId:p,tableFieldId:v}))==null?void 0:a.context}function he(e,n){let{unitId:r,subUnitId:i,pivotTableId:a,tableFieldId:o}=n,s=e.get(t.SheetsPivotTableConfigModel),c=s.getPivotTableConfig(r,i,a),l=s.getCollection(r,a),u=c==null?void 0:c.fieldsConfig.measure[o];if(!c||!l||!u||!c.fieldsConfig.valueFields.includes(u.id))return;let d=l.fields[u.dataFieldId]??l.customFields.find(e=>e.id===u.dataFieldId);if(!d)return;let f={...u.showDataAs};return{config:c,collection:l,context:{unitId:r,subUnitId:i,pivotTableId:a,tableFieldId:u.id,currentShowDataAs:f,tableFieldInfo:{tableFieldId:u.id,displayName:u.displayName,dataFieldId:u.dataFieldId,format:u.format,subTotalType:u.subtotal,showDataAs:{...f}},dataFieldInfo:{id:d.id,dataFieldId:d.id,name:d.name,type:d.fieldDataType,checked:!0,pivotTableId:a}}}}async function ge(e,n,r,i){let{unitId:a,subUnitId:o}=n.targetCellInfo,s=n.targetCellInfo,c=e.get(t.SheetsPivotTableAdaptorModel).getPivotItemRenderInfoCache(a,o,r);if(!c)throw Error(`[sheets-pivot-ui]: Pivot render cache is unavailable`);return(await Promise.all(i.map(async n=>{try{return[n,(await e.get(t.SheetsPivotRPCService).getPivotDisplayConfig({unitId:a,subUnitId:o,pivotTableId:r,params:{type:t.PivotDisplayDataType.Filter,filter:{row:s.row,col:s.col,tableFieldId:n,pivotItemRenderInfoCache:c}}})).result.filter]}catch{return[n,void 0]}}))).reduce((e,[t,n])=>(n&&(e[t]={items:n.items,itemTypes:n.itemTypes,...n.itemLabels?{itemLabels:n.itemLabels}:{},format:n.format}),e),{})}function _e(t,n,r,i){return i.type!==r.type||!pe(t,n,r).includes(i.baseFieldId)?!1:r.baseItemMode===`none`?i.baseItem===``&&i.baseItemType===void 0:i.baseItemType===void 0?i.baseItem!==``:r.baseItemMode===`item-or-relative`&&i.baseItem===``&&(i.baseItemType===e.PivotShowAsBaseItemTypeEnum.previous||i.baseItemType===e.PivotShowAsBaseItemTypeEnum.next)}function ve(e){return{...e,expected:Array.isArray(e.expected)?e.expected.concat():e.expected}}function ye(e,n){let r=e.get(t.SheetsPivotTableConfigModel).getPivotTableConfig(n.unitId,n.subUnitId,n.pivotTableId);if(r&&r.fieldsConfig.dimension[n.targetFieldId]&&(r.fieldsConfig.rowFields.includes(n.targetFieldId)||r.fieldsConfig.columnFields.includes(n.targetFieldId)))return r}function be(e){var i,a;let o=(0,r.getSheetCommandTarget)(e.get(n.IUniverInstanceService)),s=(i=e.get(r.SheetsSelectionsService).getCurrentLastSelection())==null?void 0:i.primary;if(!o||!s)return;let{unitId:c,subUnitId:l}=o,u=s.startRow,d=s.startColumn,f=e.get(t.SheetsPivotTableAdaptorModel),p=f.getPivotTableIdByCell(c,l,u,d);if(!p)return;let m=e.get(t.SheetsPivotTableConfigModel).getPivotTableConfig(c,l,p),h=f.getPivotItemRenderInfoCache(c,l,p);if(!m||!h)return;let g=(0,t.resolvePivotSheetCell)(u,d,f.getPivotTableCellData(c,l,u,d),h),_=g&&(0,t.resolvePivotValueFilterTarget)(g,m.fieldsConfig);if(!_)return;let v=m.fieldsConfig.dimension[_.targetFieldId],y=m.fieldsConfig.valueFields.flatMap(e=>{let t=m.fieldsConfig.measure[e];return t?[{id:t.id,label:t.displayName}]:[]});if(!v||y.length===0)return;let b=(a=m.fieldsConfig.valueFilter)==null||(a=a.find(e=>e.fieldId===_.targetFieldId))==null?void 0:a.filterInfo;return{unitId:c,subUnitId:l,pivotTableId:p,targetFieldId:_.targetFieldId,targetFieldName:v.displayName,valueFields:y,defaultValueFieldId:_.defaultValueFieldId,...b?{currentFilter:ve(b)}:{}}}const xe={type:n.CommandType.OPERATION,id:`sheet.operation.set-pivot-value-filter`,async handler(r){let a=be(r);if(!a)return!1;let o=r.get(i.IDialogService),s=r.get(n.LocaleService),c=null,l=!1,u=!1,d=()=>{l||(l=!0,c==null||c.dispose(),c=null)},f=async i=>{if(!u){u=!0;try{let o=ye(r,a);if(!o||i&&(!o.fieldsConfig.measure[i.valueFieldId]||!o.fieldsConfig.valueFields.includes(i.valueFieldId)||!(0,e.isValueFilterOperator)(i.operator)))return;await r.get(n.ICommandService).executeCommand(t.SetPivotValueFilterCommand.id,{unitId:a.unitId,subUnitId:a.subUnitId,pivotTableId:a.pivotTableId,fieldId:a.targetFieldId,valueFilterInfo:i?{...i,type:e.PivotFilterTypeEnum.ValueFilter,expected:Array.isArray(i.expected)?i.expected.concat():i.expected}:void 0})}catch{}finally{d()}}};try{c=o.open({id:`UNIVER_SHEET_PIVOT_VALUE_FILTER_DIALOG_ID`,title:{title:s.t(`sheets-pivot-ui.valueFilter.dialogTitle`)},draggable:!0,mask:!0,maskClosable:!1,width:420,children:{label:{name:D,props:{targetFieldId:a.targetFieldId,targetFieldName:a.targetFieldName,valueFields:a.valueFields,defaultValueFieldId:a.defaultValueFieldId,currentFilter:a.currentFilter,onConfirm:e=>f(e),onClear:()=>f(void 0),onCancel:d}}},onClose:d})}catch{return d(),!1}return!0}},Se=`sheet.pivot.context-menu.date-group`,Ce=`sheet.pivot.context-menu.excel-date-group`,we=`sheet.pivot.context-menu.number-group`,Te=`sheet.pivot.context-menu.manual-group`,Ee=`sheet.pivot.context-menu.manual-rename`,De=`sheet.pivot.context-menu.manual-ungroup`,Oe=`sheet.pivot.context-menu.show-data-as`,ke=`sheet.pivot.context-menu.value-filter`,Ae={year:`sheets-pivot-ui.dateGroup.year`,"year-quarter":`sheets-pivot-ui.dateGroup.yearQuarter`,quarter:`sheets-pivot-ui.dateGroup.quarter`,"year-month":`sheets-pivot-ui.dateGroup.yearMonth`,month:`sheets-pivot-ui.dateGroup.month`,"month-date":`sheets-pivot-ui.dateGroup.monthDate`,"year-month-date":`sheets-pivot-ui.dateGroup.yearMonthDate`,date:`sheets-pivot-ui.dateGroup.date`,hour:`sheets-pivot-ui.dateGroup.hour`,minute:`sheets-pivot-ui.dateGroup.minute`,"hour-minute":`sheets-pivot-ui.dateGroup.hourMinute`};function je(e){return{id:P.id,type:i.MenuItemType.BUTTON,icon:`PivotTableIcon`,hidden$:(0,i.getMenuHiddenObservable)(e,n.UniverInstanceType.UNIVER_SHEET),disabled$:(0,a.getObservableWithExclusiveRange$)(e,(0,a.getCurrentRangeDisable$)(e,{workbookTypes:[r.WorkbookEditablePermission],worksheetTypes:[r.WorksheetViewPermission,r.WorksheetPivotTablePermission],rangeTypes:[r.RangeProtectionPermissionViewPoint]})),tooltip:`sheets-pivot-ui.title`,title:`sheets-pivot-ui.title`}}function Me(e){return{id:Se,type:i.MenuItemType.SUBITEMS,title:`sheets-pivot-ui.dateGroup.title`,hidden$:Be(e)}}function Ne(e){return{id:Ce,commandId:B.id,type:i.MenuItemType.BUTTON,title:`sheets-pivot-ui.dateGroup.excelLikeTitle`,hidden$:W(e).pipe((0,o.map)(()=>!z(e)))}}function Pe(e){return{id:we,commandId:ue.id,type:i.MenuItemType.BUTTON,title:`sheets-pivot-ui.numberGroup.title`,hidden$:W(e).pipe((0,o.map)(()=>!le(e)))}}function Fe(e){return{id:Te,commandId:ie.id,type:i.MenuItemType.BUTTON,title:`sheets-pivot-ui.manualGroup.groupTitle`,hidden$:W(e).pipe((0,o.map)(()=>!H(e,`group`)))}}function Ie(e){return{id:Ee,commandId:ae.id,type:i.MenuItemType.BUTTON,title:`sheets-pivot-ui.manualGroup.renameTitle`,hidden$:W(e).pipe((0,o.map)(()=>!H(e,`rename`)))}}function Le(e){return{id:De,commandId:oe.id,type:i.MenuItemType.BUTTON,title:`sheets-pivot-ui.manualGroup.ungroupTitle`,hidden$:W(e).pipe((0,o.map)(()=>!H(e,`ungroup`)))}}function Re(e){return t=>({id:ze(e),commandId:I.id,type:i.MenuItemType.BUTTON,title:Ae[e],params:{dateType:e},disabled$:Ve(t,e),activated$:He(t,e)})}function ze(e){return`sheet.pivot.context-menu.date-group.${e}`}function Be(e){return W(e).pipe((0,o.map)(()=>!L(e)))}function Ve(e,n){return W(e).pipe((0,o.map)(()=>{let r=L(e);return!r||(0,t.isPivotDateGroupTypeDisabled)(r.dateGroupContext,n)}))}function He(e,t){return W(e).pipe((0,o.map)(()=>{let n=L(e);return(n==null?void 0:n.dateGroupContext.currentDateType)===t}))}function W(e){let i=e.get(t.SheetsPivotTableConfigModel),a=()=>{var i;let a=(0,r.getSheetCommandTarget)(e.get(n.IUniverInstanceService)),o=(i=e.get(r.SheetsSelectionsService).getCurrentLastSelection())==null?void 0:i.primary;if(!a||!o)return;let{unitId:s,subUnitId:c}=a,l=e.get(t.SheetsPivotTableAdaptorModel).getPivotTableIdByCell(s,c,o.startRow,o.startColumn);return l?{unitId:s,subUnitId:c,pivotTableId:l}:void 0};return(0,o.merge)(e.get(r.SheetsSelectionsService).selectionMoveEnd$,i.pivotConfigChange$.pipe((0,o.filter)(e=>{let t=a();return(t==null?void 0:t.unitId)===e.unitId&&t.subUnitId===e.subUnitId&&t.pivotTableId===e.pivotTableId})),i.collectionChange$.pipe((0,o.filter)(e=>{let t=a();if(!t||!e.pivotTableIds.includes(t.pivotTableId))return!1;let n=i.getPivotTableConfig(t.unitId,t.subUnitId,t.pivotTableId);return e.unitId===((n==null?void 0:n.sourceRangeInfo.unitId)??t.unitId)}))).pipe((0,o.startWith)(null))}const Ue=t.PIVOT_DATE_GROUP_TYPES.map(e=>({dateType:e,id:ze(e),menuItemFactory:Re(e)}));function We(e){return{id:Oe,type:i.MenuItemType.SUBITEMS,title:`sheets-pivot-ui.showValuesAs.title`,hidden$:qe(e).pipe((0,o.map)(()=>!me(e)))}}function Ge(t){return n=>({id:Ke(t.type),commandId:fe.id,type:i.MenuItemType.BUTTON,title:t.localeKey,params:{type:t.type},activated$:qe(n).pipe((0,o.map)(()=>{let r=me(n);return!!(r&&(r.currentShowDataAs.type??e.PivotShowAsTypeEnum.normal)===t.type)}))})}function Ke(e){return`sheet.pivot.context-menu.show-data-as.${e}`}function qe(e){return e.get(r.SheetsSelectionsService).selectionMoveEnd$.pipe((0,o.startWith)(null))}const Je=de.map(e=>({rule:e,id:Ke(e.type),menuItemFactory:Ge(e)}));function Ye(e){return{id:ke,commandId:xe.id,type:i.MenuItemType.BUTTON,title:`sheets-pivot-ui.valueFilter.title`,hidden$:e.get(r.SheetsSelectionsService).selectionMoveEnd$.pipe((0,o.startWith)(null),(0,o.map)(()=>!be(e)))}}const Xe=Ue.reduce((e,t,n)=>(e[t.id]={order:n+1,menuItemFactory:t.menuItemFactory},e),{}),Ze=Je.reduce((e,t,n)=>(e[t.id]={order:n+1,menuItemFactory:t.menuItemFactory},e),{}),Qe={[i.RibbonDataGroup.ANALYSIS]:{[P.id]:{order:0,gridLayout:{row:1,column:1,showLabel:!0},menuItemFactory:je}},[i.ContextMenuPosition.MAIN_AREA]:{[i.ContextMenuGroup.DATA]:{[P.id]:{order:1,menuItemFactory:je},[Se]:{order:2,menuItemFactory:Me,...Xe},[Ce]:{order:3,menuItemFactory:Ne},[we]:{order:4,menuItemFactory:Pe},[Te]:{order:5,menuItemFactory:Fe},[Ee]:{order:6,menuItemFactory:Ie},[De]:{order:7,menuItemFactory:Le},[Oe]:{order:8,menuItemFactory:We,...Ze},[ke]:{order:9,menuItemFactory:Ye}}},[i.ContextMenuPosition.COL_HEADER]:{[i.ContextMenuGroup.DATA]:{[P.id]:{order:1,menuItemFactory:je},[Se]:{order:2,menuItemFactory:Me,...Xe},[Ce]:{order:3,menuItemFactory:Ne},[we]:{order:4,menuItemFactory:Pe},[Te]:{order:5,menuItemFactory:Fe},[Ee]:{order:6,menuItemFactory:Ie},[De]:{order:7,menuItemFactory:Le}}}};var $e=`@univerjs-pro/sheets-pivot-ui`,et=`1.6.0`;const tt={type:n.CommandType.OPERATION,id:`sheet.operation.open-pivot-table-filter-panel`,async handler(e,n){if(!n)return!1;let{row:r,col:a,unitId:o,subUnitId:s,tableFieldId:c,pivotTableId:l}=n,u=e.get(t.SheetsPivotTableAdaptorModel),d=l||u.getPivotTableIdByCell(o,s,r,a);if(!d)return!1;let f=e.get(t.SheetsPivotRPCService),p=u.getPivotItemRenderInfoCache(o,s,d);if(!(p!=null&&p.rangesInfo))return!1;let m=u.getPivotTableCellData(o,s,r,a),h=(await f.getPivotDisplayConfig({unitId:o,subUnitId:s,pivotTableId:d,params:{type:t.PivotDisplayDataType.Filter,filter:{row:r,col:a,tableFieldId:c,cellData:m,pivotItemRenderInfoCache:p}}})).result.filter;if(!h)return!1;let g=e.get(i.IDialogService),_={id:x,draggable:!0,mask:!1,maskClosable:!1,width:336,dialogStyles:{header:{padding:`16px 16px 0`,height:0},body:{padding:`0 16px 16px`}},closable:!1,children:{label:b,unitId:o,subUnitId:s,key:`${r}-${a}-${c||``}`,filterItemInfo:h},onClose:()=>{g.close(x)}};return g.open(_),!0}},nt={type:n.CommandType.OPERATION,id:`sheet.operation.open-pivot-table-field-setting-panel`,handler(e,t){if(!t)return!1;let r=e.get(n.LocaleService),{tableFieldId:a,pivotTableId:o,unitId:s,subUnitId:c,tableFieldInfo:l,dataFieldInfo:u}=t,d=e.get(i.IDialogService),f={id:C,title:{title:r.t(`sheets-pivot-ui.fieldSettings`)},draggable:!0,mask:!1,maskClosable:!1,width:400,children:{label:S,tableFieldId:a,pivotTableId:o,tableFieldInfo:l,dataFieldInfo:u,unitId:s,subUnitId:c,version:Math.random(),key:`field-setting-${a||``}`,closeFn:()=>{d.close(C)}},onClose:()=>{d.close(C)}};return d.open(f),!0}};function rt(t,n,r){let i=r.fieldIds,{fieldsConfig:a}=n,{rowFields:o,columnFields:s,valueFields:c,filterFields:l,hiddenFields:u,dimension:d,measure:f,valueIndex:p,valuePosition:m}=a,h=/* @__PURE__ */ new Set;[...o,...s,...c,...l,...u].forEach(e=>{let t=d[e]||f[e];t&&h.add(t.dataFieldId)});let g=i.map(e=>{let n=r.fields[e];return{checked:h.has(e),dataFieldId:n.id,id:n.id,name:n.name,type:n.fieldDataType,pivotTableId:t}}),_=o.flatMap(e=>{let t=d[e];return t?[{dataFieldId:t.dataFieldId,tableFieldId:t.id,displayName:t.displayName}]:[]}),v=s.flatMap(e=>{let t=d[e];return t?[{dataFieldId:t.dataFieldId,tableFieldId:t.id,displayName:t.displayName}]:[]}),y=c.flatMap(e=>{let t=f[e];return t?[{dataFieldId:t.dataFieldId,tableFieldId:t.id,displayName:t.displayName,format:t.format,subTotalType:t.subtotal}]:[]}),b=l.flatMap(e=>{let t=d[e];return t?[{dataFieldId:t.dataFieldId,tableFieldId:t.id,displayName:t.displayName}]:[]}),x=u.flatMap(e=>{let t=d[e];return t?[{dataFieldId:t.dataFieldId,tableFieldId:t.id,displayName:t.displayName,filterInfo:t.filterInfo}]:[]});return y.length>1&&(m===e.PivotTableValuePositionEnum.Row?_:v).splice(p,0,{dataFieldId:``,tableFieldId:`PivotMultipleValueId`,displayName:`ΣValue`}),{sourceList:g,sourceRange:n.sourceRangeInfo,rowFields:_,columnFields:v,valueFields:y,filterFields:b,hiddenFields:x,positionInfo:n.targetCellInfo}}const it={type:n.CommandType.OPERATION,id:`sheet.operation.open-pivot-table-panel`,async handler(e,a){let o=a==null?void 0:a.pivotTableId;if(!o)return!1;let s=e.get(i.ISidebarService),c=e.get(n.IUniverInstanceService),l=e.get(n.LocaleService),u=e.get(t.SheetsPivotTableConfigModel),d=(0,r.getSheetCommandTarget)(c);if(!d)return!1;let{unitId:f,subUnitId:p}=d,m=u.getPivotTableConfig(f,p,o),h=u.getCollection(f,o);if(!m||!h)return!1;let g=rt(o,m,h),_={header:{title:l.t(`sheets-pivot-ui.title`)},id:y,children:{label:v,panelInfo:g,version:Math.random(),setVersion:()=>Math.random(),pivotTableId:o,unitId:f,subUnitId:p},width:346};return s.open(_),!0}},at=`sheets-pivot-ui.config`,ot={};function st(e){"@babel/helpers - typeof";return st=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},st(e)}function ct(e,t){if(st(e)!=`object`||!e)return e;var n;if(typeof Symbol<`u`&&(n=e[Symbol.toPrimitive])!==void 0){var r=n.call(e,t||`default`);if(st(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function lt(e){var t=ct(e,`string`);return st(t)==`symbol`?t:t+``}function G(e,t,n){return(t=lt(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function K(e,t){return function(n,r){t(n,r,e)}}function q(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let J=class extends n.Disposable{constructor(e,n,r,i,a,o,s,c){var l;super(),this._localeService=e,this._sheetInterceptorService=n,this._injector=r,this._confirmService=i,this._sheetsPivotTableService=a,this._configService=o,this._sheetPermissionCheckController=s,this._sheetsRenderService=c,G(this,`_defaultOverride`,!1),this.disposeWithMe(this._sheetsRenderService.registerSelfRenderingMutations(t.InitializePivotRuntimeLocalMutation.id)),this._initUIInterceptListener(),this._defaultOverride=((l=this._configService.getConfig(t.SHEETS_PIVOT_PLUGIN_CONFIG_KEY))==null?void 0:l.defaultOverride)??!1}_getPivotAppliedRanges(e,n,r){return(0,t.getPivotAppliedRanges)(e,n,r,this._localeService.getDirection())}_initUIInterceptListener(){this._sheetsPivotTableService.asyncInterceptor.interceptAsync(this._sheetsPivotTableService.asyncInterceptor.getInterceptPoints().PIVOT_PERFORM_CHECK,{handler:async(e,n)=>{let{cellInfo:i,view:a,isEmpty:o}=n,{unitId:s,subUnitId:c}=i,l=this._getPivotAppliedRanges(i,a,o);if(!(0,r.checkRangesEditablePermission)(this._injector,s,c,l)){let e=this._localeService.t(`sheets-pivot-ui.msg.permissionError`);this._sheetPermissionCheckController.blockExecuteWithoutPermission(e)}let u=this._sheetInterceptorService.generateMutationsByRanges({unitId:s,subUnitId:c,ranges:l}),d=(0,t.generateSetRangeValueMutations)(this._injector,s,c,l);if(u.redos.length||d.shouldClear){if(this._defaultOverride)return Promise.resolve(!0);{let e=await this._confirmService.confirm({id:`sheet-pivot.confirm.add-sheet-pivot`,title:{title:this._localeService.t(`sheets-pivot-ui.perform.tips`)},children:{title:this._localeService.t(`sheets-pivot-ui.perform.info`)},cancelText:this._localeService.t(`sheets-pivot-ui.perform.cancel`),confirmText:this._localeService.t(`sheets-pivot-ui.perform.confirm`)});return Promise.resolve(e)}}return Promise.resolve(!0)}})}};J=q([K(0,(0,n.Inject)(n.LocaleService)),K(1,(0,n.Inject)(r.SheetInterceptorService)),K(2,(0,n.Inject)(n.Injector)),K(3,(0,n.Inject)(n.IConfirmService)),K(4,(0,n.Inject)(t.SheetsPivotTableService)),K(5,(0,n.Inject)(n.IConfigService)),K(6,(0,n.Inject)(r.SheetPermissionCheckController)),K(7,(0,n.Inject)(a.SheetsRenderService))],J);function ut(e){let{editable:r,pivotTableId:a,subUnitId:o,unitId:c}=e,l=(0,i.useDependency)(n.ICommandService),u=(0,i.useDependency)(i.IDialogService),f=(0,i.useDependency)(n.LocaleService),p=[{command:{id:it.id,params:{pivotTableId:a}},disabled:!r,label:`sheets-pivot-ui.mobile.edit`},{command:{id:t.RemovePivotTableCommand.id,params:{unitId:c,subUnitId:o,pivotTableId:a}},disabled:!r,label:`sheets-pivot-ui.mobile.delete`}];async function m(e){await l.executeCommand(e.id,e.params),u.close(N)}return/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-flex-col univer-gap-2`,children:p.map(e=>{let t=f.t(e.label);return/* @__PURE__ */ (0,d.jsx)(s.MobileActionRow,{title:t,"aria-label":t,variant:`subtle`,disabled:e.disabled,onClick:()=>m(e.command)},e.command.id)})})}const dt=[{label:`1930-08-05`,suffix:`yyyy-MM-dd`},{label:`1930/08/05`,suffix:`yyyy/MM/dd`},{label:`1930年08月05日`,suffix:`yyyy"年"MM"月"dd"日"`},{label:`08-05`,suffix:`MM-dd`},{label:`8月5日`,suffix:`M"月"d"日"`},{label:`13:30:30`,suffix:`h:mm:ss`},{label:`13:30`,suffix:`h:mm`},{label:`下午01:30`,suffix:`A/P hh:mm`},{label:`下午1:30`,suffix:`A/P h:mm`},{label:`下午1:30:30`,suffix:`A/P h:mm:ss`},{label:`08-05 下午 01:30`,suffix:`MM-dd A/P hh:mm`}],ft=[{label:`(1,235)`,suffix:`#,##0_);(#,##0)`},{label:`(1,235) `,suffix:`#,##0_);[Red](#,##0)`,color:`red`},{label:`1,234.56`,suffix:`#,##0.00_);#,##0.00`},{label:`1,234.56`,suffix:`#,##0.00_);[Red]#,##0.00`,color:`red`},{label:`-1,234.56`,suffix:`#,##0.00_);-#,##0.00`},{label:`-1,234.56`,suffix:`#,##0.00_);[Red]-#,##0.00`,color:`red`}],pt=[{label:e=>`${e}1,235`,suffix:e=>`"${e}"#,##0.00_);"${e}"#,##0.00`},{label:e=>`${e}1,235`,suffix:e=>`"${e}"#,##0.00_);[Red]"${e}"#,##0.00`,color:`red`},{label:e=>`(${e}1,235)`,suffix:e=>`"${e}"#,##0.00_);("${e}"#,##0.00)`},{label:e=>`(${e}1,235)`,suffix:e=>`"${e}"#,##0.00_);[Red]("${e}"#,##0.00)`,color:`red`},{label:e=>`-${e}1,235`,suffix:e=>`"${e}"#,##0.00_);-"${e}"#,##0.00`},{label:e=>`-${e}1,235`,suffix:e=>`"${e}"#,##0.00_);[Red]-"${e}"#,##0.00`,color:`red`}],mt=`PivotMultipleValueId`;function ht(t,n){if(n===e.PivotDataFieldDataTypeEnum.blank)return`sheets-pivot-ui.dialog.filter.blank`;if(n===e.PivotDataFieldDataTypeEnum.other&&t===e.GlobalConfig.otherDataKey)return`sheets-pivot-ui.showValuesAs.other`}function gt(e,n){let r=(0,t.isolateTextForBidiContext)(n,e);return e.includes(`{0}`)?e.replace(`{0}`,r):`${e}${r}`}const _t=t=>{switch(t){case`filter`:return e.PivotTableFiledAreaEnum.Filter;case`row`:return e.PivotTableFiledAreaEnum.Row;case`column`:return e.PivotTableFiledAreaEnum.Column;case`value`:return e.PivotTableFiledAreaEnum.Value;default:return e.PivotTableFiledAreaEnum.Row}};function vt(e,t){switch(t.rangeType||n.RANGE_TYPE.NORMAL){case n.RANGE_TYPE.NORMAL:return{startRow:t.startRow,startColumn:t.startColumn,endRow:t.endRow,endColumn:t.endColumn};case n.RANGE_TYPE.ROW:return{startRow:t.startRow,startColumn:0,endRow:t.endRow,endColumn:e.getColumnCount()-1};case n.RANGE_TYPE.COLUMN:return{startRow:0,startColumn:t.startColumn,endRow:e.getRowCount()-1,endColumn:t.endColumn};case n.RANGE_TYPE.ALL:return{startRow:0,startColumn:0,endRow:e.getRowCount()-1,endColumn:e.getColumnCount()-1}}}function yt(e,t){if(!t||!e||t.endRow-t.startRow<=0)return 1;let{startRow:n,startColumn:r,endRow:i,endColumn:a}=vt(e,t),o=e.getCellMatrix(),s=!1;for(let e=n;e<=i;e++){for(let t=r;t<=a;t++)if(o.getValue(e,t)!=null){s=!0;break}if(s)break}return s?2:0}async function bt(e,n,r){let{commandService:i,pivotTableInfo:a,pivotTableId:o,unitId:s,subUnitId:c}=e,l=[],{rowFields:u,columnFields:d,filterFields:f,valueFields:p}=a,m=[...u,...d,...f,...p];n===`PivotMultipleValueId`&&p.forEach(e=>{l.push(e.tableFieldId)}),r===`source`&&m.forEach(e=>{e.dataFieldId===n&&l.push(e.tableFieldId)}),r!==`source`&&m.find(e=>e.tableFieldId===n)&&l.push(n),await i.executeCommand(t.RemovePivotFieldCommand.id,{pivotTableId:o,fieldIds:l,unitId:s,subUnitId:c})}async function xt(e,n,r,i,a){let o={unitId:i,subUnitId:a,token:n,dataRangeInfo:r};await e.executeCommand(t.UpdatePivotTableSourceRangeCommand.id,o)}const St=(e,t)=>{let{rowFields:n,columnFields:r,filterFields:i}=e,a=[...n,...r,...i],o=[];return a.forEach(e=>{e.dataFieldId===t&&o.push(e.tableFieldId)}),o};async function Ct(e,n,r,i,a){if(r===`source`&&i===`source`)return;let o=n===mt,{commandService:s,pivotTableInfo:c,pivotTableId:l,unitId:u,subUnitId:d}=e;if(r===`source`&&i!==`source`){let{sourceList:e}=c,r=e.find(e=>e.id===n);if(r){let e=St(c,n);if(i!==`value`&&e.length>0){let n=e[0];await s.executeCommand(t.MovePivotFieldCommand.id,{pivotTableId:l,fieldId:n,area:_t(i),index:a,unitId:u,subUnitId:d})}else await s.executeCommand(t.AddPivotFieldCommand.id,{pivotTableId:l,dataFieldId:r.dataFieldId,fieldArea:_t(i),index:a,unitId:u,subUnitId:d})}}if(r!==`source`&&i===`source`){let{rowFields:t,columnFields:r,filterFields:i,valueFields:a}=c,s=[...t,...r,...i,...a],l=[];o?a.forEach(e=>{l.push(e.tableFieldId)}):s.find(e=>e.tableFieldId===n)&&l.push(n),await wt(e,l)}else r!==`source`&&i!==`source`&&await Et(e,n,r,i,a)}async function wt(e,n){let{commandService:r,pivotTableId:i,unitId:a,subUnitId:o}=e;await r.executeCommand(t.RemovePivotFieldCommand.id,{pivotTableId:i,fieldIds:n,unitId:a,subUnitId:o})}async function Tt(e,n,r){let{commandService:i,pivotTableId:a,unitId:o,subUnitId:s}=e;await i.executeCommand(t.UpdatePivotValuePositionCommand.id,{pivotTableId:a,position:n,index:r,unitId:o,subUnitId:s})}async function Et(n,r,i,a,o){let s=r===mt,{commandService:c,pivotTableInfo:l,pivotTableId:u,unitId:d,subUnitId:f}=n;if(s){let n=_t(a);(n===e.PivotTableFiledAreaEnum.Row||n===e.PivotTableFiledAreaEnum.Column)&&await c.executeCommand(t.UpdatePivotValuePositionCommand.id,{pivotTableId:u,position:n===e.PivotTableFiledAreaEnum.Row?e.PivotTableValuePositionEnum.Row:e.PivotTableValuePositionEnum.Column,index:o,unitId:d,subUnitId:f});return}let{rowFields:p,columnFields:m,filterFields:h,valueFields:g}=l,_=[...p,...m,...h,...g].find(e=>e.tableFieldId===r);if(_){let e=[...p,...m,...h];if(i===`value`&&a!==`value`&&e.find(e=>e.dataFieldId===_.dataFieldId)){await wt(n,[r]);return}await c.executeCommand(t.MovePivotFieldCommand.id,{pivotTableId:u,fieldId:r,area:_t(a),index:o,unitId:d,subUnitId:f})}}function Dt(e,t,n,r,i){e.executeCommand(tt.id,{row:-1,col:-1,unitId:t,subUnitId:n,tableFieldId:i,pivotTableId:r})}function Ot(e,t,n,r,i,a){let{sourceList:o,rowFields:s,columnFields:c,valueFields:l,filterFields:u}=a,d=[...s,...c,...l,...u].find(e=>e.tableFieldId===n),f=o.find(e=>e.id===(d==null?void 0:d.dataFieldId));if(d&&f){let a={tableFieldId:n,pivotTableId:t,unitId:r,subUnitId:i,tableFieldInfo:d,dataFieldInfo:f};e.executeCommand(nt.id,a)}}const kt=[{type:e.PivotSubtotalTypeEnum.average,textKey:`sheets-pivot-ui.subtotal.average`},{type:e.PivotSubtotalTypeEnum.count,textKey:`sheets-pivot-ui.subtotal.count`},{type:e.PivotSubtotalTypeEnum.countNums,textKey:`sheets-pivot-ui.subtotal.countNums`},{type:e.PivotSubtotalTypeEnum.max,textKey:`sheets-pivot-ui.subtotal.max`},{type:e.PivotSubtotalTypeEnum.min,textKey:`sheets-pivot-ui.subtotal.min`},{type:e.PivotSubtotalTypeEnum.product,textKey:`sheets-pivot-ui.subtotal.product`},{type:e.PivotSubtotalTypeEnum.stdDev,textKey:`sheets-pivot-ui.subtotal.stdDev`},{type:e.PivotSubtotalTypeEnum.stdDevp,textKey:`sheets-pivot-ui.subtotal.stdDevp`},{type:e.PivotSubtotalTypeEnum.sum,textKey:`sheets-pivot-ui.subtotal.sum`},{type:e.PivotSubtotalTypeEnum.var,textKey:`sheets-pivot-ui.subtotal.var`},{type:e.PivotSubtotalTypeEnum.varp,textKey:`sheets-pivot-ui.subtotal.varp`}],At=(e,t=0)=>e?n.numfmt.getFormatInfo(e).maxDecimals??t:t,jt=e=>Array(Math.min(Math.max(0,Number(e)),30)).fill(0).join(``),Y=(e,t)=>e.split(`;`).map(e=>/\.0?/.test(e)?e.replace(/\.0*/g,`${t>0?`.`:``}${jt(Number(t||0))}`):/0[^0]?/.test(e)?e.replace(/0[^0]+|0$/,`0${t>0?`.`:``}${jt(Number(t||0))}`):e).join(`;`),Mt=e=>n.currencySymbols.find(t=>e.includes(t)),Nt=e=>!e,Pt=e=>!!Mt(e)&&e.startsWith(`_(`),Ft=e=>!!Mt(e)&&!e.startsWith(`_(`),It=()=>dt.map(e=>({label:e.label,value:e.suffix})),Lt=e=>{let t=n.numfmt.getFormatInfo(e);return It().map(e=>e.value).includes(e)||[`date`,`datetime`,`time`].includes(t.type)},Rt=()=>ft.map(e=>({label:e.label,value:e.suffix,color:e.color})),zt=e=>Rt().some(t=>(0,n.isPatternEqualWithoutDecimal)(t.value,e)),Bt=(t,n)=>{switch(n){case e.PivotTableFiledAreaEnum.Row:return t.rowFields.length;case e.PivotTableFiledAreaEnum.Column:return t.columnFields.length;case e.PivotTableFiledAreaEnum.Filter:return t.filterFields.length;case e.PivotTableFiledAreaEnum.Value:return t.valueFields.length;default:return 0}},Vt=(e,n,r)=>{for(let i of[`sheets-pivot-ui.averageName`,`sheets-pivot-ui.countName`,`sheets-pivot-ui.countNumsName`,`sheets-pivot-ui.maxName`,`sheets-pivot-ui.minName`,`sheets-pivot-ui.productName`,`sheets-pivot-ui.stdDevName`,`sheets-pivot-ui.stdDevpName`,`sheets-pivot-ui.sumName`,`sheets-pivot-ui.varName`,`sheets-pivot-ui.varpName`]){let a=r[i];if(a+(0,t.isolateTextForBidiContext)(e,a)===n||a+e===n)return!0}return!1},Ht=(n,r,i)=>{let a=``;switch(i){case e.PivotSubtotalTypeEnum.average:a=r[`sheets-pivot-ui.averageName`];break;case e.PivotSubtotalTypeEnum.count:a=r[`sheets-pivot-ui.countName`];break;case e.PivotSubtotalTypeEnum.countNums:a=r[`sheets-pivot-ui.countNumsName`];break;case e.PivotSubtotalTypeEnum.max:a=r[`sheets-pivot-ui.maxName`];break;case e.PivotSubtotalTypeEnum.min:a=r[`sheets-pivot-ui.minName`];break;case e.PivotSubtotalTypeEnum.product:a=r[`sheets-pivot-ui.productName`];break;case e.PivotSubtotalTypeEnum.stdDev:a=r[`sheets-pivot-ui.stdDevName`];break;case e.PivotSubtotalTypeEnum.stdDevp:a=r[`sheets-pivot-ui.stdDevpName`];break;case e.PivotSubtotalTypeEnum.sum:a=r[`sheets-pivot-ui.sumName`];break;case e.PivotSubtotalTypeEnum.var:a=r[`sheets-pivot-ui.varName`];break;case e.PivotSubtotalTypeEnum.varp:a=r[`sheets-pivot-ui.varpName`];break;default:return n}return a+(0,t.isolateTextForBidiContext)(n,a)};function Ut(t,n){if(n.conditionFilter&&(0,e.isPivotTableFilterRestrictive)(n.conditionFilter))return`HasFilter`;if(!t)return`NotFilter`;if((0,e.isPivotDateFilter)(t))return`HasFilter`;if((0,e.isManualPivotFilter)(t)){var r;return t.isAll||((r=n.items)==null?void 0:r.length)===t.list.length?`NotFilter`:`HasFilter`}return(0,e.isPivotTableFilterRestrictive)(t)?`HasFilter`:`NotFilter`}function Wt(e){return Pt(e)?`accounting`:Ft(e)?`currency`:Lt(e)?`date`:zt(e)?`thousandthPercentile`:`general`}function Gt(e){let{originPattern:t=``,onChange:r}=e,a=(0,i.useDependency)(n.LocaleService),[o,c]=(0,f.useState)(()=>Wt(t)),[l,u]=(0,f.useState)(()=>At(t,2)),[p,m]=(0,f.useState)(()=>Mt(t)||n.currencySymbols[0]),[h,g]=(0,f.useState)(t),_=(0,f.useMemo)(()=>[{value:`general`,label:a.t(`sheets-pivot-ui.dialog.numfmt.general`)},{value:`accounting`,label:a.t(`sheets-pivot-ui.dialog.numfmt.accounting`)},{value:`currency`,label:a.t(`sheets-pivot-ui.dialog.numfmt.currency`)},{value:`date`,label:a.t(`sheets-pivot-ui.dialog.numfmt.date`)},{value:`thousandthPercentile`,label:a.t(`sheets-pivot-ui.dialog.numfmt.thousandthPercentile`)}],[a]),v=n.currencySymbols.map(e=>({label:e,value:e}));function y(e){g(e),r(e)}function b(e,t){return Y(`_("${e}"* #,##0${t>0?`.0`:``}_)`,t)}function x(e){c(e),y(e===`general`?``:e===`accounting`?b(p,l):e===`currency`?Y(pt[0].suffix(p),l):e===`date`?dt[0].suffix:Y(ft[0].suffix,l))}function S(e){m(e),y(o===`accounting`?b(e,l):Y(pt[0].suffix(e),l))}function C(e){let t=e??0;u(t),y(o===`accounting`?b(p,t):Y(h,t))}let w=pt.map(e=>({label:e.label(p),value:e.suffix(p),color:e.color})),T=ft.map(e=>({label:e.label,value:e.suffix,color:e.color})),E=dt.map(e=>({label:e.label,value:e.suffix}));return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:a.t(`sheets-pivot-ui.dialog.numfmt.formatTypes`)}),/* @__PURE__ */ (0,d.jsx)(s.MobileSelect,{className:`univer-w-full`,value:o,options:_,onChange:e=>x(e),size:`small`})]}),(o===`accounting`||o===`currency`)&&/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-grid univer-grid-cols-2 univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:a.t(`sheets-pivot-ui.dialog.numfmt.decimalPlaces`)}),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-w-full`,value:l,min:0,max:20,onChange:C,size:`small`,controls:!1})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:a.t(`sheets-pivot-ui.dialog.numfmt.currencyTypes`)}),/* @__PURE__ */ (0,d.jsx)(s.MobileSelect,{className:`univer-w-full`,value:p,options:v,onChange:S,size:`small`})]})]}),o===`thousandthPercentile`&&/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:a.t(`sheets-pivot-ui.dialog.numfmt.decimalPlaces`)}),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-w-full`,value:l,min:0,max:20,onChange:C,size:`small`,controls:!1})]}),o===`currency`&&/* @__PURE__ */ (0,d.jsx)(s.SelectList,{className:`univer-rounded-xl univer-p-2`,options:w,value:h,onChange:e=>y(Y(e,l))}),o===`date`&&/* @__PURE__ */ (0,d.jsx)(s.SelectList,{className:`univer-rounded-xl univer-p-2`,options:E,value:h,onChange:e=>y(e)}),o===`thousandthPercentile`&&/* @__PURE__ */ (0,d.jsx)(s.SelectList,{className:`univer-rounded-xl univer-p-2`,options:T,value:h,onChange:e=>y(Y(e,l))}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-text-gray-500`,children:a.t(`sheets-pivot-ui.dialog.numfmt.${o}Desc`)})]})}function Kt(e){let{unitId:r,subUnitId:a,pivotTableId:o,tableFieldId:c,originPattern:l,tableFieldInfo:u,dataFieldInfo:p}=e,m=(0,i.useDependency)(n.ICommandService),h=(0,i.useDependency)(i.IDialogService),g=(0,i.useDependency)(n.LocaleService),[_,v]=(0,f.useState)(l??``);async function y(){let e={format:_,tableFieldId:c,pivotTableId:o,unitId:r,subUnitId:a,tableFieldInfo:u,dataFieldInfo:p};await m.executeCommand(t.UpdatePivotFieldSettingCommand.id,e),h.close(T)}return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-5`,dir:g.getDirection(),children:[/* @__PURE__ */ (0,d.jsx)(Gt,{originPattern:l,onChange:v}),/* @__PURE__ */ (0,d.jsxs)(s.MobileActionRowGroup,{children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:()=>h.close(T),size:`middle`,children:g.t(`sheets-pivot-ui.dialog.numfmt.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,onClick:y,size:`middle`,children:g.t(`sheets-pivot-ui.dialog.numfmt.confirm`)})]})]})}function qt(e){let{unitId:r,subUnitId:a,pivotTableId:o,tableFieldId:c,tableFieldInfo:l,dataFieldInfo:p,closeFn:m,format:h,version:g}=e,_=(0,i.useDependency)(n.ICommandService),v=(0,i.useDependency)(t.SheetsPivotTableConfigModel),y=(0,i.useDependency)(n.LocaleService),[b,x]=(0,f.useState)(`field`),[S,C]=(0,f.useState)(l.displayName),[w,T]=(0,f.useState)(l.subTotalType),[E,D]=(0,f.useState)(h??l.format??``),[O,k]=(0,f.useState)(E);(0,f.useEffect)(()=>{x(`field`),C(l.displayName),T(l.subTotalType),D(h??l.format??``),k(h??l.format??``)},[h,l,g]);let A={...v.getTextInfo()};[`sheets-pivot-ui.averageName`,`sheets-pivot-ui.countName`,`sheets-pivot-ui.countNumsName`,`sheets-pivot-ui.maxName`,`sheets-pivot-ui.minName`,`sheets-pivot-ui.productName`,`sheets-pivot-ui.stdDevName`,`sheets-pivot-ui.stdDevpName`,`sheets-pivot-ui.sumName`,`sheets-pivot-ui.varName`,`sheets-pivot-ui.varpName`].forEach(e=>{A[e]=y.t(e)});function j(e){T(e),Vt(p.name,S,A)&&C(Ht(p.name,A,e))}async function M(){let e={displayName:l.displayName===S?void 0:S,subtotalType:l.subTotalType===w?void 0:w,format:l.format===E?void 0:E,tableFieldId:c,pivotTableId:o,unitId:r,subUnitId:a,tableFieldInfo:l,dataFieldInfo:p};await _.executeCommand(t.UpdatePivotFieldSettingCommand.id,e),m()}return b===`format`?/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-5`,dir:y.getDirection(),children:[/* @__PURE__ */ (0,d.jsxs)(`button`,{type:`button`,className:`univer-flex univer-h-10 univer-items-center univer-gap-2 univer-border-0 univer-bg-transparent univer-p-0 univer-text-base univer-font-semibold`,onClick:()=>x(`field`),children:[/* @__PURE__ */ (0,d.jsx)(u.ArrowLeftIcon,{}),y.t(`sheets-pivot-ui.dialog.numfmt.fieldSetting`)]}),/* @__PURE__ */ (0,d.jsx)(Gt,{originPattern:E,onChange:k}),/* @__PURE__ */ (0,d.jsxs)(s.MobileActionRowGroup,{children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:()=>x(`field`),size:`middle`,children:y.t(`sheets-pivot-ui.dialog.numfmt.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,onClick:()=>{D(O),x(`field`)},size:`middle`,children:y.t(`sheets-pivot-ui.dialog.numfmt.confirm`)})]})]}):/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,dir:y.getDirection(),children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm univer-font-medium`,children:y.t(`sheets-pivot-ui.dialog.fieldName`)}),/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-truncate univer-text-xs univer-text-gray-500`,children:gt(y.t(`sheets-pivot-ui.dialog.sourceName`),p.name)})]}),/* @__PURE__ */ (0,d.jsx)(s.Input,{className:`univer-mt-2 univer-w-full`,value:S,onChange:C,size:`small`})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:y.t(`sheets-pivot-ui.dialog.summarizeBy`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-max-h-[40vh] univer-overflow-y-auto univer-rounded-xl univer-bg-gray-50 univer-p-1 dark:!univer-bg-gray-800`,children:kt.map(e=>/* @__PURE__ */ (0,d.jsxs)(`button`,{type:`button`,className:(0,s.clsx)(`univer-flex univer-h-12 univer-w-full univer-items-center univer-gap-2 univer-rounded-xl univer-border-0 univer-bg-transparent univer-px-3 univer-text-left univer-text-sm`,{"univer-bg-primary-50 dark:!univer-bg-gray-700":w===e.type}),onClick:()=>j(e.type),children:[/* @__PURE__ */ (0,d.jsx)(u.CheckMarkIcon,{className:(0,s.clsx)(`univer-text-primary-600`,{"univer-invisible":w!==e.type})}),y.t(e.textKey)]},e.type))})]}),/* @__PURE__ */ (0,d.jsx)(s.Button,{className:`univer-w-full`,onClick:()=>{k(E),x(`format`)},size:`middle`,children:y.t(`sheets-pivot-ui.dialog.format`)}),/* @__PURE__ */ (0,d.jsxs)(s.MobileActionRowGroup,{children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:m,size:`middle`,children:y.t(`sheets-pivot-ui.dialog.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:!S,onClick:M,size:`middle`,children:y.t(`sheets-pivot-ui.dialog.ok`)})]})]})}function Jt(r){var a;let{filterItemInfo:o,unitId:c,subUnitId:l}=r,p=(0,i.useDependency)(n.ICommandService),m=(0,i.useDependency)(i.IDialogService),h=(0,i.useDependency)(n.LocaleService),g=(0,i.useDependency)(n.IUniverInstanceService),_=(0,i.useObservable)(h.direction$,h.getDirection()),[v,y]=(0,f.useState)(()=>{var e;return o.checkedItems??((e=o.filterInfo)==null?void 0:e.list)??o.items}),[b,S]=(0,f.useState)(()=>o.partialItems??[]),[C,w]=(0,f.useState)((a=o.sortInfo)==null?void 0:a.type),T=b.length===0&&v.length===o.items.length,E=Ut(o.filterInfo,{items:o.items,conditionFilter:o.conditionFilter});(0,f.useEffect)(()=>{let e=g.getCurrentUnitOfType(n.UniverInstanceType.UNIVER_SHEET);if(!e)return;let t=e.activeSheet$.subscribe(e=>{(e==null?void 0:e.getSheetId())!==l&&m.close(x)});return()=>t.unsubscribe()},[m,l,g]);function D(){m.close(x)}function O(e){b.includes(e)?(S(t=>t.filter(t=>t!==e)),y(t=>[...t,e])):y(t=>t.includes(e)?t.filter(t=>t!==e):[...t,e])}async function k(e,n,r,i=!1){await p.executeCommand(t.SetPivotFilterCommand.id,{unitId:c,subUnitId:l,pivotTableId:o.pivotTableId,tableFieldId:o.tableFieldId,items:e,partialItems:r,isAll:n,clearAllFilters:i}),D()}async function A(e){await p.executeCommand(t.SetPivotSortCommand.id,{unitId:c,subUnitId:l,pivotTableId:o.pivotTableId,tableFieldId:o.tableFieldId,info:{type:e}}),w(e),D()}return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,dir:_,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-base univer-font-semibold`,children:h.t(`sheets-pivot-ui.filterField`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-grid univer-grid-cols-2 univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsxs)(s.Button,{className:(0,s.clsx)(``,{"univer-bg-gray-100 dark:!univer-bg-gray-700":C===e.PivotDataFieldSortOperatorEnum.ascending}),onClick:()=>A(e.PivotDataFieldSortOperatorEnum.ascending),size:`middle`,children:[/* @__PURE__ */ (0,d.jsx)(u.AscendingIcon,{}),h.t(`sheets-pivot-ui.dialog.filter.ascending`)]}),/* @__PURE__ */ (0,d.jsxs)(s.Button,{className:(0,s.clsx)(``,{"univer-bg-gray-100 dark:!univer-bg-gray-700":C===e.PivotDataFieldSortOperatorEnum.descending}),onClick:()=>A(e.PivotDataFieldSortOperatorEnum.descending),size:`middle`,children:[/* @__PURE__ */ (0,d.jsx)(u.DescendingIcon,{}),h.t(`sheets-pivot-ui.dialog.filter.descending`)]})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-max-h-[40vh] univer-overflow-y-auto univer-rounded-xl univer-bg-gray-50 univer-p-2 dark:!univer-bg-gray-800`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-min-h-12 univer-items-center univer-px-2`,children:/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:T,indeterminate:!T&&(v.length>0||b.length>0),onChange:()=>{S([]),y(T?[]:o.items)},children:h.t(`sheets-pivot-ui.dialog.filter.selectAll`)})}),o.items.map((t,r)=>{var i;let a=o.itemTypes[r],c=(i=o.itemLabels)==null?void 0:i[t],l=ht(t,a),u=t;return l?u=h.t(l):c===void 0?a===e.PivotDataFieldDataTypeEnum.date&&o.format&&(u=n.numfmt.format(o.format,Number(t))):u=c,/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-min-h-12 univer-items-center univer-px-2`,children:/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:v.includes(t),indeterminate:b.includes(t),contentClassName:`univer-max-w-[70vw] univer-truncate`,onChange:()=>O(t),children:u})},t)})]}),/* @__PURE__ */ (0,d.jsx)(s.Button,{className:`univer-w-full`,variant:`link`,disabled:E===`NotFilter`,onClick:()=>k(o.items,!0,[],!0),size:`middle`,children:h.t(`sheets-pivot-ui.dialog.filter.clearFilter`)}),/* @__PURE__ */ (0,d.jsxs)(s.MobileActionRowGroup,{children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:D,size:`middle`,children:h.t(`sheets-pivot-ui.dialog.filter.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:!v.length&&!b.length,onClick:()=>k(v,T,b),size:`middle`,children:h.t(`sheets-pivot-ui.dialog.filter.confirm`)})]})]})}const Yt=[{label:`sheets-pivot-ui.config.pageDownThenOver`,value:`false`},{label:`sheets-pivot-ui.config.pageOverThenDown`,value:`true`}],Xt=[{label:`sheets-pivot-ui.config.layoutTabular`,value:String(e.PivotLayoutTypeEnum.tabular)},{label:`sheets-pivot-ui.config.layoutCompact`,value:String(e.PivotLayoutTypeEnum.compact)},{label:`sheets-pivot-ui.config.layoutOutline`,value:String(e.PivotLayoutTypeEnum.outline)}],Zt=[{label:`sheets-pivot-ui.config.rowSubtotalTop`,value:String(e.PivotSubtotalPositionEnum.top)},{label:`sheets-pivot-ui.config.rowSubtotalBottom`,value:String(e.PivotSubtotalPositionEnum.bottom)}],Qt=[{label:`sheets-pivot-ui.config.dateGroupingLegacy`,value:e.PivotDateGroupingMode.Legacy},{label:`sheets-pivot-ui.config.dateGroupingExcelLike`,value:e.PivotDateGroupingMode.ExcelLike}];function $t(r){let{unitId:a,subUnitId:c,pivotTableId:l}=r,u=(0,i.useDependency)(n.ICommandService),p=(0,i.useDependency)(n.IConfirmService),m=(0,i.useDependency)(n.ILogService),h=(0,i.useDependency)(n.LocaleService),g=(0,i.useDependency)(t.SheetsPivotTableConfigModel),_=(0,f.useMemo)(()=>g.watchPivotTableChanges(a,c,l).pipe((0,o.debounceTime)(0)),[g,a,c,l]);(0,i.useObservable)(_);let[v,y]=(0,f.useState)(!1),b=(0,f.useRef)(!1),x=g.getPivotTableConfig(a,c,l),S=(0,e.getPivotDateGroupingMode)(x==null?void 0:x.fieldsConfig.options),[C,w]=(0,f.useState)(()=>{var t,n;let r=g.getPivotTableConfig(a,c,l),i=(r==null||(t=r.fieldsConfig)==null?void 0:t.options)??{};return{showRowSubtotal:i.showRowSubtotal??!0,showRowGrandTotal:i.showRowGrandTotal??!0,showColSubtotal:i.showColSubtotal??!0,showColGrandTotal:i.showColGrandTotal??!0,pageWrap:i.pageWrap??1,pageOverThenDown:i.pageOverThenDown??!1,repeatRowLabels:i.repeatRowLabels??!1,repeatColLabels:i.repeatColLabels??!1,layout:(r==null||(n=r.fieldsConfig)==null?void 0:n.layout)??e.PivotLayoutTypeEnum.tabular,rowSubtotalPosition:i.rowSubtotalPosition??e.PivotSubtotalPositionEnum.top}});function T(e){let n={unitId:a,subUnitId:c,pivotTableId:l,options:e};return w(t=>({...t,...e})),u.executeCommand(t.SetPivotOptionCommand.id,n).catch(e=>m.error(`[MobilePivotConfigPanel]`,e))}function E(e){let n={unitId:a,subUnitId:c,pivotTableId:l,layout:e};return w(t=>({...t,layout:e})),u.executeCommand(t.SetPivotLayoutCommand.id,n).catch(e=>m.error(`[MobilePivotConfigPanel]`,e))}async function D(n){if(!(b.current||n===S)){b.current=!0,y(!0);try{let r=g.getPivotTableConfig(a,c,l),i=g.getCollection(a,l);if(!r||!i||(Object.values(i.fields).some(e.isDateGroupFieldJSON)||i.customFields.some(e.isDateGroupFieldJSON))&&!await p.confirm({id:`sheet-pivot.confirm.change-date-grouping-mode`,title:{title:h.t(`sheets-pivot-ui.config.dateGroupingSwitchTitle`)},children:{title:h.t(`sheets-pivot-ui.config.dateGroupingSwitchMessage`)},cancelText:h.t(`sheets-pivot-ui.dialog.cancel`),confirmText:h.t(`sheets-pivot-ui.dialog.ok`)})||g.getPivotTableConfig(a,c,l)!==r||g.getCollection(a,l)!==i||(0,e.getPivotDateGroupingMode)(r.fieldsConfig.options)===n)return;await u.executeCommand(t.SetPivotOptionCommand.id,{unitId:a,subUnitId:c,pivotTableId:l,options:{dateGroupingMode:n}})}catch(e){m.error(`[MobilePivotConfigPanel]`,e)}finally{b.current=!1,y(!1)}}}let O=e=>e.map(e=>({...e,label:h.t(e.label)}));return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-5 univer-py-4`,children:[/* @__PURE__ */ (0,d.jsxs)(`section`,{className:`univer-flex univer-flex-col univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.displaySection`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-h-10 univer-items-center univer-justify-between univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm`,children:h.t(`sheets-pivot-ui.config.dateGroupingMode`)}),/* @__PURE__ */ (0,d.jsx)(s.MobileSelect,{className:`univer-w-40`,value:S,options:O(Qt),disabled:v,onChange:e=>D(e),size:`small`})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-h-10 univer-items-center univer-justify-between univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm`,children:h.t(`sheets-pivot-ui.config.layout`)}),/* @__PURE__ */ (0,d.jsx)(s.MobileSelect,{className:`univer-w-40`,value:String(C.layout),options:O(Xt),onChange:e=>E(Number(e)),size:`small`})]}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.repeatRowLabels,disabled:C.layout===e.PivotLayoutTypeEnum.compact,onChange:e=>T({repeatRowLabels:!!e}),children:h.t(`sheets-pivot-ui.config.repeatRowLabels`)}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.repeatColLabels,onChange:e=>T({repeatColLabels:!!e}),children:h.t(`sheets-pivot-ui.config.repeatColLabels`)})]}),/* @__PURE__ */ (0,d.jsxs)(`section`,{className:`univer-flex univer-flex-col univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.totalsSection`)}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showRowSubtotal,onChange:e=>T({showRowSubtotal:!!e}),children:h.t(`sheets-pivot-ui.config.showRowSubtotal`)}),C.layout===e.PivotLayoutTypeEnum.outline&&/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-h-10 univer-items-center univer-justify-between univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm`,children:h.t(`sheets-pivot-ui.config.rowSubtotalPosition`)}),/* @__PURE__ */ (0,d.jsx)(s.MobileSelect,{className:`univer-w-40`,value:String(C.rowSubtotalPosition),options:O(Zt),onChange:e=>T({rowSubtotalPosition:Number(e)}),size:`small`})]}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showRowGrandTotal,onChange:e=>T({showRowGrandTotal:!!e}),children:h.t(`sheets-pivot-ui.config.showRowGrandTotal`)}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showColSubtotal,onChange:e=>T({showColSubtotal:!!e}),children:h.t(`sheets-pivot-ui.config.showColSubtotal`)}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showColGrandTotal,onChange:e=>T({showColGrandTotal:!!e}),children:h.t(`sheets-pivot-ui.config.showColGrandTotal`)})]}),/* @__PURE__ */ (0,d.jsxs)(`section`,{className:`univer-flex univer-flex-col univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.pageSection`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-h-10 univer-items-center univer-justify-between univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm`,children:h.t(`sheets-pivot-ui.config.pageWrap`)}),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-w-24`,value:C.pageWrap,min:1,max:20,onChange:e=>{let t=Number(e);return Number.isFinite(t)&&t>=1?T({pageWrap:t}):void 0},size:`small`,controls:!1})]}),/* @__PURE__ */ (0,d.jsx)(`p`,{className:`univer-text-xs univer-text-gray-600`,children:h.t(C.pageOverThenDown?`sheets-pivot-ui.config.pageWrapOverThenDownHelp`:`sheets-pivot-ui.config.pageWrapDownThenOverHelp`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-h-10 univer-items-center univer-justify-between univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm`,children:h.t(`sheets-pivot-ui.config.pageDirection`)}),/* @__PURE__ */ (0,d.jsx)(s.MobileSelect,{className:`univer-w-40`,value:String(C.pageOverThenDown),options:O(Yt),onChange:e=>T({pageOverThenDown:e===`true`}),size:`small`})]})]})]})}function en(e,t,n,r){let i=[];return t>0&&i.push(`moveUp`),t<n-1&&i.push(`moveDown`),r?(i.push(e===`column`?`moveToRow`:`moveToColumn`),i.push(`remove`),i):(e!==`filter`&&i.push(`moveToFilter`),e!==`column`&&i.push(`moveToColumn`),e!==`row`&&i.push(`moveToRow`),e===`value`?i.push(`settings`):(i.push(`moveToValue`),i.push(`filter`)),i.push(`remove`),i)}const tn=[`filter`,`column`,`row`,`value`];function nn(e,t){switch(t){case`filter`:return e.filterFields;case`column`:return e.columnFields;case`row`:return e.rowFields;case`value`:return e.valueFields;default:return[]}}function rn(e){switch(e){case`filter`:return`sheets-pivot-ui.filter`;case`column`:return`sheets-pivot-ui.column`;case`row`:return`sheets-pivot-ui.row`;default:return`sheets-pivot-ui.value`}}function an(e){switch(e){case`filter`:return`sheets-pivot-ui.mobile.filterDesc`;case`column`:return`sheets-pivot-ui.mobile.columnDesc`;case`row`:return`sheets-pivot-ui.mobile.rowDesc`;default:return`sheets-pivot-ui.valueDesc`}}function on(e){switch(e.area){case`filter`:return/* @__PURE__ */ (0,d.jsx)(u.FilterIcon,{});case`column`:return/* @__PURE__ */ (0,d.jsx)(u.RowIcon,{});case`row`:return/* @__PURE__ */ (0,d.jsx)(u.ColumnIcon,{});default:return/* @__PURE__ */ (0,d.jsx)(u.FunctionIcon,{})}}function sn(e){switch(e){case`moveUp`:return`sheets-pivot-ui.mobile.moveUp`;case`moveDown`:return`sheets-pivot-ui.mobile.moveDown`;case`moveToFilter`:return`sheets-pivot-ui.mobile.moveToFilter`;case`moveToColumn`:return`sheets-pivot-ui.mobile.moveToColumn`;case`moveToRow`:return`sheets-pivot-ui.mobile.moveToRow`;case`moveToValue`:return`sheets-pivot-ui.mobile.moveToValue`;case`filter`:return`sheets-pivot-ui.filterField`;case`settings`:return`sheets-pivot-ui.fieldSettings`;default:return`sheets-pivot-ui.removeField`}}function cn(e){switch(e){case`moveToFilter`:return`filter`;case`moveToColumn`:return`column`;case`moveToRow`:return`row`;case`moveToValue`:return`value`;default:return null}}function ln(e){let{area:t,field:r,index:a,fieldCount:o,info:c,pivotTableId:l,unitId:f,subUnitId:p,refresh:m}=e,h=(0,i.useDependency)(n.ICommandService),g=(0,i.useDependency)(n.LocaleService),_=r.tableFieldId===mt,v={pivotTableInfo:c,pivotTableId:l,commandService:h,unitId:f,subUnitId:p};async function y(e){if(e===`filter`){Dt(h,f,p,l,r.tableFieldId);return}if(e===`settings`){Ot(h,l,r.tableFieldId,f,p,c);return}if(e===`remove`){await bt(v,r.tableFieldId,t),await m();return}let n=cn(e)??t,i=nn(c,n).length;e===`moveUp`?i=a-1:e===`moveDown`&&(i=a+1),await Ct(v,r.tableFieldId,t,n,i),await m()}let b=en(t,a,o,_).map(e=>({type:`item`,children:g.t(sn(e)),variant:e===`remove`?`destructive`:`default`,onSelect:()=>y(e)}));return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-h-12 univer-items-center univer-gap-3 univer-rounded-xl univer-px-3`,children:[/* @__PURE__ */ (0,d.jsx)(`bdi`,{className:`univer-min-w-0 univer-flex-1 univer-truncate univer-text-sm`,dir:`auto`,children:_?g.t(`sheets-pivot-ui.multipleValue`):r.displayName}),/* @__PURE__ */ (0,d.jsx)(s.MobileDropdownMenu,{items:b,children:/* @__PURE__ */ (0,d.jsx)(`button`,{type:`button`,className:`univer-flex univer-size-10 univer-shrink-0 univer-items-center univer-justify-center univer-rounded-lg univer-border-0 univer-bg-transparent active:univer-bg-gray-100 dark:active:!univer-bg-gray-700`,children:/* @__PURE__ */ (0,d.jsx)(u.MoreHorizontalIcon,{})})})]})}function un(e){let t=(0,i.useDependency)(n.LocaleService);return/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,children:tn.map(n=>{let r=nn(e.info,n);return/* @__PURE__ */ (0,d.jsxs)(`section`,{children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-2 univer-flex univer-items-center univer-gap-2 univer-text-sm univer-font-medium`,children:[/* @__PURE__ */ (0,d.jsx)(on,{area:n}),t.t(rn(n))]}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-rounded-xl univer-bg-gray-50 univer-p-1 dark:!univer-bg-gray-800`,children:r.length?r.map((t,i)=>/* @__PURE__ */ (0,d.jsx)(ln,{...e,area:n,field:t,index:i,fieldCount:r.length},t.tableFieldId)):/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-p-4 univer-text-center univer-text-xs univer-text-gray-500`,children:t.t(an(n))})})]},n)})})}function dn(e,t){switch(t){case`filter`:return e.filterFields.length;case`column`:return e.columnFields.length;case`row`:return e.rowFields.length;case`value`:return e.valueFields.length;default:return 0}}function fn(t){let n=`univer-size-4 univer-shrink-0 univer-text-gray-500`;switch(t.type){case e.PivotDataFieldDataTypeEnum.number:return/* @__PURE__ */ (0,d.jsx)(u.NumberIcon,{className:n});case e.PivotDataFieldDataTypeEnum.date:return/* @__PURE__ */ (0,d.jsx)(u.CalendarIcon,{className:n});default:return/* @__PURE__ */ (0,d.jsx)(u.TextIcon,{className:n})}}function pn(t){let{field:r,info:a,pivotTableId:o,unitId:c,subUnitId:l,refresh:f}=t,p=(0,i.useDependency)(n.ICommandService),m=(0,i.useDependency)(n.LocaleService),h={pivotTableInfo:a,pivotTableId:o,commandService:p,unitId:c,subUnitId:l};async function g(t){if(t){let t=r.type===e.PivotDataFieldDataTypeEnum.number?`value`:`row`;await Ct(h,r.dataFieldId,`source`,t,dn(a,t))}else await bt(h,r.dataFieldId,`source`);await f()}async function _(e){await Ct(h,r.dataFieldId,`source`,e,dn(a,e)),await f()}let v=[{type:`item`,children:m.t(`sheets-pivot-ui.addToRow`),onSelect:()=>_(`row`)},{type:`item`,children:m.t(`sheets-pivot-ui.addToColumn`),onSelect:()=>_(`column`)},{type:`item`,children:m.t(`sheets-pivot-ui.addToValue`),onSelect:()=>_(`value`)},{type:`item`,children:m.t(`sheets-pivot-ui.addToFilter`),onSelect:()=>_(`filter`)}];return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-h-12 univer-items-center univer-gap-3 univer-rounded-xl univer-px-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:r.checked,onChange:e=>g(!!e)}),/* @__PURE__ */ (0,d.jsx)(fn,{type:r.type}),/* @__PURE__ */ (0,d.jsx)(`bdi`,{className:`univer-min-w-0 univer-flex-1 univer-truncate univer-text-sm`,dir:`auto`,children:r.name}),/* @__PURE__ */ (0,d.jsx)(s.MobileDropdownMenu,{items:v,children:/* @__PURE__ */ (0,d.jsx)(`button`,{type:`button`,className:`univer-flex univer-size-10 univer-shrink-0 univer-items-center univer-justify-center univer-rounded-lg univer-border-0 univer-bg-transparent active:univer-bg-gray-100 dark:active:!univer-bg-gray-700`,children:/* @__PURE__ */ (0,d.jsx)(u.MoreHorizontalIcon,{})})})]})}function mn(e){let{info:t}=e,r=(0,i.useDependency)(n.LocaleService),[a,o]=(0,f.useState)(``),c=a.trim().toLocaleLowerCase(),l=c?t.sourceList.filter(e=>e.name.toLocaleLowerCase().includes(c)):t.sourceList;return/* @__PURE__ */ (0,d.jsxs)(`section`,{className:`univer-flex univer-flex-col univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.sourceFieldTile`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-1 univer-text-xs univer-text-gray-500`,children:r.t(`sheets-pivot-ui.mobile.sourceFieldDescription`)})]}),/* @__PURE__ */ (0,d.jsx)(s.Input,{allowClear:!0,value:a,placeholder:r.t(`sheets-pivot-ui.searchFieldPlaceholder`),slot:/* @__PURE__ */ (0,d.jsx)(u.SearchIcon,{}),onChange:o,size:`small`}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-rounded-xl univer-bg-gray-50 univer-p-1 dark:!univer-bg-gray-800`,children:l.length?l.map(t=>/* @__PURE__ */ (0,d.jsx)(pn,{...e,field:t},t.id)):/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-p-4 univer-text-center univer-text-sm univer-text-gray-500`,children:r.t(`sheets-pivot-ui.noMatchingFields`)})})]})}function hn(e){var t;let{sourceRangeInfo:r,positionInfo:a,pivotTableId:o,refresh:s}=e,c=(0,i.useDependency)(n.ICommandService),l=(0,i.useDependency)(n.ILogService),u=(0,i.useDependency)(n.LocaleService),h=(0,i.useDependency)(n.IUniverInstanceService).getUnit(r.unitId),g=(h==null||(t=h.getSheetBySheetId(r.subUnitId))==null?void 0:t.getName())??r.sheetName,[_,v]=(0,f.useState)(r.range),[y,b]=(0,f.useState)(g),[x,S]=(0,f.useState)(``);(0,f.useEffect)(()=>{v(r.range),b(g)},[g,r]);async function C(e){let t=e.split(`,`).filter(Boolean).map(p.deserializeRangeWithSheet)[0];if(!t||!h){S(u.t(`sheets-pivot-ui.msg.emptySourceRange`));return}let n=t.sheetName||r.sheetName,i=h.getSheetBySheetName(n),l=yt(i,t.range);if(l!==2||!i){let e=l===0?`sheets-pivot-ui.msg.emptySourceRange`:`sheets-pivot-ui.msg.invalidSize`;S(u.t(e))}else S(``),v(t.range),b(n),await xt(c,o,{unitId:r.unitId,subUnitId:i.getSheetId(),range:t.range,sheetName:n},a.unitId,a.subUnitId),await s()}return/* @__PURE__ */ (0,d.jsxs)(`section`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:u.t(`sheets-pivot-ui.sourceRangeLabel`)}),/* @__PURE__ */ (0,d.jsx)(m.MobileRangeSelector,{unitId:r.unitId,subUnitId:r.subUnitId,initialValue:(0,p.serializeRangeWithSheet)(y,_),maxRangeCount:1,keepSheetReference:!0,supportAcrossSheet:!0,onChange:(e,t)=>C(t).catch(e=>{l.error(`[MobilePivotSourceRange]`,e)})}),x&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-2 univer-text-xs univer-text-red-500`,children:x})]})}function gn(e){let{pivotTableId:r,version:a,unitId:s,subUnitId:c,panelInfo:l}=e,u=(0,i.useDependency)(t.SheetsPivotTableConfigModel),p=(0,i.useDependency)(t.SheetsPivotRPCService),m=(0,i.useDependency)(n.ILogService),h=(0,f.useMemo)(()=>u.watchPivotTableChanges(s,c,r).pipe((0,o.debounceTime)(0)),[u,s,c,r]),g=(0,i.useObservable)(h),[_,v]=(0,f.useState)(l),y=(0,f.useCallback)(async()=>{if(!u.getPivotTableConfig(s,c,r))return;let e=await p.getPivotDisplayConfig({unitId:s,subUnitId:c,pivotTableId:r,params:{type:t.PivotDisplayDataType.Panel,panel:{pivotTableId:r}}});e.result.panel&&v(e.result.panel)},[u,r,p,c,s]);return(0,f.useEffect)(()=>{y().catch(e=>m.error(`[MobilePivotPanel]`,e))},[g,m,y,a]),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-5 univer-py-4`,children:[/* @__PURE__ */ (0,d.jsx)(hn,{sourceRangeInfo:_.sourceRange,positionInfo:_.positionInfo,pivotTableId:r,refresh:y}),/* @__PURE__ */ (0,d.jsx)(mn,{info:_,pivotTableId:r,unitId:s,subUnitId:c,refresh:y}),/* @__PURE__ */ (0,d.jsx)(un,{info:_,pivotTableId:r,unitId:s,subUnitId:c,refresh:y})]})}function _n(e){let{className:t,unitId:r,subUnitId:a,pivotTableId:o}=e,[c,l]=(0,f.useState)(`fields`),u=(0,i.useDependency)(n.LocaleService);return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:(0,s.clsx)(`univer-flex univer-size-full univer-min-h-0 univer-flex-col`,t),dir:u.getDirection(),children:[/* @__PURE__ */ (0,d.jsx)(s.Segmented,{className:`univer-sticky univer-top-0 univer-z-10 univer-my-3 univer-w-full univer-shrink-0`,items:[{label:u.t(`sheets-pivot-ui.fieldPanel`),value:`fields`},{label:u.t(`sheets-pivot-ui.configPanel`),value:`config`}],value:c,onChange:l}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-min-h-0 univer-flex-1 univer-overflow-y-auto`,children:c===`fields`?/* @__PURE__ */ (0,d.jsx)(gn,{...e}):/* @__PURE__ */ (0,d.jsx)($t,{unitId:r,subUnitId:a,pivotTableId:o})})]})}function vn(e,t){switch(e){case 1:return t.t(`sheets-pivot-ui.msg.invalidSize`);case 0:return t.t(`sheets-pivot-ui.msg.emptySourceRange`);default:return``}}function yn(e){let{unitId:r,subUnitId:a,sourceRange:o,positionRange:c,sourceSheetName:l,positionSheetName:u,onConfirm:h,onCancel:g}=e,_=(0,i.useDependency)(n.LocaleService),v=(0,i.useDependency)(n.IUniverInstanceService).getUnit(r),[y,b]=(0,f.useState)(o),[x,S]=(0,f.useState)(c),[C,w]=(0,f.useState)(l),[T,E]=(0,f.useState)(u),[D,O]=(0,f.useState)(t.PositionType.New),[k,A]=(0,f.useState)(``),[j,M]=(0,f.useState)(!0);(0,f.useEffect)(()=>{let e=yt(v.getSheetBySheetName(C),y);A(vn(e,_))},[_,y,C,v]);function N(e){let t=e.split(`,`).filter(Boolean).map(p.deserializeRangeWithSheet)[0];t?(b(t.range),w(t.sheetName||C)):A(_.t(`sheets-pivot-ui.msg.emptySourceRange`))}function P(e){let t=e.split(`,`).filter(Boolean).map(p.deserializeRangeWithSheet)[0];if(!t){S(null);return}let r=t.sheetName||l;S(t.range),E(r),M(r!==C||!n.Rectangle.intersects(t.range,y))}function F(){if(k)return;let e;if(D===t.PositionType.Existing){if(!x||!j)return;e={sourceRange:y,sourceSheetName:C,positionRange:x,positionSheetName:T,positionType:D}}else e={sourceRange:y,sourceSheetName:C,positionType:D};h(e)}return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-5`,dir:_.getDirection(),children:[/* @__PURE__ */ (0,d.jsxs)(`section`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:_.t(`sheets-pivot-ui.dialog.source.sourceData`)}),/* @__PURE__ */ (0,d.jsx)(m.MobileRangeSelector,{unitId:r,subUnitId:a,initialValue:(0,p.serializeRangeWithSheet)(C,y),maxRangeCount:1,supportAcrossSheet:!0,keepSheetReference:!0,onChange:(e,t)=>N(t)}),k&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-2 univer-text-xs univer-text-red-500`,children:k})]}),/* @__PURE__ */ (0,d.jsxs)(`section`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:_.t(`sheets-pivot-ui.dialog.source.position`)}),/* @__PURE__ */ (0,d.jsxs)(s.RadioGroup,{className:`univer-flex univer-flex-col univer-gap-3`,value:D,onChange:e=>O(e),children:[/* @__PURE__ */ (0,d.jsx)(s.Radio,{value:t.PositionType.New,children:_.t(`sheets-pivot-ui.dialog.source.newSheet`)}),/* @__PURE__ */ (0,d.jsx)(s.Radio,{value:t.PositionType.Existing,children:_.t(`sheets-pivot-ui.dialog.source.existingSheet`)})]}),D===t.PositionType.Existing&&/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mt-3`,children:[/* @__PURE__ */ (0,d.jsx)(m.MobileRangeSelector,{unitId:r,subUnitId:a,initialValue:x?(0,p.serializeRangeWithSheet)(T,x):``,maxRangeCount:1,supportAcrossSheet:!0,keepSheetReference:!0,onChange:(e,t)=>P(t)}),!j&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-2 univer-text-xs univer-text-red-500`,children:_.t(`sheets-pivot-ui.dialog.source.intersect`)})]})]}),/* @__PURE__ */ (0,d.jsxs)(s.MobileActionRowGroup,{children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:g,size:`middle`,children:_.t(`sheets-pivot-ui.dialog.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,onClick:F,size:`middle`,children:_.t(`sheets-pivot-ui.dialog.ok`)})]})]})}let bn=class extends n.Disposable{constructor(e,t){super(),[[j,yn],[v,_n],[b,Jt],[S,qt],[w,Kt],[M,ut]].forEach(([t,n])=>{this.disposeWithMe(e.register(t,n))}),this.disposeWithMe(t.register({PivotTableIcon:u.PivotTableIcon}))}};bn=q([K(0,(0,n.Inject)(i.ComponentManager)),K(1,(0,n.Inject)(i.IconManager))],bn);function xn(e){return{id:P.id,commandId:P.id,type:i.MenuItemType.BUTTON,icon:`PivotTableIcon`,title:`sheets-pivot-ui.title`,tooltip:`sheets-pivot-ui.title`,hidden$:(0,i.getMenuHiddenObservable)(e,n.UniverInstanceType.UNIVER_SHEET),disabled$:(0,a.getObservableWithExclusiveRange$)(e,(0,a.getCurrentRangeDisable$)(e,{workbookTypes:[r.WorkbookEditablePermission],worksheetTypes:[r.WorksheetViewPermission,r.WorksheetPivotTablePermission],rangeTypes:[r.RangeProtectionPermissionViewPoint]}))}}const Sn={[i.RibbonDataGroup.ORGANIZATION]:{[P.id]:{order:1,gridLayout:{row:2,column:1},menuItemFactory:xn}},[i.ContextMenuPosition.MAIN_AREA]:{[i.ContextMenuGroup.DATA]:{[P.id]:{order:1,menuItemFactory:xn}}}};let Cn=class extends n.Disposable{constructor(e){super(),e.mergeMenu(Sn)}};Cn=q([K(0,i.IMenuManagerService)],Cn);let wn=class extends n.Disposable{constructor(e,r,i,a){super(),this._sidebarService=i,this._dialogService=a,G(this,`_pivotTableId`,null),this.disposeWithMe(e.onCommandExecuted(e=>{e.id===it.id?this._pivotTableId=e.params.pivotTableId:e.id===t.RemovePivotTableMutation.id&&e.params.pivotTableId===this._pivotTableId&&this._closePanel()}));let s=r.getCurrentUnitOfType(n.UniverInstanceType.UNIVER_SHEET);s&&(this.disposeWithMe(s.activeSheet$.pipe((0,o.skip)(1)).subscribe(()=>this._closePanel())),this.disposeWithMe(s.sheetDisposed$.subscribe(()=>this._closePanel()))),this.disposeWithMe(this._sidebarService.sidebarOptions$.subscribe(e=>{e.id===`UNIVER_SHEET_PIVOT_PANEL_ID`&&e.visible===!1&&(this._pivotTableId=null,this._closeChildDialogs())}))}_closePanel(){this._pivotTableId=null,this._sidebarService.close(y),this._closeChildDialogs()}_closeChildDialogs(){this._dialogService.close(x),this._dialogService.close(C),this._dialogService.close(T)}};wn=q([K(0,n.ICommandService),K(1,n.IUniverInstanceService),K(2,i.ISidebarService),K(3,i.IDialogService)],wn);var X=class{static drawExpand(e,t,n,r,i){let a=(i-t)/2;e.save(),e.fillStyle=r,e.strokeStyle=n,e.beginPath(),e.moveTo(6,a),e.lineTo(4+t-2,a),e.arcTo(4+t,a,4+t,a+2,2),e.lineTo(4+t,a+t-2),e.arcTo(4+t,a+t,4+t-2,a+t,2),e.lineTo(6,a+t),e.arcTo(4,a+t,4,a+t-2,2),e.lineTo(4,a+2),e.arcTo(4,a,6,a,2),e.closePath(),e.stroke(),e.fill(),e.beginPath(),e.moveTo(4+t/2,a+2),e.lineTo(4+t/2,a+t-2),e.moveTo(6,a+t/2),e.lineTo(4+t-2,a+t/2),e.stroke(),e.restore()}static drawCollapse(e,t,n,r,i){let a=(i-t)/2;e.save(),e.fillStyle=r,e.strokeStyle=n,e.beginPath(),e.moveTo(6,a),e.lineTo(4+t-2,a),e.arcTo(4+t,a,4+t,a+2,2),e.lineTo(4+t,a+t-2),e.arcTo(4+t,a+t,4+t-2,a+t,2),e.lineTo(6,a+t),e.arcTo(4,a+t,4,a+t-2,2),e.lineTo(4,a+2),e.arcTo(4,a,6,a,2),e.closePath(),e.fill(),e.stroke(),e.beginPath(),e.moveTo(6,a+t/2),e.lineTo(4+t-2,a+t/2),e.stroke(),e.restore()}static drawFilterIcon(e,t,n,r){l.Rect.drawWith(e,{radius:2,width:16,height:16,fill:r}),(0,h.drawCanvasIcon)(e,t,0,0,16,n)}};const Tn=/* @__PURE__ */ new Set([e.PivotCellStyleTypeEnum.FilterAll,e.PivotCellStyleTypeEnum.FilterPartial,e.PivotCellStyleTypeEnum.FilterSingle,e.PivotCellStyleTypeEnum.FilterNoneSortNone,e.PivotCellStyleTypeEnum.FilteredSortAsc,e.PivotCellStyleTypeEnum.FilteredSortDesc,e.PivotCellStyleTypeEnum.FilteredSortNone,e.PivotCellStyleTypeEnum.FilterNoneSortAsc,e.PivotCellStyleTypeEnum.FilterNoneSortDesc]),En=/* @__PURE__ */ new Set([e.PivotCellStyleTypeEnum.Expand,e.PivotCellStyleTypeEnum.Collapse]);let Dn=class extends l.Shape{constructor(e,t,n,r){super(e,t),this._commandService=n,this._themeService=r,G(this,`_cellWidth`,0),G(this,`_cellHeight`,0),G(this,`_hovered`,!1),G(this,`_pivotParams`,void 0),this.setShapeProps(t),this.onPointerDown$.subscribeEvent(e=>this.onPointerDown(e)),this.onPointerEnter$.subscribeEvent(()=>this.onPointerEnter()),this.onPointerLeave$.subscribeEvent(()=>this.onPointerLeave())}setShapeProps(e){e.cellHeight!==void 0&&(this._cellHeight=e.cellHeight),e.cellWidth!==void 0&&(this._cellWidth=e.cellWidth),e.pivotParams!==void 0&&(this._pivotParams=e.pivotParams),this.transformByState({width:e.width,height:e.height})}onPointerDown(n){if(n.button===2)return;let{col:r,row:i,unitId:a,subUnitId:o,buttonType:s}=this._pivotParams;Tn.has(s)?setTimeout(()=>{this._commandService.executeCommand(tt.id,{row:i,col:r,unitId:a,subUnitId:o})},200):this._commandService.executeCommand(t.SetPivotCollapseCommand.id,{row:i,col:r,unitId:a,subUnitId:o,collapse:s===e.PivotCellStyleTypeEnum.Expand})}onPointerEnter(){this._hovered=!0,this.setCursor(l.CURSOR_TYPE.POINTER),this.makeDirty(!0)}onPointerLeave(){this._hovered=!1,this.setCursor(l.CURSOR_TYPE.DEFAULT),this.makeDirty(!0)}_draw(t){var n,r;if(!((n=this._pivotParams)!=null&&n.buttonType))return;let i=this._themeService.getColorFromTheme(`primary.600`),a=this._themeService.getColorFromTheme(`gray.50`);t.save();let o=new Path2D;o.rect(0,0,this._cellWidth,this._cellHeight),t.clip(o),((r=this._pivotParams)==null?void 0:r.buttonType)===e.PivotCellStyleTypeEnum.Collapse?X.drawExpand(t,12,i,a,this._cellHeight):this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.Expand?X.drawCollapse(t,12,i,a,this._cellHeight):this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilterAll||this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilterNoneSortNone?X.drawFilterIcon(t,`filter-options-icon`,i,a):this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilterPartial||this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilterSingle||this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilteredSortNone?X.drawFilterIcon(t,`filter-applied-icon`,i,a):this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilterNoneSortAsc?X.drawFilterIcon(t,`ascending-icon`,i,a):this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilterNoneSortDesc?X.drawFilterIcon(t,`descending-icon`,i,a):this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilteredSortAsc?X.drawFilterIcon(t,`filter-sort-ascending-icon`,i,a):this._pivotParams.buttonType===e.PivotCellStyleTypeEnum.FilteredSortDesc&&X.drawFilterIcon(t,`filter-sort-descending-icon`,i,a),t.restore()}};Dn=q([K(2,n.ICommandService),K(3,(0,n.Inject)(n.ThemeService))],Dn);let On=class extends l.Shape{constructor(e,t,n,r){super(e,t),this._commandService=n,this._themeService=r,G(this,`_cellWidth`,void 0),G(this,`_cellHeight`,void 0),G(this,`_pivotParams`,void 0),this._cellWidth=t.cellWidth,this._cellHeight=t.cellHeight,this._pivotParams=t.pivotParams,this.transformByState({width:t.width,height:t.height}),this.onPointerUp$.subscribeEvent(e=>this._handlePointerUp(e))}_handlePointerUp(n){if(n.button===2)return;let{col:r,row:i,unitId:a,subUnitId:o,buttonType:s}=this._pivotParams;if(Tn.has(s))return this._commandService.executeCommand(tt.id,{row:i,col:r,unitId:a,subUnitId:o});if(En.has(s))return this._commandService.executeCommand(t.SetPivotCollapseCommand.id,{row:i,col:r,unitId:a,subUnitId:o,collapse:s===e.PivotCellStyleTypeEnum.Expand})}_draw(t){let n=this._pivotParams.buttonType,r=this._themeService.getColorFromTheme(`primary.600`),i=this._themeService.getColorFromTheme(`gray.50`);t.save();let a=new Path2D;a.rect(0,0,this._cellWidth,this._cellHeight),t.clip(a),t.translate(6,6),n===e.PivotCellStyleTypeEnum.Collapse?X.drawExpand(t,12,r,i,this._cellHeight):n===e.PivotCellStyleTypeEnum.Expand?X.drawCollapse(t,12,r,i,this._cellHeight):n===e.PivotCellStyleTypeEnum.FilterAll||n===e.PivotCellStyleTypeEnum.FilterNoneSortNone?X.drawFilterIcon(t,`filter-options-icon`,r,i):n===e.PivotCellStyleTypeEnum.FilterPartial||n===e.PivotCellStyleTypeEnum.FilterSingle||n===e.PivotCellStyleTypeEnum.FilteredSortNone?X.drawFilterIcon(t,`filter-applied-icon`,r,i):n===e.PivotCellStyleTypeEnum.FilterNoneSortAsc?X.drawFilterIcon(t,`ascending-icon`,r,i):n===e.PivotCellStyleTypeEnum.FilterNoneSortDesc?X.drawFilterIcon(t,`descending-icon`,r,i):n===e.PivotCellStyleTypeEnum.FilteredSortAsc?X.drawFilterIcon(t,`filter-sort-ascending-icon`,r,i):n===e.PivotCellStyleTypeEnum.FilteredSortDesc&&X.drawFilterIcon(t,`filter-sort-descending-icon`,r,i),t.restore()}};On=q([K(2,n.ICommandService),K(3,(0,n.Inject)(n.ThemeService))],On);function kn(e,t,n){let{startX:r,startY:i,endX:a,endY:o}=n;return{left:(t===`filter`?e!==`rtl`:e===`rtl`)?a-16-1:r+1,top:t===`filter`?o-16-1:i+1}}let An=class extends n.Disposable{constructor(e,t,n,r,i,a,o,s,c,l){super(),this._context=e,this._injector=t,this._adaptorModel=n,this._skeletonManagerService=r,this._configModel=i,this._permissionController=a,this._permissionService=o,this._localeService=s,this._dialogService=c,this._contextMenuController=l,G(this,`_buttonShapes`,[]),this._initContextMenu(),this._initButtonRender(),this._initSkeleton()}dispose(){this._clearButtons(),super.dispose()}_clearButtons(){this._buttonShapes.forEach(e=>e.dispose()),this._buttonShapes.length=0}_initContextMenu(){if(!this._contextMenuController)return;let e=this._contextMenuController.contextMenuInterceptor.intercept(a.MOBILE_CONTEXT_MENU,{handler:(e,t,n)=>{if(e||t.menuPosition!==i.ContextMenuPosition.MAIN_AREA)return n(e);let{col:r,row:a,subUnitId:o,unitId:s}=t;if(r==null||a==null)return n(e);let c=this._adaptorModel.getPivotTableIdByCell(s,o,a,r);return c?(this._openPivotActionPanel(s,o,c,a,r),!0):n(e)}});this.disposeWithMe((0,n.toDisposable)(e))}_openPivotActionPanel(e,t,n,i,a){let o=this._permissionController.permissionCheckWithRanges({workbookTypes:[r.WorkbookEditablePermission],worksheetTypes:[r.WorksheetPivotTablePermission],rangeTypes:[r.RangeProtectionPermissionEditPoint]},[{startRow:i,endRow:i,startColumn:a,endColumn:a}],e,t);this._dialogService.open({id:N,title:{title:this._localeService.t(`sheets-pivot-ui.title`)},children:{label:{name:M,props:{editable:o,pivotTableId:n,subUnitId:t,unitId:e}}},onClose:()=>this._dialogService.close(N)})}_initButtonRender(){this.disposeWithMe((0,o.merge)(this._adaptorModel.viewUpdate$.pipe((0,o.auditTime)(0)),this._skeletonManagerService.currentSkeleton$,this._configModel.pivotConfigChange$,this._permissionService.permissionPointUpdate$.pipe((0,o.auditTime)(100))).subscribe(()=>{let e=this._context.unit,t=this._skeletonManagerService.getCurrentSkeleton();if(!e||!t)return;let n=e.getActiveSheet(),i=e.getUnitId(),o=n.getSheetId(),s=this._localeService.getDirection(),{scene:c}=this._context;this._clearButtons(),this._adaptorModel.getSubUnitPivotMatrix(i,o).forValue((e,n,l)=>{if(!(l!=null&&l.pbt))return;let{startX:u,startY:d,endX:f,endY:p}=(0,a.getCoordByCell)(e,n,c,t),m=f-u,h=p-d;if(h<8||m<8||!this._permissionController.permissionCheckWithRanges({workbookTypes:[r.WorkbookEditablePermission],worksheetTypes:[r.WorksheetViewPermission,r.WorksheetEditPermission,r.WorksheetPivotTablePermission],rangeTypes:[r.RangeProtectionPermissionViewPoint,r.RangeProtectionPermissionEditPoint]},[{startRow:e,endRow:e,startColumn:n,endColumn:n}],i,o))return;let g;if(Tn.has(l.pbt))g=`filter`;else if(En.has(l.pbt))g=`collapse`;else return;let _=kn(s,g,{startX:u,startY:d,endX:f,endY:p}),v={left:_.left-6,top:_.top-6,width:28,height:28,zIndex:5e3,cellWidth:m,cellHeight:h,pivotParams:{row:e,col:n,unitId:i,subUnitId:o,buttonType:l.pbt}},y=`sheets-mobile-pivot-button-${e}-${n}-${i}-${o}`;this._buttonShapes.push(this._injector.createInstance(On,y,v))}),this._buttonShapes.length&&c.addObjects(this._buttonShapes),c.makeDirty()}))}_initSkeleton(){var e;this.disposeWithMe(this._adaptorModel.markDirty$.subscribe(({unitId:e,subUnitId:t})=>{if(e===this._context.unitId&&(this._skeletonManagerService.makeDirty({sheetId:t}),this._context.unit.getActiveSheet().getSheetId()===t)){var n;this._skeletonManagerService.reCalculate(),(n=this._context.mainComponent)==null||n.makeDirty()}}));let t=this._context.unitId;this._context.unit.getSheets().forEach(e=>{var n;let r=e.getSheetId();(n=this._adaptorModel.getSubUnitPivotRenderCache(t,r))!=null&&n.size&&this._skeletonManagerService.makeDirty({sheetId:r})}),this._skeletonManagerService.reCalculate(),(e=this._context.mainComponent)==null||e.makeDirty()}};An=q([K(1,(0,n.Inject)(n.Injector)),K(2,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),K(3,(0,n.Inject)(a.SheetSkeletonManagerService)),K(4,(0,n.Inject)(t.SheetsPivotTableConfigModel)),K(5,(0,n.Inject)(r.SheetPermissionCheckController)),K(6,(0,n.Inject)(n.IPermissionService)),K(7,(0,n.Inject)(n.LocaleService)),K(8,i.IDialogService),K(9,(0,n.Optional)(a.SheetContextMenuMobileRenderController))],An);let jn=class extends n.Disposable{constructor(e,t,n){super(),this._univerInstanceService=e,this._sheetsPivotTableAdaptorModel=t,this._sheetClipboardService=n,this._initialize()}_initialize(){let e=this,r,i;this.disposeWithMe(this._sheetClipboardService.addClipboardHook({id:t.SHEET_PIVOT_TABLE_PLUGIN,onBeforeCopy(t,a){let o=e._univerInstanceService.getUnit(t,n.UniverInstanceType.UNIVER_SHEET);o&&(r=e._sheetsPivotTableAdaptorModel.getSubUnitPivotMatrix(t,a),i=o.getStyles())},onAfterCopy(){r=null,i=null},getCellValueBySpecialMatrix(e,t){if(!r)return;let n=r==null?void 0:r.getValue(e,t);if(n){var a;return{...n,s:i==null?void 0:i.get(n.s),displayV:((a=n.v)==null?void 0:a.toString())||``}}}}))}};jn=q([K(0,(0,n.Inject)(n.IUniverInstanceService)),K(1,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),K(2,a.ISheetClipboardService)],jn);const Mn=[r.InsertRowCommand.id,r.RemoveColCommand.id,r.RemoveRowCommand.id,r.InsertColCommand.id];let Nn=class extends n.Disposable{constructor(e,n,r,i,a,o,s,c,l){var u;super(),this._injector=e,this._univerInstanceService=n,this._sheetsPivotTableAdaptorModel=r,this._sheetInterceptorService=i,this._confirmService=a,this._localeService=o,this._sheetsPivotTableConfigModel=s,this._messageService=c,this._configService=l,G(this,`_defaultOverride`,!1),this._initMoveRangeConfirmCheck(),this._initRowColOperationCheck(),this._defaultOverride=((u=this._configService.getConfig(t.SHEETS_PIVOT_PLUGIN_CONFIG_KEY))==null?void 0:u.defaultOverride)??!1}_initMoveRangeConfirmCheck(){this.disposeWithMe(this._sheetInterceptorService.interceptBeforeCommand({performCheck:async e=>{if(e.id===r.MoveRangeCommand.id){let n=(0,r.getSheetCommandTarget)(this._univerInstanceService);if(!n)return Promise.resolve(!1);let{unitId:i,subUnitId:a}=n,{fromRange:o,toRange:s}=e.params,c=this._sheetsPivotTableConfigModel.getSubUnitPivotConfigs(i,a);if(!(c!=null&&c.size))return Promise.resolve(!0);for(let[e,n]of c){let{targetCellInfo:r}=n,{row:c,col:l}=r,u=c-o.startRow,d=l-o.startColumn,f={row:s.startRow+u,col:s.startColumn+d},p=f.row-r.row,m=f.col-r.col,h=this._sheetsPivotTableAdaptorModel.getPivotItemRenderInfoCache(i,a,e),g=h==null?void 0:h.rangesInfo;if(!g)return Promise.resolve(!0);let _=[];Object.keys(g).forEach(e=>{g[e].forEach(e=>{let t={...e};t.startRow=e.startRow+p,t.endRow=e.endRow+p,t.startColumn=e.startColumn+m,t.endColumn=e.endColumn+m,_.push(t)})});let v=this._sheetInterceptorService.generateMutationsByRanges({unitId:i,subUnitId:a,ranges:_}),y=(0,t.generateSetRangeValueMutations)(this._injector,i,a,_);if(v.redos.length||y.shouldClear){if(this._defaultOverride)return Promise.resolve(!0);{let e=await this._confirmService.confirm({id:`sheet-pivot.confirm.add-sheet-pivot`,title:{title:this._localeService.t(`sheets-pivot-ui.perform.tips`)},children:{title:this._localeService.t(`sheets-pivot-ui.perform.info`)},cancelText:this._localeService.t(`sheets-pivot-ui.perform.cancel`),confirmText:this._localeService.t(`sheets-pivot-ui.perform.confirm`)});return Promise.resolve(e)}}}}return Promise.resolve(!0)}}))}_initRowColOperationCheck(){this.disposeWithMe(this._sheetInterceptorService.interceptBeforeCommand({performCheck:async e=>{if(Mn.includes(e.id)){let t=e.params,{range:i}=t,a=(0,r.getSheetCommandTarget)(this._univerInstanceService),o=t.unitId||(a==null?void 0:a.unitId),c=t.subUnitId||(a==null?void 0:a.subUnitId);if(!o||!c)return Promise.resolve(!0);let l=this._sheetsPivotTableConfigModel.getSubUnitPivotConfigs(o,c);if(l!=null&&l.size)for(let[e,t]of l){let t=this._sheetsPivotTableAdaptorModel.getPivotItemRenderInfoCache(o,c,e);if(t!=null&&t.rangesInfo){for(let e of Object.keys(t.rangesInfo))if(t.rangesInfo[e].some(e=>n.Rectangle.intersects(e,i)))return this._messageService.show({type:s.MessageType.Error,content:this._localeService.t(`sheets-pivot-ui.perform.rowColError`)}),Promise.resolve(!1)}}}return Promise.resolve(!0)}}))}};Nn=q([K(0,(0,n.Inject)(n.Injector)),K(1,(0,n.Inject)(n.IUniverInstanceService)),K(2,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),K(3,(0,n.Inject)(r.SheetInterceptorService)),K(4,(0,n.Inject)(n.IConfirmService)),K(5,(0,n.Inject)(n.LocaleService)),K(6,(0,n.Inject)(t.SheetsPivotTableConfigModel)),K(7,(0,n.Inject)(i.IMessageService)),K(8,(0,n.Inject)(n.IConfigService))],Nn);const Pn=[g.InsertTextCommand.id,_.IMEInputCommand.id,a.SetCellEditVisibleOperation.id];let Fn=class extends n.Disposable{constructor(e,t,n,r,i,a){super(),this._sheetsSelectionsService=e,this._univerInstanceService=t,this._sheetsPivotTableAdaptorModel=n,this._commandService=r,this._sheetsPivotTableConfigModel=i,this._localeService=a,this._initEditListener()}_initEditListener(){this.disposeWithMe(this._commandService.beforeCommandExecuted(e=>{if(Pn.includes(e.id)){var t;let i=(t=this._sheetsSelectionsService.getCurrentLastSelection())==null?void 0:t.primary;if(!i)return;let{actualRow:a,actualColumn:o}=i,s=(0,n.cellToRange)(a,o),c=(0,r.getSheetCommandTarget)(this._univerInstanceService,e.params);if(!c)return;let{unitId:l,subUnitId:u}=c,d=this._sheetsPivotTableConfigModel.getSubUnitPivotConfigs(l,u);if(d!=null&&d.size)for(let[e]of d){let t=this._sheetsPivotTableAdaptorModel.getPivotItemRenderInfoCache(l,u,e);if(t!=null&&t.rangesInfo){for(let e of Object.keys(t.rangesInfo))if(t.rangesInfo[e].some(e=>n.Rectangle.intersects(e,s)))throw new n.CustomCommandExecutionError(this._localeService.t(`sheets-pivot-ui.msg.editCellError`))}}}}))}};Fn=q([K(0,(0,n.Inject)(r.SheetsSelectionsService)),K(1,(0,n.Inject)(n.IUniverInstanceService)),K(2,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),K(3,(0,n.Inject)(n.ICommandService)),K(4,(0,n.Inject)(t.SheetsPivotTableConfigModel)),K(5,(0,n.Inject)(n.LocaleService))],Fn);let In=class extends n.Disposable{constructor(e,t,n,r){super(),this._sheetsPivotTableAdaptorModel=e,this._formatPainterService=t,this._messageService=n,this._localeService=r,this._initFormatPainterListener()}_initFormatPainterListener(){let e=this._sheetsPivotTableAdaptorModel,t=this._messageService,n=this._localeService;this._formatPainterService.addHook({id:`pivot-format-painter-hook`,onBeforeApply(i){let{redoMutationsInfo:a}=i;for(let i of a)if(i.id===r.AddWorksheetMergeMutation.id){let{unitId:r,subUnitId:a,ranges:o}=i.params;for(let i of o)if(e.getIntersectsPivotTable(r,a,i).length>0)return t.show({type:s.MessageType.Error,content:n.t(`sheets-pivot-ui.msg.mergeCellError`)}),!1}return!0}})}};In=q([K(0,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),K(1,(0,n.Inject)(a.IFormatPainterService)),K(2,(0,n.Inject)(i.IMessageService)),K(3,(0,n.Inject)(n.LocaleService))],In);let Ln=class extends n.Disposable{constructor(e,t,n){super(),this._sheetPrintInterceptorService=e,this._sheetsPivotTableAdaptorModel=t,this._sheetsPivotTableConfigModel=n,this._initPrintArea()}_initPrintArea(){this.disposeWithMe(this._sheetPrintInterceptorService.interceptor.intercept(this._sheetPrintInterceptorService.interceptor.getInterceptPoints().PRINTING_RANGE,{handler:(e,t,r)=>{let{unitId:i,subUnitId:a}=t,o=(e==null?void 0:e.startRow)??1/0,s=(e==null?void 0:e.endRow)??-1/0,c=(e==null?void 0:e.startColumn)??1/0,l=(e==null?void 0:e.endColumn)??-1/0,u=this._sheetsPivotTableConfigModel.getSubUnitPivotConfigs(i,a);if(!u)return r(e);for(let[e,t]of u){var d;let t=(d=this._sheetsPivotTableAdaptorModel.getPivotItemRenderInfoCache(i,a,e))==null?void 0:d.rangesInfo;t&&Object.values(t).forEach(e=>{e.forEach(e=>{let{startRow:t,endRow:n,startColumn:r,endColumn:i}=e;o=Math.min(t,o),s=Math.max(n,s),c=Math.min(r,c),l=Math.max(i,l)})})}let f={startRow:o,endRow:s,startColumn:c,endColumn:l};return(0,n.isValidRange)(f)?r(f):r(e)}}))}};Ln=q([K(0,(0,n.Inject)(a.SheetPrintInterceptorService)),K(1,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),K(2,(0,n.Inject)(t.SheetsPivotTableConfigModel))],Ln);let Rn=class extends n.Disposable{constructor(e,t,n){super(),this._mergeCellController=e,this._univerInstanceService=t,this._sheetsPivotTableConfigModel=n,this._initMenuInterceptor()}_initMenuInterceptor(){this.disposeWithMe(this._mergeCellController.interceptor.intercept(this._mergeCellController.interceptor.getInterceptPoints().MERGE_CELL_INTERCEPTOR_CHECK,{handler:(e,t)=>{let i=(0,r.getSheetCommandTarget)(this._univerInstanceService);if(!i)return e;let{unitId:a,subUnitId:o}=i,s=this._sheetsPivotTableConfigModel.getSubUnitPivotConfigs(a,o),c=[];return s&&s.forEach((e,t)=>{var n;let r=(n=this._sheetsPivotTableConfigModel.getPivotTableRangeInfo(a,o,t))==null?void 0:n.rangeInfo;r&&Object.keys(r).forEach(e=>{let t=r[e];Array.isArray(t)&&c.push(...t)})}),c.length&&t.some(e=>c.some(t=>n.Rectangle.intersects(t,e)))?!0:e}}))}};Rn=q([K(0,(0,n.Inject)(r.MergeCellController)),K(1,(0,n.Inject)(n.IUniverInstanceService)),K(2,(0,n.Inject)(t.SheetsPivotTableConfigModel))],Rn);let Z=class extends n.Plugin{constructor(e=ot,t,r,i,a){super(),this._config=e,this._injector=t,this._commandService=r,this._renderManagerService=i,this._configService=a;let{menu:o,...s}=(0,n.merge)({},ot,this._config);o&&this._configService.setConfig(`menu`,o,{merge:!0}),this._configService.setConfig(at,s),this._registerCommands()}onStarting(){[[bn],[Cn],[wn],[Rn],[Nn],[Ln],[Fn],[In],[J],[jn]].forEach(e=>this._injector.add(e)),(0,n.touchDependencies)(this._injector,[[bn],[Cn],[wn],[J]])}onRendered(){this._registerRenderModules(),(0,n.touchDependencies)(this._injector,[[Nn],[Ln],[Fn],[In],[jn]])}onSteady(){(0,n.touchDependencies)(this._injector,[[Rn]])}_registerCommands(){[it,P,tt,nt,I].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}_registerRenderModules(){this.disposeWithMe(this._renderManagerService.registerRenderModule(n.UniverInstanceType.UNIVER_SHEET,[An]))}};G(Z,`pluginName`,`${t.SHEET_PIVOT_TABLE_PLUGIN}_UI_PLUGIN`),G(Z,`packageName`,$e),G(Z,`version`,et),G(Z,`type`,n.UniverInstanceType.UNIVER_SHEET),Z=q([(0,n.DependentOn)(c.UniverLicensePlugin,l.UniverRenderEnginePlugin,r.UniverSheetsPlugin,t.UniverSheetsPivotTablePlugin,a.UniverSheetsMobileUIPlugin,i.UniverMobileUIPlugin),K(1,(0,n.Inject)(n.Injector)),K(2,n.ICommandService),K(3,l.IRenderManagerService),K(4,n.IConfigService)],Z);const zn=[e.PivotExcelDateGroupUnit.Year,e.PivotExcelDateGroupUnit.Quarter,e.PivotExcelDateGroupUnit.Month,e.PivotExcelDateGroupUnit.Day,e.PivotExcelDateGroupUnit.Hour,e.PivotExcelDateGroupUnit.Minute,e.PivotExcelDateGroupUnit.Second],Bn={[e.PivotExcelDateGroupUnit.Year]:`sheets-pivot-ui.dateGroup.year`,[e.PivotExcelDateGroupUnit.Quarter]:`sheets-pivot-ui.dateGroup.quarter`,[e.PivotExcelDateGroupUnit.Month]:`sheets-pivot-ui.dateGroup.month`,[e.PivotExcelDateGroupUnit.Day]:`sheets-pivot-ui.dateGroup.day`,[e.PivotExcelDateGroupUnit.Hour]:`sheets-pivot-ui.dateGroup.hour`,[e.PivotExcelDateGroupUnit.Minute]:`sheets-pivot-ui.dateGroup.minute`,[e.PivotExcelDateGroupUnit.Second]:`sheets-pivot-ui.dateGroup.second`},Vn=`univer-h-8 univer-rounded-md univer-border univer-border-gray-300 univer-bg-gray-0 univer-px-2 univer-text-sm univer-text-gray-900 dark:univer-bg-gray-700 dark:univer-text-gray-0`;function Hn({targetFieldName:t,dateSystem:r,grouping:a,onConfirm:o,onCancel:c}){let l=(0,i.useDependency)(n.LocaleService),u=(0,i.useObservable)(l.direction$,l.getDirection()),p=(e,t)=>{let i=e===void 0?null:(0,n.excelSerialToDateTimeParts)(e,{dateSystem:r});if(!i)return{date:``,time:t};let a=e=>String(e).padStart(2,`0`);return{date:`${i.year}-${a(i.month)}-${a(i.day)}`,time:`${a(i.hours)}:${a(i.minutes)}:${a(i.seconds)}`}},[m,h]=(0,f.useState)((a==null?void 0:a.units)??[e.PivotExcelDateGroupUnit.Day]),[g,_]=(0,f.useState)((a==null?void 0:a.autoStart)??!0),[v,y]=(0,f.useState)((a==null?void 0:a.autoEnd)??!0),[b,x]=(0,f.useState)(()=>p(a==null?void 0:a.start,`00:00:00`).date),[S,C]=(0,f.useState)(()=>p(a==null?void 0:a.end,`23:59:59`).date),[w,T]=(0,f.useState)(()=>p(a==null?void 0:a.start,`00:00:00`).time),[E,D]=(0,f.useState)(()=>p(a==null?void 0:a.end,`23:59:59`).time),[O,k]=(0,f.useState)((a==null?void 0:a.daysInterval)??1),[A,j]=(0,f.useState)(!1),M=(0,f.useRef)(!1),N=m.some(t=>t===e.PivotExcelDateGroupUnit.Hour||t===e.PivotExcelDateGroupUnit.Minute||t===e.PivotExcelDateGroupUnit.Second),P=m.length===1&&m[0]===e.PivotExcelDateGroupUnit.Day,F=(e,t,i)=>{let a=/^(\d{4})-(\d{2})-(\d{2})$/.exec(e),o=/^(\d{2}):(\d{2})(?::(\d{2}))?$/.exec(t);return!a||!o?null:(0,n.excelDateTimePartsToSerial)({year:Number(a[1]),month:Number(a[2]),day:Number(a[3]),hours:N?Number(o[1]):i?23:0,minutes:N?Number(o[2]):i?59:0,seconds:N?Number(o[3]??0):i?59:0,fractionalSecond:i?.999:0},{dateSystem:r})},I=g?void 0:F(b,w,!1),L=v?void 0:F(S,E,!0),R=m.length>0&&(g||I!==null)&&(v||L!==null)&&(I==null||L==null||I<=L)&&(!P||Number.isInteger(O)&&O>=1);return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,dir:u,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium`,children:t}),/* @__PURE__ */ (0,d.jsxs)(`fieldset`,{children:[/* @__PURE__ */ (0,d.jsx)(`legend`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:l.t(`sheets-pivot-ui.dateGroup.groupBy`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-grid univer-grid-cols-2 univer-gap-2`,children:zn.map(e=>/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:m.includes(e),onChange:t=>h(n=>t?zn.filter(t=>t===e||n.includes(t)):n.filter(t=>t!==e)),children:l.t(Bn[e])},e))})]}),P&&/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-items-center univer-justify-between univer-gap-3 univer-text-sm`,children:[l.t(`sheets-pivot-ui.dateGroup.daysInterval`),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-w-24`,value:O,min:1,onChange:e=>k(Number(e))})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:g,onChange:e=>_(!!e),children:l.t(`sheets-pivot-ui.dateGroup.autoStart`)}),!g&&/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-items-center univer-gap-2 univer-text-sm`,children:[l.t(`sheets-pivot-ui.dateGroup.start`),/* @__PURE__ */ (0,d.jsx)(s.Input,{type:`date`,value:b,onChange:x}),N&&/* @__PURE__ */ (0,d.jsx)(`input`,{className:Vn,type:`time`,step:1,value:w,onChange:e=>T(e.target.value)})]}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:v,onChange:e=>y(!!e),children:l.t(`sheets-pivot-ui.dateGroup.autoEnd`)}),!v&&/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-items-center univer-gap-2 univer-text-sm`,children:[l.t(`sheets-pivot-ui.dateGroup.end`),/* @__PURE__ */ (0,d.jsx)(s.Input,{type:`date`,value:S,onChange:C}),N&&/* @__PURE__ */ (0,d.jsx)(`input`,{className:Vn,type:`time`,step:1,value:E,onChange:e=>D(e.target.value)})]})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-justify-end univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:c,children:l.t(`sheets-pivot-ui.dialog.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:!R||A,onClick:async()=>{if(!R||M.current)return;let e={units:zn.filter(e=>m.includes(e)),autoStart:g,autoEnd:v};!g&&I!=null&&(e.start=I),!v&&L!=null&&(e.end=L),P&&O>1&&(e.daysInterval=O),M.current=!0,j(!0);try{await o(e)}catch{}finally{M.current=!1,j(!1)}},children:l.t(`sheets-pivot-ui.dialog.ok`)})]})]})}function Un(t){let n=t.operator===e.ST_PivotFilterOperatorEnum.dateBetween||t.operator===e.ST_PivotFilterOperatorEnum.dateNotBetween,r={type:e.PivotFilterTypeEnum.CustomFilter,operator:t.operator,expected:n?[t.lower,t.upper]:t.lower,useWholeDay:t.useWholeDay};try{return(0,e.compilePivotDateFilter)(r),r}catch{return}}function Wn(e,t){let r=(0,n.excelSerialToDateTimeParts)(e,{dateSystem:t});if(!r||r.day===0||r.year===1900&&r.month===2&&r.day===29)return;let i=new Date(2e3,0,1,12);return i.setFullYear(r.year,r.month-1,r.day),i}function Gn(e,t,r){let i=(0,n.excelDateTimePartsToSerial)({year:t.getFullYear(),month:t.getMonth()+1,day:t.getDate(),hours:0,minutes:0,seconds:0,fractionalSecond:0},{dateSystem:r});return i===null?void 0:i+(e===void 0?0:e-Math.floor(e))}function Kn(e,t){return e===void 0||!Number.isFinite(t.getTime())?e:Math.floor(e)+(t.getHours()*3600+t.getMinutes()*60+t.getSeconds()+t.getMilliseconds()/1e3)/86400}const qn=[{value:e.ST_PivotFilterOperatorEnum.dateEqual,label:`sheets-pivot-ui.dateFilter.equal`},{value:e.ST_PivotFilterOperatorEnum.dateNotEqual,label:`sheets-pivot-ui.dateFilter.notEqual`},{value:e.ST_PivotFilterOperatorEnum.dateNewerThan,label:`sheets-pivot-ui.dateFilter.after`},{value:e.ST_PivotFilterOperatorEnum.dateNewerThanOrEqual,label:`sheets-pivot-ui.dateFilter.afterOrEqual`},{value:e.ST_PivotFilterOperatorEnum.dateOlderThan,label:`sheets-pivot-ui.dateFilter.before`},{value:e.ST_PivotFilterOperatorEnum.dateOlderThanOrEqual,label:`sheets-pivot-ui.dateFilter.beforeOrEqual`},{value:e.ST_PivotFilterOperatorEnum.dateBetween,label:`sheets-pivot-ui.dateFilter.between`},{value:e.ST_PivotFilterOperatorEnum.dateNotBetween,label:`sheets-pivot-ui.dateFilter.notBetween`}];function Jn({dateSystem:t,defaultDate:r,currentFilter:a,onConfirm:o,onCancel:c}){let l=(0,i.useDependency)(n.LocaleService);(0,i.useObservable)(l.currentLocale$);let u=(0,i.useObservable)(l.direction$,l.getDirection()),[p,m]=(0,f.useState)((a==null?void 0:a.operator)??e.ST_PivotFilterOperatorEnum.dateEqual),h=Array.isArray(a==null?void 0:a.expected)?a.expected[0]:a==null?void 0:a.expected,g=Array.isArray(a==null?void 0:a.expected)?a.expected[1]:void 0,[_,v]=(0,f.useState)(()=>h??r??Gn(void 0,/* @__PURE__ */ new Date,t)),[y,b]=(0,f.useState)(()=>g??h??r??Gn(void 0,/* @__PURE__ */ new Date,t)),[x,S]=(0,f.useState)((a==null?void 0:a.useWholeDay)===!0),[C,w]=(0,f.useState)(!1),[T,E]=(0,f.useState)(!1),D=(0,f.useRef)(!1),O=P(_),k=P(y),A=p===e.ST_PivotFilterOperatorEnum.dateBetween||p===e.ST_PivotFilterOperatorEnum.dateNotBetween,j=Un({operator:p,lower:O,upper:k,useWholeDay:x}),M=j!==void 0&&a!==void 0&&p===a.operator&&x===(a.useWholeDay===!0)&&n.Tools.diffValue(j.expected,a.expected);async function N(e){if(!D.current){D.current=!0,w(!0),E(!1);try{await o(e)||E(!0)}catch{E(!0)}finally{D.current=!1,w(!1)}}}function P(e){return typeof e==`string`?e.trim()===``?void 0:Number(e):e}function F(e,t){t?b(e):v(e)}function I(e,n){let r=Gn(n?k:O,e,t);n?b(r):v(r)}function L(e,t){let n=t?k:O;if(n===void 0)return;let r=Kn(n,e);t?b(r):v(r)}return/* @__PURE__ */ (0,d.jsxs)(`div`,{dir:u,className:`univer-flex univer-min-w-0 univer-flex-col univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium`,children:l.t(`sheets-pivot-ui.dateFilter.title`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-1`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-xs`,children:l.t(`sheets-pivot-ui.dateFilter.rule`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-full`,value:String(p),options:qn.map(e=>({value:String(e.value),label:l.t(e.label)})),onChange:e=>m(Number(e))})]}),(A?[!1,!0]:[!1]).map(e=>{let i=e?k:O,a=i===void 0?void 0:Wn(i,t),o=(e?g:void 0)??h??r,c=o!==void 0&&!Wn(o,t),u=i===void 0?null:(0,n.excelSerialToDateTimeParts)(i,{dateSystem:t}),f=u?new Date(2e3,0,1,u.hours,u.minutes,u.seconds,Math.floor(u.fractionalSecond*1e3)):void 0,p=`sheets-pivot-ui.dateFilter.date`;return A&&(p=e?`sheets-pivot-ui.dateFilter.to`:`sheets-pivot-ui.dateFilter.from`),/* @__PURE__ */ (0,d.jsxs)(`div`,{role:`group`,"aria-label":l.t(p),className:`univer-flex univer-min-w-0 univer-flex-col univer-gap-1`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-xs`,children:l.t(p)}),c?/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-1`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-xs univer-text-gray-500`,children:l.t(`sheets-pivot-ui.dateFilter.serial`)}),/* @__PURE__ */ (0,d.jsx)(s.Input,{value:String((e?y:_)??``),onChange:t=>F(t,e)})]}):/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-wrap univer-items-center univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.DatePicker,{value:a,onValueChange:t=>I(t,e)}),!x&&i!==void 0&&/* @__PURE__ */ (0,d.jsx)(s.TimeInput,{value:f,onValueChange:t=>L(t,e)})]})]},String(e))}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:x,onChange:e=>S(e===!0),children:l.t(`sheets-pivot-ui.dateFilter.wholeDay`)}),T&&/* @__PURE__ */ (0,d.jsx)(`div`,{role:`alert`,className:`univer-text-xs univer-text-red-500`,children:l.t(`sheets-pivot-ui.dateFilter.failed`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-wrap univer-items-center univer-justify-between univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`link`,disabled:C||!a,onClick:()=>N(void 0),children:l.t(`sheets-pivot-ui.dialog.filter.clearFilter`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{disabled:C,onClick:c,children:l.t(`sheets-pivot-ui.dialog.filter.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:C||!j||M,onClick:()=>N(j),children:l.t(`sheets-pivot-ui.dialog.filter.confirm`)})]})]})]})}function Yn(r){let{dialogId:a=x,filterItemInfo:o,onClose:c,onSetFilter:l,onSetSort:p,onSetDateFilter:m,unitId:h,subUnitId:g}=r,{format:_,filterInfo:v,sortInfo:y}=o,b=o.conditionFilter??v,S=(0,i.useDependency)(n.ICommandService),C=(0,i.useDependency)(n.LocaleService);(0,i.useObservable)(C.currentLocale$);let w=(0,i.useDependency)(i.IDialogService),T=(0,i.useDependency)(n.IUniverInstanceService),E=(0,i.useObservable)(C.direction$,C.getDirection()),D=(0,f.useRef)(!0),[O,k]=(0,f.useState)(!1);(0,f.useEffect)(()=>(D.current=!0,()=>{D.current=!1}),[]);let[A,j]=(0,f.useState)(()=>o?o.checkedItems??(v==null?void 0:v.list)??o.items:[]),[M,N]=(0,f.useState)(()=>o.partialItems??[]),P=M.length===0&&A.length===o.items.length,F=A.length===0&&M.length===0,[I,L]=(0,f.useState)(()=>{if(o)return y==null?void 0:y.type}),R=()=>{D.current&&(D.current=!1,c?c():w.close(a))};if((0,f.useEffect)(()=>{let e=T.getCurrentUnitOfType(n.UniverInstanceType.UNIVER_SHEET);if(!e)return;let t=e.activeSheet$.subscribe(e=>{e&&e.getSheetId()!==g&&D.current&&(D.current=!1,c?c():w.close(a))});return()=>t==null?void 0:t.unsubscribe()},[a,w,c,g,T]),!o)return;let z=()=>{N([]),j(P?[]:o.items)},B=e=>{M.includes(e)?(N(M.filter(t=>t!==e)),j([...A,e])):A.includes(e)?j(A.filter(t=>t!==e)):j([...A,e])},V=async()=>{let{tableFieldId:e,pivotTableId:n}=o;if(e&&n)try{(l?await l(A,P,M):await S.executeCommand(t.SetPivotFilterCommand.id,{unitId:h,subUnitId:g,pivotTableId:n,tableFieldId:e,items:A,partialItems:M,isAll:P}))&&D.current&&R()}catch{}},ee=()=>{R()},te=async()=>{let{tableFieldId:e,pivotTableId:n}=o;if(e&&n){j(o.items),N([]);try{(l?await l(o.items,!0,[],!0):await S.executeCommand(t.SetPivotFilterCommand.id,{unitId:h,subUnitId:g,pivotTableId:n,tableFieldId:e,items:o.items,partialItems:[],isAll:!0,clearAllFilters:!0}))&&D.current&&R()}catch{}}},H=async e=>{let{tableFieldId:n,pivotTableId:r}=o;if(!n||!r)return;let i={type:e};try{(p?await p(i):await S.executeCommand(t.SetPivotSortCommand.id,{unitId:h,subUnitId:g,pivotTableId:r,tableFieldId:n,info:i}))&&D.current&&(L(e),R())}catch{}},U=Ut(v,{items:o.items,conditionFilter:o.conditionFilter});async function ne(e){if(!D.current)return!1;let n=m?await m(e):await S.executeCommand(t.SetPivotDateFilterCommand.id,{unitId:h,subUnitId:g,pivotTableId:o.pivotTableId,tableFieldId:o.tableFieldId,filterInfo:e});return n&&D.current&&R(),n}if(O){let t=T.getUnit(h),r=o.itemTypes.findIndex(t=>t===e.PivotDataFieldDataTypeEnum.date),i=Number(o.items[r]);return/* @__PURE__ */ (0,d.jsx)(Jn,{dateSystem:(t==null?void 0:t.getDateSystem())??n.DateSystem.Date1900,defaultDate:Number.isFinite(i)?i:void 0,currentFilter:(0,e.isPivotDateFilter)(b)?b:void 0,onConfirm:ne,onCancel:()=>k(!1)})}return/* @__PURE__ */ (0,d.jsxs)(`div`,{dir:E,children:[o.supportsDateFilter&&/* @__PURE__ */ (0,d.jsx)(s.Button,{className:`univer-mb-3 univer-w-full`,onClick:()=>k(!0),children:C.t(`sheets-pivot-ui.dateFilter.title`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:(0,s.clsx)(`univer-flex univer-h-8 univer-w-[140px] univer-flex-1 univer-cursor-pointer univer-items-center univer-justify-center univer-rounded-l-md univer-text-center univer-text-sm hover:univer-bg-gray-100 rtl:univer-rounded-l-none rtl:univer-rounded-r-md dark:hover:!univer-bg-gray-700`,s.borderClassName,{"univer-bg-gray-100 dark:!univer-bg-gray-700":I===e.PivotDataFieldSortOperatorEnum.ascending}),onClick:()=>H(e.PivotDataFieldSortOperatorEnum.ascending),children:[/* @__PURE__ */ (0,d.jsx)(u.AscendingIcon,{}),C.t(`sheets-pivot-ui.dialog.filter.ascending`)]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:(0,s.clsx)(`univer-flex univer-h-8 univer-w-[140px] univer-flex-1 univer-cursor-pointer univer-items-center univer-justify-center univer-rounded-r-md univer-text-center univer-text-sm hover:univer-bg-gray-100 rtl:univer-rounded-l-md rtl:univer-rounded-r-none dark:hover:!univer-bg-gray-700`,s.borderClassName,{"univer-bg-gray-100 dark:!univer-bg-gray-700":I===e.PivotDataFieldSortOperatorEnum.descending}),onClick:()=>H(e.PivotDataFieldSortOperatorEnum.descending),children:[/* @__PURE__ */ (0,d.jsx)(u.DescendingIcon,{}),C.t(`sheets-pivot-ui.dialog.filter.descending`)]})]}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-mx-0 univer-mb-4 univer-mt-3 univer-h-[300px] univer-rounded-lg univer-p-2`,s.borderClassName),children:/* @__PURE__ */ (0,d.jsxs)(`div`,{className:(0,s.clsx)(`univer-h-full univer-overflow-y-auto`,s.scrollbarClassName),children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-1 univer-box-border univer-h-7 univer-py-[5px] univer-pl-[22px] univer-pr-1 univer-text-sm rtl:univer-pl-1 rtl:univer-pr-[22px]`,children:/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:P,onChange:z,indeterminate:!P&&(A.length>0||M.length>0),children:C.t(`sheets-pivot-ui.dialog.filter.selectAll`)})}),o.items.map((t,r)=>{var i,a;let c=t,l=(i=o.itemLabels)==null?void 0:i[t],u=(a=o.itemTypes)==null?void 0:a[r],f=ht(t,u);return f?c=C.t(f):l===void 0?u===e.PivotDataFieldDataTypeEnum.date&&_&&(c=n.numfmt.format(_,Number(t))):c=l,/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-1 univer-box-border univer-h-7 univer-py-1 univer-pl-[22px] univer-pr-1 univer-text-sm rtl:univer-pl-1 rtl:univer-pr-[22px]`,children:/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:A.includes(t),indeterminate:M.includes(t),onChange:()=>B(t),contentClassName:`univer-max-w-[200px] univer-truncate`,children:c})},t)})]})}),/* @__PURE__ */ (0,d.jsxs)(s.ActionRow,{className:`univer-flex univer-items-center univer-justify-between`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-cursor-pointer univer-text-xs univer-text-primary-600`,children:/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`link`,disabled:U===`NotFilter`,onClick:te,children:C.t(`sheets-pivot-ui.dialog.filter.clearFilter`)})}),/* @__PURE__ */ (0,d.jsxs)(s.ActionRow,{className:`univer-flex univer-flex-1 univer-items-center univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:ee,children:C.t(`sheets-pivot-ui.dialog.filter.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:F,onClick:V,children:C.t(`sheets-pivot-ui.dialog.filter.confirm`)})]})]})]})}function Xn({name:e,onConfirm:t,onCancel:r}){let a=(0,i.useDependency)(n.LocaleService),o=(0,i.useObservable)(a.direction$,a.getDirection()),[c,l]=(0,f.useState)(e),[u,p]=(0,f.useState)(!1),m=(0,f.useRef)(!1);return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,dir:o,children:[/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-flex-col univer-gap-2 univer-text-sm`,children:[a.t(`sheets-pivot-ui.manualGroup.name`),/* @__PURE__ */ (0,d.jsx)(s.Input,{value:c,onChange:l})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-justify-end univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:r,children:a.t(`sheets-pivot-ui.dialog.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:!c.trim()||u,onClick:async()=>{if(c.trim()&&!m.current){m.current=!0,p(!0);try{await t(c)}catch{}finally{m.current=!1,p(!1)}}},children:a.t(`sheets-pivot-ui.dialog.ok`)})]})]})}const Zn=`univer-flex univer-items-center univer-justify-between univer-gap-3 univer-text-sm`;function Qn({targetFieldName:e,grouping:t,defaults$:r,onConfirm:a,onCancel:o}){let c=(0,i.useDependency)(n.LocaleService),l=(0,i.useObservable)(c.direction$,c.getDirection()),u=(0,i.useObservable)(r),[p,m]=(0,f.useState)((t==null?void 0:t.autoStart)??!0),[h,g]=(0,f.useState)((t==null?void 0:t.autoEnd)??!0),[_,v]=(0,f.useState)((t==null?void 0:t.autoStart)===!1?String(t.start):void 0),[y,b]=(0,f.useState)((t==null?void 0:t.autoEnd)===!1?String(t.end):void 0),[x,S]=(0,f.useState)(t?String(t.interval):void 0),[C,w]=(0,f.useState)(!1),T=(0,f.useRef)(!1),E=e=>e===void 0||!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(e.trim())?NaN:Number(e),D={autoStart:p,autoEnd:h,start:p?u==null?void 0:u.start:E(_),end:h?u==null?void 0:u.end:E(y),interval:x===void 0||x.trim()===``?(u==null?void 0:u.interval)??0:E(x)},O=!!u&&se(D),k=e=>{!e&&_===void 0&&(u==null?void 0:u.start)!==void 0&&v(String(u.start)),m(e)},A=e=>{!e&&y===void 0&&(u==null?void 0:u.end)!==void 0&&b(String(u.end)),g(e)};return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,dir:l,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium`,children:e}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:p,onChange:e=>k(!!e),children:c.t(`sheets-pivot-ui.numberGroup.autoStart`)}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:Zn,children:[c.t(`sheets-pivot-ui.numberGroup.start`),/* @__PURE__ */ (0,d.jsx)(s.Input,{className:`univer-w-32`,value:p?String((u==null?void 0:u.start)??``):_??``,disabled:p,onChange:v})]}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:h,onChange:e=>A(!!e),children:c.t(`sheets-pivot-ui.numberGroup.autoEnd`)}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:Zn,children:[c.t(`sheets-pivot-ui.numberGroup.end`),/* @__PURE__ */ (0,d.jsx)(s.Input,{className:`univer-w-32`,value:h?String((u==null?void 0:u.end)??``):y??``,disabled:h,onChange:b})]}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:Zn,children:[c.t(`sheets-pivot-ui.numberGroup.interval`),/* @__PURE__ */ (0,d.jsx)(s.Input,{className:`univer-w-32`,value:x??String((u==null?void 0:u.interval)??``),placeholder:String((u==null?void 0:u.interval)??``),onChange:S})]})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-justify-end univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:o,children:c.t(`sheets-pivot-ui.dialog.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:!O||C,onClick:async()=>{if(O&&!T.current){T.current=!0,w(!0);try{await a(D)}catch{}finally{T.current=!1,w(!1)}}},children:c.t(`sheets-pivot-ui.dialog.ok`)})]})]})}const $n=({handlePatternChange:e})=>{let t=(0,i.useDependency)(n.LocaleService);return(0,f.useEffect)(()=>{e(``)},[]),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-1.5 univer-text-sm univer-leading-5 univer-text-gray-600 dark:!univer-text-gray-200`,children:t.t(`sheets-pivot-ui.dialog.numfmt.generalDesc`)})},er=({originPattern:e,handlePatternChange:t})=>{let r=(0,i.useDependency)(n.LocaleService),a=n.currencySymbols.map(e=>({label:e,value:e})),[o,c]=(0,f.useState)(()=>At(e||``,2)),[l,u]=(0,f.useState)(()=>Mt(e??``)||a[0].value);return(0,f.useEffect)(()=>{t(Y(`_("${l}"* #,##0${o>0?`.0`:``}_)`,o))},[]),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mt-5`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-w-[136px]`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.decimalPlaces`)}),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-box-border univer-h-8`,value:o,max:20,min:0,onChange:e=>{let n=e??0;c(n),t(Y(`_("${l}"* #,##0${n>0?`.0`:``}_)`,n))}})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-w-[136px]`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.currencyTypes`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{options:a,value:l,onChange:e=>{u(e),t(Y(`_("${e}"* #,##0${o>0?`.0`:``}_)`,o))}})]})]}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-1.5 univer-text-sm univer-leading-5 univer-text-gray-600 dark:!univer-text-gray-200`,children:r.t(`sheets-pivot-ui.dialog.numfmt.accountingDesc`)})]})},tr=({originPattern:e,handlePatternChange:t})=>{let r=(0,i.useDependency)(n.LocaleService),a=n.currencySymbols.map(e=>({label:e,value:e})),[o,c]=(0,f.useState)(()=>At(e||``,2)),[l,u]=(0,f.useState)(()=>Mt(e??``)||a[0].value),p=pt.map(e=>({label:e.label(l),value:e.suffix(l),color:e.color})),[m,h]=(0,f.useState)(()=>{var t;return((t=p.find(t=>(0,n.isPatternEqualWithoutDecimal)(t.value,e??``)))==null?void 0:t.value)||p[0].value});return(0,f.useEffect)(()=>{t(Y(m,o))},[]),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mt-5`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-w-[136px]`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.decimalPlaces`)}),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-box-border univer-h-8`,value:o,max:20,min:0,onChange:e=>{e&&(c(e),t(Y(m,e)))}})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-w-[136px]`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.currencyTypes`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{options:a,value:l,onChange:e=>{u(e);let n=pt.map(t=>({label:t.label(e),value:t.suffix(e),color:t.color}));h(n[0].value),t(Y(n[0].value,o))}})]})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mt-4`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.negativeNumberTypes`)}),/* @__PURE__ */ (0,d.jsx)(s.SelectList,{className:`univer-rounded-lg univer-p-2`,options:p,value:m,onChange:e=>{h(e),t(Y(e,o))}}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-1.5 univer-text-sm univer-leading-5 univer-text-gray-600 dark:!univer-text-gray-200`,children:r.t(`sheets-pivot-ui.dialog.numfmt.currencyDesc`)})]})]})},nr=({originPattern:e,handlePatternChange:t})=>{let r=(0,i.useDependency)(n.LocaleService),a=dt.map(e=>({label:e.label,value:e.suffix})),[o,c]=(0,f.useState)(()=>{if(e){let t=a.find(t=>t.value===e);if(t)return t.value}return a[0].value});return(0,f.useEffect)(()=>{t(o)},[]),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-5`,children:/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mt-5`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.dateTypes`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{children:/* @__PURE__ */ (0,d.jsx)(s.SelectList,{className:`univer-rounded-lg univer-p-2`,options:a,value:o,onChange:e=>{c(e),t(e)}})}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-1.5 univer-text-sm univer-leading-5 univer-text-gray-600 dark:!univer-text-gray-200`,children:r.t(`sheets-pivot-ui.dialog.numfmt.dateDesc`)})]})})},rr=({originPattern:e,handlePatternChange:t})=>{let r=(0,i.useDependency)(n.LocaleService),[a,o]=(0,f.useState)(()=>At(e||``,0)),c=ft.map(e=>({label:e.label,value:e.suffix,color:e.color})),[l,u]=(0,f.useState)(()=>{let t=c.find(t=>(0,n.isPatternEqualWithoutDecimal)(t.value,e||``));return(t==null?void 0:t.value)||c[0].value});return(0,f.useEffect)(()=>{t(Y(l,Number(a||0)))},[]),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mt-5`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-items-center univer-justify-between`,children:/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-w-[136px]`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.decimalPlaces`)}),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-box-border univer-h-8`,value:a,max:20,min:0,onChange:e=>{e&&(o(e),t(Y(l,Number(e))))}})]})}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mt-4`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:r.t(`sheets-pivot-ui.dialog.numfmt.negativeNumberTypes`)}),/* @__PURE__ */ (0,d.jsx)(s.SelectList,{className:`univer-rounded-lg univer-p-2`,options:c,value:l,onChange:e=>{o(At(e,0)),u(e),t(e)}}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-1.5 univer-text-sm univer-leading-5 univer-text-gray-600 dark:!univer-text-gray-200`,children:r.t(`sheets-pivot-ui.dialog.numfmt.thousandthPercentileDesc`)})]})]})},ir=({unitId:e,subUnitId:t,pivotTableId:r,tableFieldId:a,originPattern:o,tableFieldInfo:c,dataFieldInfo:l})=>{let p=(0,i.useDependency)(n.LocaleService),m=(0,i.useDependency)(i.IDialogService),h=p.getDirection(),g=(0,f.useMemo)(()=>[{label:`sheets-pivot-ui.dialog.numfmt.general`,component:$n,key:`sheet.numfmt.general`},{label:`sheets-pivot-ui.dialog.numfmt.accounting`,component:er,key:`sheet.numfmt.accounting`},{label:`sheets-pivot-ui.dialog.numfmt.currency`,component:tr,key:`sheet.numfmt.currency`},{label:`sheets-pivot-ui.dialog.numfmt.date`,component:nr,key:`sheet.numfmt.date`},{label:`sheets-pivot-ui.dialog.numfmt.thousandthPercentile`,component:rr,key:`sheet.numfmt.thousandthPercentile`}].map(e=>({...e,label:p.t(e.label)})),[p]),_=g.map(e=>({label:e.label,value:e.key})),[v,y]=(0,f.useState)(()=>[Nt,Pt,Ft,Lt,zt].reduce((e,t,n)=>e||(t(o??``)?g[n].key:``),``)||g[0].key),[b,x]=(0,f.useState)(o),w=e=>x(e),E=e=>{y(e)},D=n=>({id:C,title:{title:p.t(`sheets-pivot-ui.fieldSettings`)},draggable:!0,mask:!1,maskClosable:!1,width:400,children:{label:S,tableFieldId:a,pivotTableId:r,unitId:e,subUnitId:t,tableFieldInfo:c,dataFieldInfo:l,key:`field-setting-${a||``}`,closeFn:()=>{m.close(C)},format:n?b:``},onClose:()=>{m.close(C)}}),O=()=>{m.close(T);let e=D(!1);m.open(e)};return/* @__PURE__ */ (0,d.jsxs)(`div`,{dir:h,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex`,children:/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-5 univer-flex univer-h-6 univer-cursor-pointer univer-items-center univer-text-base univer-font-medium univer-leading-6`,onClick:O,children:[/* @__PURE__ */ (0,d.jsx)(u.ArrowLeftIcon,{}),p.t(`sheets-pivot-ui.dialog.numfmt.fieldSetting`)]})}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-h-5 univer-text-sm univer-font-medium univer-leading-5`,children:p.t(`sheets-pivot-ui.dialog.numfmt.formatTypes`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{children:/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-full`,value:v,options:_,onChange:e=>E(e)})}),/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[v===`sheet.numfmt.general`&&/* @__PURE__ */ (0,d.jsx)($n,{originPattern:o,handlePatternChange:w}),v===`sheet.numfmt.accounting`&&/* @__PURE__ */ (0,d.jsx)(er,{originPattern:o,handlePatternChange:w}),v===`sheet.numfmt.currency`&&/* @__PURE__ */ (0,d.jsx)(tr,{originPattern:o,handlePatternChange:w}),v===`sheet.numfmt.date`&&/* @__PURE__ */ (0,d.jsx)(nr,{originPattern:o,handlePatternChange:w}),v===`sheet.numfmt.thousandthPercentile`&&/* @__PURE__ */ (0,d.jsx)(rr,{originPattern:o,handlePatternChange:w})]}),/* @__PURE__ */ (0,d.jsxs)(s.ActionRow,{className:`univer-mt-2 univer-flex univer-justify-end`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:O,children:p.t(`sheets-pivot-ui.dialog.numfmt.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{className:`univer-ml-3 rtl:univer-ml-0 rtl:univer-mr-3`,variant:`primary`,onClick:()=>{m.close(T);let e=D(!0);m.open(e)},children:p.t(`sheets-pivot-ui.dialog.numfmt.confirm`)})]})]})};function ar(a){var o,c;let{scope:l,report:u,getCurrentReport:p,onChange:m}=a,h=(0,i.useDependency)(n.LocaleService),g=(0,i.useDependency)(n.IConfirmService),_=(0,i.useDependency)(t.SheetsPivotTableService),v=(0,i.useDependency)(n.IPermissionService),y=(0,i.useDependency)(n.ILogService);(0,i.useObservable)(v.permissionPointUpdate$);let[b,x]=(0,f.useState)(!1),S=(0,f.useRef)(!1),C=(0,f.useRef)(!0),w=JSON.stringify(l),T=(0,f.useRef)(w);T.current=w,(0,f.useEffect)(()=>(C.current=!0,()=>{C.current=!1}),[]);let E=b||!u||((o=v.getPermissionPoint(new r.WorkbookEditablePermission(l.unitId).id))==null?void 0:o.value)===!1||((c=v.getPermissionPoint(new r.WorksheetFilterPermission(l.unitId,l.subUnitId).id))==null?void 0:c.value)===!1,D=async r=>{if(S.current||E||!u||u.options.allowMultipleFilters===r)return;S.current=!0,x(!0);let i=n.Tools.deepClone(u);try{if(!r&&(0,e.hasPivotMultipleFilters)(u)){let e=await _.asyncInterceptor.fetchThroughAsyncInterceptors(t.PIVOT_OPTION_FILTER_COORDINATION)({redos:[],undos:[],groups:[],validate:()=>void 0},l);if(!C.current||T.current!==w||!await g.confirm({id:`sheet-pivot.confirm.disable-multiple-filters`,title:{title:h.t(`sheets-pivot-ui.config.multipleFiltersTitle`)},children:{title:h.t(`sheets-pivot-ui.config.multipleFiltersClearWarning`)},cancelText:h.t(`sheets-pivot-ui.dialog.cancel`),confirmText:h.t(`sheets-pivot-ui.dialog.ok`)}))return;e==null||e.validate()}if(!C.current||T.current!==w||!n.Tools.diffValue(i,p()))return;await m(r)}catch(e){y.error(`[PivotFilterOptions]`,e)}finally{S.current=!1,C.current&&x(!1)}};return/* @__PURE__ */ (0,d.jsxs)(`section`,{className:`univer-flex univer-flex-col univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.filteringSection`)}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-items-start univer-gap-2 univer-text-sm`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:(u==null?void 0:u.options.allowMultipleFilters)===!0,disabled:E,onChange:e=>D(!!e)}),/* @__PURE__ */ (0,d.jsx)(`span`,{children:h.t(`sheets-pivot-ui.config.allowMultipleFilters`)})]})]})}const or=[{label:`sheets-pivot-ui.config.pageDownThenOver`,value:`false`},{label:`sheets-pivot-ui.config.pageOverThenDown`,value:`true`}],sr=[{label:`sheets-pivot-ui.config.layoutTabular`,value:String(e.PivotLayoutTypeEnum.tabular)},{label:`sheets-pivot-ui.config.layoutCompact`,value:String(e.PivotLayoutTypeEnum.compact)},{label:`sheets-pivot-ui.config.layoutOutline`,value:String(e.PivotLayoutTypeEnum.outline)}],cr=[{label:`sheets-pivot-ui.config.rowSubtotalTop`,value:String(e.PivotSubtotalPositionEnum.top)},{label:`sheets-pivot-ui.config.rowSubtotalBottom`,value:String(e.PivotSubtotalPositionEnum.bottom)}],lr=[{label:`sheets-pivot-ui.config.dateGroupingLegacy`,value:e.PivotDateGroupingMode.Legacy},{label:`sheets-pivot-ui.config.dateGroupingExcelLike`,value:e.PivotDateGroupingMode.ExcelLike}];function ur(r){let{unitId:a,subUnitId:c,pivotTableId:l}=r,u=(0,i.useDependency)(n.ICommandService),p=(0,i.useDependency)(n.IConfirmService),m=(0,i.useDependency)(n.ILogService),h=(0,i.useDependency)(n.LocaleService),g=(0,i.useDependency)(t.SheetsPivotTableConfigModel),_=(0,f.useMemo)(()=>g.watchPivotTableChanges(a,c,l).pipe((0,o.debounceTime)(0)),[g,a,c,l]);(0,i.useObservable)(_);let[v,y]=(0,f.useState)(!1),b=(0,f.useRef)(!1),x=g.getPivotTableConfig(a,c,l),S=(0,e.getPivotDateGroupingMode)(x==null?void 0:x.fieldsConfig.options),[C,w]=(0,f.useState)(()=>{var t,n;let r=g.getPivotTableConfig(a,c,l),i=(r==null||(t=r.fieldsConfig)==null?void 0:t.options)??{};return{showRowSubtotal:i.showRowSubtotal??!0,showRowGrandTotal:i.showRowGrandTotal??!0,showColSubtotal:i.showColSubtotal??!0,showColGrandTotal:i.showColGrandTotal??!0,pageWrap:i.pageWrap??1,pageOverThenDown:i.pageOverThenDown??!1,repeatRowLabels:i.repeatRowLabels??!1,repeatColLabels:i.repeatColLabels??!1,layout:(r==null||(n=r.fieldsConfig)==null?void 0:n.layout)??e.PivotLayoutTypeEnum.tabular,rowSubtotalPosition:i.rowSubtotalPosition??e.PivotSubtotalPositionEnum.top}}),T=e=>{let n={unitId:a,subUnitId:c,pivotTableId:l,options:e};u.executeCommand(t.SetPivotOptionCommand.id,n).catch(e=>m.error(`[PivotConfigPanel]`,e)),w(t=>({...t,...e}))},E=e=>{let n={unitId:a,subUnitId:c,pivotTableId:l,layout:e};u.executeCommand(t.SetPivotLayoutCommand.id,n).catch(e=>m.error(`[PivotConfigPanel]`,e)),w(t=>({...t,layout:e}))},D=async n=>{if(!(b.current||n===S)){b.current=!0,y(!0);try{let r=g.getPivotTableConfig(a,c,l),i=g.getCollection(a,l);if(!r||!i||(Object.values(i.fields).some(e.isDateGroupFieldJSON)||i.customFields.some(e.isDateGroupFieldJSON))&&!await p.confirm({id:`sheet-pivot.confirm.change-date-grouping-mode`,title:{title:h.t(`sheets-pivot-ui.config.dateGroupingSwitchTitle`)},children:{title:h.t(`sheets-pivot-ui.config.dateGroupingSwitchMessage`)},cancelText:h.t(`sheets-pivot-ui.dialog.cancel`),confirmText:h.t(`sheets-pivot-ui.dialog.ok`)})||g.getPivotTableConfig(a,c,l)!==r||g.getCollection(a,l)!==i||(0,e.getPivotDateGroupingMode)(r.fieldsConfig.options)===n)return;await u.executeCommand(t.SetPivotOptionCommand.id,{unitId:a,subUnitId:c,pivotTableId:l,options:{dateGroupingMode:n}})}catch(e){m.error(`[PivotConfigPanel]`,e)}finally{b.current=!1,y(!1)}}};return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4 univer-py-4`,children:[/* @__PURE__ */ (0,d.jsx)(ar,{scope:{unitId:a,subUnitId:c,pivotTableId:l},report:x==null?void 0:x.fieldsConfig,getCurrentReport:()=>{var e;return(e=g.getPivotTableConfig(a,c,l))==null?void 0:e.fieldsConfig},onChange:e=>u.executeCommand(t.SetPivotOptionCommand.id,{unitId:a,subUnitId:c,pivotTableId:l,options:{allowMultipleFilters:e}})}),/* @__PURE__ */ (0,d.jsxs)(`section`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.displaySection`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.dateGroupingMode`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-36`,value:S,options:lr.map(e=>({...e,label:h.t(e.label)})),disabled:v,onChange:e=>D(e)})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.layout`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-36`,value:String(C.layout),options:sr.map(e=>({...e,label:h.t(e.label)})),onChange:e=>E(Number(e))})]}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-gap-2 univer-text-sm univer-text-gray-600`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.repeatRowLabels,disabled:C.layout===e.PivotLayoutTypeEnum.compact,onChange:e=>T({repeatRowLabels:!!e})}),h.t(`sheets-pivot-ui.config.repeatRowLabels`)]}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-gap-2 univer-text-sm univer-text-gray-600`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.repeatColLabels,onChange:e=>T({repeatColLabels:!!e})}),h.t(`sheets-pivot-ui.config.repeatColLabels`)]})]})]}),/* @__PURE__ */ (0,d.jsxs)(`section`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-mb-2 univer-text-sm univer-font-medium univer-text-gray-600`),children:h.t(`sheets-pivot-ui.config.totalsSection`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-gap-2 univer-text-sm univer-text-gray-600`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showRowSubtotal,onChange:e=>T({showRowSubtotal:!!e})}),h.t(`sheets-pivot-ui.config.showRowSubtotal`)]}),C.layout===e.PivotLayoutTypeEnum.outline&&/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.rowSubtotalPosition`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-36`,value:String(C.rowSubtotalPosition),options:cr.map(e=>({...e,label:h.t(e.label)})),onChange:e=>T({rowSubtotalPosition:Number(e)})})]}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-gap-2 univer-text-sm univer-text-gray-600`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showRowGrandTotal,onChange:e=>T({showRowGrandTotal:!!e})}),h.t(`sheets-pivot-ui.config.showRowGrandTotal`)]}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-gap-2 univer-text-sm univer-text-gray-600`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showColSubtotal,onChange:e=>T({showColSubtotal:!!e})}),h.t(`sheets-pivot-ui.config.showColSubtotal`)]}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-gap-2 univer-text-sm univer-text-gray-600`,children:[/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{checked:C.showColGrandTotal,onChange:e=>T({showColGrandTotal:!!e})}),h.t(`sheets-pivot-ui.config.showColGrandTotal`)]})]})]}),/* @__PURE__ */ (0,d.jsxs)(`section`,{children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.pageSection`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.pageWrap`)}),/* @__PURE__ */ (0,d.jsx)(s.InputNumber,{className:`univer-w-24`,value:C.pageWrap,min:1,max:20,onChange:e=>{let t=Number(e);Number.isFinite(t)&&t>=1&&T({pageWrap:t})}})]}),/* @__PURE__ */ (0,d.jsx)(`p`,{className:`univer-text-xs univer-text-gray-600`,children:h.t(C.pageOverThenDown?`sheets-pivot-ui.config.pageWrapOverThenDownHelp`:`sheets-pivot-ui.config.pageWrapDownThenOverHelp`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm univer-text-gray-600`,children:h.t(`sheets-pivot-ui.config.pageDirection`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-36`,value:String(C.pageOverThenDown),options:or.map(e=>({...e,label:h.t(e.label)})),onChange:e=>T({pageOverThenDown:e===`true`})})]})]})]})]})}let Q=class extends n.Disposable{constructor(e,t,n,r,i,a,s,c){super(),this._sheetsSelectionsService=e,this._univerInstanceService=t,this._sheetsPivotTableAdaptorModel=n,this._commandService=r,this._sidebarService=i,this._dialogService=a,this._injector=s,this._permissionService=c,G(this,`_pivotTableId`,void 0),G(this,`_pivotTablePanelVisible$`,new o.Subject),G(this,`pivotTablePanelVisible$`,this._pivotTablePanelVisible$.asObservable()),G(this,`_isRangeSelectorFocus`,!1),G(this,`_isRangeSelector2Focus`,!1),this._initPanelOpen(),this._initSheetChange()}setIsRangeSelectorFocus(e){this._isRangeSelectorFocus=e}setIsRangeSelector2Focus(e){this._isRangeSelector2Focus=e}_setPivotTableId(e){this._pivotTableId=e}_closePivotPanel(){this._isRangeSelectorFocus||this._isRangeSelector2Focus||(this._setPivotTableId(void 0),this._sidebarService.close(y))}_initPanelOpen(){let e=this._univerInstanceService.getCurrentUnitOfType(n.UniverInstanceType.UNIVER_SHEET);if(e){let i=e.activeSheet$;this.disposeWithMe((0,o.merge)(i,this._sheetsSelectionsService.selectionMoveEnd$).subscribe(()=>{let e=this._sheetsSelectionsService.getCurrentSelections();if(!e)return;let i=e[e.length-1];if(!i)return;let a=i.primary;if(!a)return;let o=this._univerInstanceService.getCurrentUnitOfType(n.UniverInstanceType.UNIVER_SHEET),s=o==null?void 0:o.getActiveSheet();if(!o||!s)return;let c=o.getUnitId(),l=s.getSheetId(),u=this._sheetsPivotTableAdaptorModel.getPivotTableIdByCell(c,l,a.startRow,a.startColumn);if(u){var d,f;let e=(d=this._sheetsPivotTableAdaptorModel.getPivotItemRenderInfoCache(c,l,u))==null?void 0:d.rangesInfo,n=(0,t.unionPivotViewRange)(e),i=(0,r.checkRangesEditablePermission)(this._injector,c,l,n),a=(f=this._permissionService.getPermissionPoint(new r.WorksheetPivotTablePermission(c,l).id))==null?void 0:f.value;if(!i||!a)return;this._setPivotTableId(u),this._pivotTablePanelVisible$.next(!0),this._commandService.executeCommand(it.id,{pivotTableId:u})}else this._closePivotPanel()})),this.disposeWithMe(this._commandService.onCommandExecuted(e=>{e.id===t.RemovePivotTableMutation.id&&e.params.pivotTableId===this._pivotTableId&&this._sidebarService.close(y)}))}}_initSheetChange(){let e=this._univerInstanceService.getCurrentUnitOfType(n.UniverInstanceType.UNIVER_SHEET);e&&this.disposeWithMe(e.sheetDisposed$.subscribe(()=>{this._sidebarService.close()})),this.disposeWithMe(this._sidebarService.sidebarOptions$.subscribe(e=>{if(e.id===`UNIVER_SHEET_PIVOT_PANEL_ID`){let t=!!e.visible;this._pivotTablePanelVisible$.next(t),t||this._dialogService.close(C)}})),this.disposeWithMe(this._dialogService.getDialogs$().subscribe(e=>{let t=e.find(e=>e.id===j);t&&t.open===!1&&this._dialogService.close(j)}))}};Q=q([K(0,(0,n.Inject)(r.SheetsSelectionsService)),K(1,(0,n.Inject)(n.IUniverInstanceService)),K(2,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),K(3,(0,n.Inject)(n.ICommandService)),K(4,(0,n.Inject)(i.ISidebarService)),K(5,(0,n.Inject)(i.IDialogService)),K(6,(0,n.Inject)(n.Injector)),K(7,(0,n.Inject)(n.IPermissionService))],Q);const dr=(0,f.createContext)(null),fr=(0,f.createContext)(null);function pr(e,t){return e===t?!0:!e||!t?!1:e.droppableId===t.droppableId&&e.index===t.index}function mr(e){if(!(e instanceof Element))return null;let t=e.closest(`[data-univer-dnd-droppable-id]`);return t instanceof HTMLElement?t:null}function hr(e,t,n,r){let i=Array.from(e.querySelectorAll(`[data-univer-dnd-item="true"]`)).filter(e=>{let t=e.dataset.univerDndId;return t?t!==n.draggableId||n.source.droppableId!==r:!1});if(!i.length)return 0;for(let e=0;e<i.length-1;e++){let n=i[e].getBoundingClientRect();if(t<n.top+n.height/2)return e}let a=i.length-1,o=i[a].getBoundingClientRect();return t<o.top+o.height*.35?a:i.length}function gr(){let e=(0,f.useContext)(dr);if(!e)throw Error(`Draggable/Droppable must be used inside DragDropContext`);return e}function _r(e){let{children:t,onDragEnd:n,onDragStart:r,onDragUpdate:i}=e,[a,o]=(0,f.useState)(null),s=(0,f.useRef)(null),c=(0,f.useCallback)((e,t)=>{let n={draggableId:e,source:t,destination:null};s.current=n,o(n),r==null||r({draggableId:e,source:t}),i==null||i({draggableId:e,source:t,destination:null})},[r,i]),l=(0,f.useCallback)(e=>{let t=s.current;if(!t||pr(t.destination,e))return;let n={...t,destination:e};s.current=n,o(n),i==null||i({draggableId:n.draggableId,source:n.source,destination:n.destination})},[i]),u=(0,f.useCallback)(async(e,t)=>{let r=s.current;r&&(s.current=null,o(null),await(n==null?void 0:n({draggableId:r.draggableId,source:r.source,destination:t===void 0?r.destination:t,reason:e})))},[n]);(0,f.useEffect)(()=>{if(!a)return;let e=e=>{let t=mr(e.target);t&&t.dataset.univerDndDropDisabled!==`true`||l(null)};return window.addEventListener(`dragover`,e,!0),()=>{window.removeEventListener(`dragover`,e,!0)}},[a,l]);let p=(0,f.useMemo)(()=>({activeDrag:a,startDrag:c,updateDestination:l,endDrag:u}),[a,c,l,u]);return/* @__PURE__ */ (0,d.jsx)(dr.Provider,{value:p,children:t})}function vr(e){var t;let{children:n,droppableId:r,isDropDisabled:i=!1}=e,{activeDrag:a,updateDestination:o,endDrag:s}=gr(),c=(0,f.useRef)(null),l={innerRef:(0,f.useCallback)(e=>{c.current=e},[]),droppableProps:{onDragOver:(0,f.useCallback)(e=>{if(!a||i||!c.current)return;e.preventDefault();let t=hr(c.current,e.clientY,a,r);o({droppableId:r,index:t})},[a,i,r,o]),onDrop:(0,f.useCallback)(e=>{var t;if(!a||i||!c.current)return;e.preventDefault();let n=(((t=a.destination)==null?void 0:t.droppableId)===r?a.destination:null)??{droppableId:r,index:hr(c.current,e.clientY,a,r)};return s(`DROP`,n)},[a,s,i,r]),"data-univer-dnd-droppable-id":r,"data-univer-dnd-drop-disabled":String(i)},placeholder:null},u={isDraggingOver:!!(a&&((t=a.destination)==null?void 0:t.droppableId)===r)};return/* @__PURE__ */ (0,d.jsx)(fr.Provider,{value:r,children:n(l,u)})}function yr(e){var t;let{children:n,draggableId:r,index:i,isDragDisabled:a=!1}=e,{activeDrag:o,startDrag:s,updateDestination:c,endDrag:l}=gr(),u=(0,f.useContext)(fr),d=(0,f.useRef)(null),p=(0,f.useRef)(null),m=(0,f.useRef)(!1),h=(0,f.useRef)(null),g=(0,f.useRef)(null),_=(0,f.useRef)(!1),v=(0,f.useRef)(null),y=(0,f.useRef)(null),b=(0,f.useRef)(null),x=(0,f.useCallback)(e=>{d.current=e},[]),S=(o==null?void 0:o.draggableId)===r,C=(0,f.useCallback)(()=>{g.current&&=(window.clearTimeout(g.current),null)},[]),w=(0,f.useCallback)(()=>{C(),b.current=null,h.current&&=(h.current(),null),v.current&&=(v.current.remove(),null),y.current=null,m.current=!1},[C]);(0,f.useEffect)(()=>w,[w]);let T=(0,f.useCallback)((e,t)=>{if(v.current)return;let n=d.current;if(!n)return;let r=n.getBoundingClientRect(),i=e>0?Math.max(0,e-r.left):12,a=t>0?Math.max(0,t-r.top):12,o=n.cloneNode(!0);if(!(o instanceof HTMLDivElement))return;let s=o;s.style.position=`fixed`,s.style.left=`0px`,s.style.top=`0px`,s.style.width=`${r.width}px`,s.style.height=`${r.height}px`,s.style.pointerEvents=`none`,s.style.opacity=`0.9`,s.style.zIndex=`2147483647`,s.style.boxSizing=`border-box`,s.style.transform=`translate(${Math.max(0,e-i)}px, ${Math.max(0,t-a)}px)`,document.body.appendChild(s),v.current=s,y.current={x:i,y:a}},[]),E=(0,f.useCallback)((e,t)=>{let n=v.current,r=y.current;n&&r&&(n.style.transform=`translate(${Math.max(0,e-r.x)}px, ${Math.max(0,t-r.y)}px)`)},[]),D=(0,f.useCallback)((e,t)=>{let n=mr(document.elementFromPoint(e,t));if(!n||n.dataset.univerDndDropDisabled===`true`)return null;let a=n.dataset.univerDndDroppableId;return a?{droppableId:a,index:hr(n,t,{draggableId:r,source:{droppableId:u??``,index:i},destination:null},a)}:null},[r,u,i]),O=(0,f.useCallback)((e,t,n,o)=>{if(m.current||_.current||a||!u)return;m.current=!0,C(),_.current=!1;try{var l;(l=d.current)==null||l.setPointerCapture(t)}catch{}T(n,o),s(r,{droppableId:u,index:i});let f=D(n,o);c(f);try{e.preventDefault()}catch{}},[C,r,u,i,a,T,D,s,c]),k=(0,f.useCallback)(e=>{if(a||!u||e.pointerType===`mouse`&&e.button!==0)return;_.current=!1;let t=e.pointerId;b.current={pointerId:t,pointerType:e.pointerType,startX:e.clientX,startY:e.clientY},C(),e.pointerType!==`mouse`&&(g.current=window.setTimeout(()=>{let n=b.current;n&&n.pointerId===t&&O(e.nativeEvent,n.pointerId,n.startX,n.startY)},150));let n=e=>{let t=b.current;if(!t||e.pointerId!==t.pointerId)return;if(!m.current){if(_.current){w();return}if(t.pointerType===`mouse`){let n=e.clientX-t.startX,r=e.clientY-t.startY;n*n+r*r>=16&&O(e,t.pointerId,e.clientX,e.clientY)}return}E(e.clientX,e.clientY);let n=D(e.clientX,e.clientY);c(n);try{e.preventDefault()}catch{}},r=e=>{let t=b.current;if(!t||e.pointerId!==t.pointerId)return;let n=m.current?D(e.clientX,e.clientY):null,r=m.current?l(n?`DROP`:`CANCEL`,n):Promise.resolve();return w(),r};window.addEventListener(`pointermove`,n,{capture:!0,passive:!1}),window.addEventListener(`pointerup`,r,{capture:!0}),window.addEventListener(`pointercancel`,r,{capture:!0}),h.current=()=>{window.removeEventListener(`pointermove`,n,!0),window.removeEventListener(`pointerup`,r,!0),window.removeEventListener(`pointercancel`,r,!0)}},[w,C,u,l,a,D,O,c,E]),A=(0,f.useCallback)(e=>{let t=e.dataTransfer;if(!t||typeof t.setDragImage!=`function`)return;let n=e.currentTarget,r=n.cloneNode(!0);if(!(r instanceof HTMLDivElement))return;let i=r;i.style.position=`fixed`,i.style.left=`-9999px`,i.style.top=`-9999px`,i.style.width=`${n.getBoundingClientRect().width}px`,i.style.pointerEvents=`none`,i.style.opacity=`0.95`,i.style.zIndex=`2147483647`,document.body.appendChild(i);let a=n.getBoundingClientRect(),o=e.clientX>0?Math.max(0,e.clientX-a.left):12,s=e.clientY>0?Math.max(0,e.clientY-a.top):12;t.setDragImage(i,o,s),p.current&&p.current!==i&&p.current.remove(),p.current=i},[]),j=(0,f.useCallback)(e=>{if(m.current||a||!u){e.preventDefault();return}_.current=!0,C(),b.current=null,h.current&&=(h.current(),null);let t=e.dataTransfer;if(t){try{t.effectAllowed=`move`}catch{}try{t.setData(`text/plain`,r)}catch{}}try{A(e)}catch{}s(r,{droppableId:u,index:i})},[A,C,r,u,i,a,s]),M=(0,f.useCallback)(()=>(p.current&&=(p.current.remove(),null),l(`CANCEL`,null)),[l]);return n({innerRef:x,draggableProps:{draggable:!a,onDragStart:j,onDragEnd:M,style:S?{opacity:1}:void 0,"data-univer-dnd-item":`true`,"data-univer-dnd-id":r,"data-univer-dnd-index":String(i)},dragHandleProps:{onPointerDown:k}},{isDragging:S,draggingOver:o==null||(t=o.destination)==null?void 0:t.droppableId})}function br(e,t){switch(e){case`filter`:return t.t(`sheets-pivot-ui.filter`);case`row`:return t.t(`sheets-pivot-ui.row`);case`column`:return t.t(`sheets-pivot-ui.column`);case`value`:return t.t(`sheets-pivot-ui.value`)}}function xr(e){switch(e){case`filter`:return/* @__PURE__ */ (0,d.jsx)(u.FilterIcon,{className:`univer-flex univer-flex-row univer-gap-4`});case`row`:return/* @__PURE__ */ (0,d.jsx)(u.ColumnIcon,{className:`univer-flex univer-flex-row univer-gap-4`});case`column`:return/* @__PURE__ */ (0,d.jsx)(u.RowIcon,{className:`univer-flex univer-flex-row univer-gap-4`});case`value`:return/* @__PURE__ */ (0,d.jsx)(u.FunctionIcon,{className:`univer-flex univer-flex-row univer-gap-4`})}}function Sr(e){switch(e){case`filter`:return/* @__PURE__ */ (0,d.jsxs)(`svg`,{xmlns:`http://www.w3.org/2000/svg`,width:`32`,height:`32`,viewBox:`0 0 32 32`,fill:`none`,children:[/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M4.99957 1.875C2.84556 1.875 1.67491 4.3932 3.06336 6.04001L11.5946 16.1587V26.4187C11.5946 27.4582 12.2298 28.3922 13.1966 28.7741L16.1601 29.9449C17.8221 30.6015 19.6232 29.3766 19.6232 27.5895V16.1587L28.1544 6.04C29.5428 4.3932 28.3722 1.875 26.2182 1.875H4.99957Z`,fill:`#ECECEC`}),/* @__PURE__ */ (0,d.jsx)(`rect`,{x:`21.9404`,y:`17.8544`,width:`7.59766`,height:`2.2793`,rx:`1.13965`,fill:`#274FEE`}),/* @__PURE__ */ (0,d.jsx)(`rect`,{x:`21.9404`,y:`21.9067`,width:`7.59766`,height:`2.2793`,rx:`1.13965`,fill:`#274FEE`}),/* @__PURE__ */ (0,d.jsx)(`rect`,{x:`21.9404`,y:`25.959`,width:`7.59766`,height:`2.2793`,rx:`1.13965`,fill:`#274FEE`})]});case`row`:return/* @__PURE__ */ (0,d.jsxs)(`svg`,{xmlns:`http://www.w3.org/2000/svg`,width:`32`,height:`32`,viewBox:`0 0 32 32`,fill:`none`,children:[/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M2 12.3661C2 12.0347 2.26863 11.7661 2.6 11.7661H9.24705C9.57842 11.7661 9.84705 12.0347 9.84705 12.3661V29.278C9.84705 29.6093 9.57842 29.878 9.24705 29.878H4C2.89543 29.878 2 28.9825 2 27.878V12.3661Z`,fill:`#D8D8D8`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M2 4.0863C2 2.98173 2.89543 2.0863 4 2.0863H9.24705C9.57842 2.0863 9.84705 2.35493 9.84705 2.6863V8.73981C9.84705 9.07118 9.57842 9.33981 9.24705 9.33981H2.6C2.26863 9.33981 2 9.07118 2 8.73981V4.0863Z`,fill:`#D8D8D8`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M12.2983 12.4011C12.2982 12.0697 12.5669 11.8009 12.8983 11.8009L29.3931 11.8009C29.7243 11.8009 29.9929 12.0694 29.9931 12.4007L29.9993 27.9129C29.9997 29.0178 29.1042 29.9137 27.9993 29.9137H12.9051C12.5738 29.9137 12.3053 29.6452 12.3051 29.314L12.2983 12.4011Z`,fill:`#ECECEC`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M12.2517 2.6863C12.2517 2.35493 12.5203 2.0863 12.8517 2.0863H27.9582C29.0628 2.0863 29.9582 2.98173 29.9582 4.0863V8.73627C29.9582 9.06764 29.6896 9.33627 29.3582 9.33627H12.8517C12.5203 9.33627 12.2517 9.06764 12.2517 8.73627L12.2517 2.6863Z`,fill:`#274FEE`})]});case`column`:return/* @__PURE__ */ (0,d.jsxs)(`svg`,{xmlns:`http://www.w3.org/2000/svg`,width:`32`,height:`32`,viewBox:`0 0 32 32`,fill:`none`,children:[/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M2 12.3661C2 12.0347 2.26863 11.7661 2.6 11.7661H9.24705C9.57842 11.7661 9.84705 12.0347 9.84705 12.3661V29.278C9.84705 29.6093 9.57842 29.878 9.24705 29.878H4C2.89543 29.878 2 28.9825 2 27.878V12.3661Z`,fill:`#274FEE`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M2 4.0863C2 2.98173 2.89543 2.0863 4 2.0863H9.24705C9.57842 2.0863 9.84705 2.35493 9.84705 2.6863V8.73981C9.84705 9.07118 9.57842 9.33981 9.24705 9.33981H2.6C2.26863 9.33981 2 9.07118 2 8.73981V4.0863Z`,fill:`#D8D8D8`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M12.2637 2.6863C12.2637 2.35493 12.5323 2.0863 12.8637 2.0863H27.9702C29.0748 2.0863 29.9702 2.98173 29.9702 4.0863V8.73627C29.9702 9.06764 29.7016 9.33627 29.3702 9.33627H12.8637C12.5323 9.33627 12.2637 9.06764 12.2637 8.73627L12.2637 2.6863Z`,fill:`#D8D8D8`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M12.2982 12.4011C12.2981 12.0697 12.5667 11.8009 12.8982 11.8009L29.3929 11.8009C29.7242 11.8009 29.9928 12.0694 29.9929 12.4007L29.9992 27.9129C29.9996 29.0178 29.104 29.9137 27.9992 29.9137H12.905C12.5737 29.9137 12.3051 29.6452 12.305 29.314L12.2982 12.4011Z`,fill:`#ECECEC`})]});case`value`:return/* @__PURE__ */ (0,d.jsxs)(`svg`,{xmlns:`http://www.w3.org/2000/svg`,width:`32`,height:`32`,viewBox:`0 0 32 32`,fill:`none`,children:[/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M2 12.3661C2 12.0347 2.26863 11.7661 2.6 11.7661H9.24705C9.57842 11.7661 9.84705 12.0347 9.84705 12.3661V29.278C9.84705 29.6093 9.57842 29.878 9.24705 29.878H4C2.89543 29.878 2 28.9825 2 27.878V12.3661Z`,fill:`#D8D8D8`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M2 4.0863C2 2.98173 2.89543 2.0863 4 2.0863H9.24705C9.57842 2.0863 9.84705 2.35493 9.84705 2.6863V8.73981C9.84705 9.07118 9.57842 9.33981 9.24705 9.33981H2.6C2.26863 9.33981 2 9.07118 2 8.73981V4.0863Z`,fill:`#D8D8D8`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M12.2637 2.6863C12.2637 2.35493 12.5323 2.0863 12.8637 2.0863H27.9702C29.0748 2.0863 29.9702 2.98173 29.9702 4.0863V8.73627C29.9702 9.06764 29.7016 9.33627 29.3702 9.33627H12.8637C12.5323 9.33627 12.2637 9.06764 12.2637 8.73627L12.2637 2.6863Z`,fill:`#D8D8D8`}),/* @__PURE__ */ (0,d.jsx)(`path`,{d:`M12.2982 12.4011C12.2981 12.0697 12.5667 11.8009 12.8982 11.8009L29.3929 11.8009C29.7242 11.8009 29.9928 12.0694 29.9929 12.4007L29.9992 27.9129C29.9996 29.0178 29.104 29.9137 27.9992 29.9137H12.905C12.5737 29.9137 12.3051 29.6452 12.305 29.314L12.2982 12.4011Z`,fill:`#274FEE`})]})}}function Cr(e,t){switch(e){case`filter`:return t.t(`sheets-pivot-ui.filterDesc`);case`row`:return t.t(`sheets-pivot-ui.rowDesc`);case`column`:return t.t(`sheets-pivot-ui.columnDesc`);case`value`:return t.t(`sheets-pivot-ui.valueDesc`)}}function wr(e){let t=(0,i.useDependency)(n.LocaleService),{areaType:r}=e,a=Cr(r,t),o=Sr(r);return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-h-[140px] univer-flex-col univer-items-center univer-justify-center univer-gap-1 univer-self-stretch univer-px-2`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-size-8`,children:o}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-center univer-text-xs univer-leading-4 univer-text-gray-600 dark:!univer-text-gray-200`,children:a})]})}function Tr(e){let{areaType:t,items:r,positionInfo:a,pivotTableId:o,pivotTableInfo:c}=e,l=(0,i.useDependency)(n.LocaleService);return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-min-h-[200px] univer-flex-1 univer-p-1`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-2 univer-mt-0 univer-flex univer-w-full univer-text-sm univer-leading-4 univer-text-gray-900`,children:[xr(t),/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-inline-block univer-align-top`,children:br(t,l)})]}),/* @__PURE__ */ (0,d.jsx)(vr,{droppableId:`${t}`,isCombineEnabled:!1,isDropDisabled:!1,ignoreContainerClipping:!1,direction:`vertical`,children:(e,n)=>/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-box-border univer-h-[161px] univer-w-full univer-flex-col univer-items-center univer-justify-center univer-overflow-x-hidden univer-rounded-lg univer-bg-gray-50 univer-py-2 dark:!univer-bg-gray-900`,s.borderClassName,{"univer-border-primary-600":n.isDraggingOver}),children:/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-h-full univer-overflow-y-auto`,s.scrollbarClassName),children:/* @__PURE__ */ (0,d.jsxs)(`div`,{ref:e.innerRef,className:`univer-box-border univer-h-full univer-px-2`,...e.droppableProps,children:[r.length>0&&r.map((e,n)=>/* @__PURE__ */ (0,d.jsx)(`div`,{children:/* @__PURE__ */ (0,d.jsx)(yr,{isDragDisabled:!1,draggableId:e.tableFieldId,index:n,children:(n,r)=>/* @__PURE__ */ (0,d.jsx)(d.Fragment,{children:/* @__PURE__ */ (0,d.jsx)(`div`,{ref:n.innerRef,...n.draggableProps,...n.dragHandleProps,style:n.draggableProps.style,className:(0,s.clsx)(`univer-flex univer-items-center univer-gap-1 univer-self-stretch univer-border univer-border-transparent univer-p-0.5 univer-leading-4 hover:univer-bg-gray-100 dark:hover:!univer-bg-gray-700`,r.draggingOver&&`univer-border-primary-600`),children:/* @__PURE__ */ (0,d.jsx)(Er,{pivotTableId:o,positionInfo:a,item:e,areaType:t,pivotTableInfo:c})})})},e.tableFieldId)},e.tableFieldId)),r.length===0&&/* @__PURE__ */ (0,d.jsx)(wr,{areaType:t})]})})})})]})}function Er(t){let{areaType:r,item:a,positionInfo:o,pivotTableId:c,pivotTableInfo:l}=t,{unitId:f,subUnitId:p}=o,m=a.tableFieldId===mt,h=(0,i.useDependency)(n.ICommandService),g=(0,i.useDependency)(n.LocaleService),_=()=>{if(m){let e=(l.valueFields||[]).map(e=>e.tableFieldId);wt({pivotTableId:c,unitId:f,subUnitId:p,commandService:h},e)}else wt({pivotTableId:c,unitId:f,subUnitId:p,commandService:h},[a.tableFieldId])},v=()=>{f&&p&&Dt(h,f,p,c,a.tableFieldId)},y=()=>{f&&p&&Ot(h,c,a.tableFieldId,f,p,l)},b=t=>{let n=t===`column`?e.PivotTableValuePositionEnum.Row:e.PivotTableValuePositionEnum.Column,r=t===`column`?l.rowFields.length:l.columnFields.length;Tt({pivotTableId:c,unitId:f,subUnitId:p,commandService:h},n,r)},x=/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-inline-flex univer-flex-col univer-rounded-lg univer-border univer-border-gray-200 univer-bg-gray-0 univer-p-2 univer-shadow-lg`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 dark:!univer-text-gray-0`,onClick:()=>{_()},children:g.t(`sheets-pivot-ui.removeField`)}),r!==`value`&&!m&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 dark:!univer-text-gray-0`,onClick:()=>{v()},children:g.t(`sheets-pivot-ui.filterField`)}),m&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 dark:!univer-text-gray-0`,onClick:()=>{b(r)},children:r===`column`?g.t(`sheets-pivot-ui.moveToRow`):g.t(`sheets-pivot-ui.moveToColumn`)}),r===`value`&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 dark:!univer-text-gray-0`,onClick:()=>{y()},children:g.t(`sheets-pivot-ui.fieldSettings`)})]}),S=m?g.t(`sheets-pivot-ui.multipleValue`):a.displayName,C=/* @__PURE__ */ (0,d.jsx)(`bdi`,{dir:`auto`,children:S});return/* @__PURE__ */ (0,d.jsxs)(d.Fragment,{children:[/* @__PURE__ */ (0,d.jsx)(u.GripVerticalIcon,{}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-max-w-[66px] univer-flex-1 univer-shrink-0 univer-truncate univer-text-xs univer-leading-4 univer-text-gray-800 dark:!univer-text-gray-100`,children:/* @__PURE__ */ (0,d.jsx)(s.Tooltip,{title:C,children:C})}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-cursor-pointer`,children:/* @__PURE__ */ (0,d.jsx)(s.Dropdown,{align:`end`,overlay:x,children:/* @__PURE__ */ (0,d.jsx)(`span`,{children:/* @__PURE__ */ (0,d.jsx)(u.MoreHorizontalIcon,{})})})})]})}function Dr(e){var t;let r=(0,i.useDependency)(n.IUniverInstanceService),a=(0,i.useDependency)(n.LocaleService),o=(0,i.useDependency)(n.ICommandService),s=(0,i.useDependency)(Q),{sourceRangeInfo:c,refreshHandler:l,pivotTableId:u,positionInfo:h}=e,{unitId:g,range:_,subUnitId:v}=c,y=r.getUnit(g),b=(y==null||(t=y.getSheetBySheetId(v))==null?void 0:t.getName())??``,[x,S]=(0,f.useState)(void 0),C=(0,f.useRef)(x);C.current=x;let[w,T]=(0,f.useState)(_),[E,D]=(0,f.useState)(b),[O,k]=(0,f.useState)(!1);(0,f.useEffect)(()=>{s.setIsRangeSelectorFocus(O)},[O,s]),(0,f.useEffect)(()=>{T(c.range),D(b)},[c,b]);let A=(0,f.useMemo)(()=>(0,n.debounce)(async e=>{let t=e.split(`,`).filter(e=>!!e).map(p.deserializeRangeWithSheet);if(!t[0])return;let n=t[0];if(!y||C.current)return;let r=n.sheetName||c.sheetName;T(n.range),D(r);let i={unitId:g,subUnitId:y.getSheetBySheetName(r).getSheetId(),range:{...n.range},sheetName:r};await xt(o,u,i,h.unitId,h.subUnitId),l()},300,{leading:!1,maxWait:1e3,trailing:!0}),[]);return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-4`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-2 univer-flex univer-h-5 univer-justify-between univer-text-sm univer-leading-5`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-justify univer-text-gray-900 dark:!univer-text-gray-0`,children:a.t(`sheets-pivot-ui.sourceRangeLabel`)}),/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-xs univer-text-gray-600 dark:!univer-text-gray-200`,children:a.t(`sheets-pivot-ui.sourceRangePlaceholder`)})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-relative`,children:[/* @__PURE__ */ (0,d.jsx)(m.RangeSelector,{unitId:g,subUnitId:v,initialValue:(0,p.serializeRangeWithSheet)(E,w),onChange:(e,t)=>{(0,p.serializeRangeWithSheet)(E,w)!==t&&A(t)},keepSheetReference:!0,supportAcrossSheet:!0,onVerify:(e,t)=>{let n=t.split(`,`).filter(e=>!!e);if(!n[0]){S(a.t(`sheets-pivot-ui.msg.emptySourceRange`));return}if(!e){S(a.t(`sheets-pivot-ui.msg.invalidSize`));return}let r=(0,p.deserializeRangeWithSheet)(n[0]),i=r.sheetName||c.sheetName;if(y)switch(yt(y.getSheetBySheetName(i),r.range)){case 2:S(void 0);break;case 0:S(a.t(`sheets-pivot-ui.msg.emptySourceRange`));break;case 1:S(a.t(`sheets-pivot-ui.msg.invalidSize`))}},maxRangeCount:1,onFocusChange:e=>{k(e)},onRangeSelectorDialogVisibleChange:e=>{k(!!e)}}),x?/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-my-1 univer-text-xs univer-text-red-500`,children:x}):null]})]})}function Or(t){let{type:n}=t;switch(n){case e.PivotDataFieldDataTypeEnum.text:return/* @__PURE__ */ (0,d.jsx)(u.TextIcon,{className:`univer-mx-1.5 univer-my-0 univer-h-4 univer-text-xs univer-text-gray-200 dark:!univer-text-gray-600`});case e.PivotDataFieldDataTypeEnum.number:return/* @__PURE__ */ (0,d.jsx)(u.NumberIcon,{className:`univer-mx-1.5 univer-my-0 univer-h-4 univer-text-xs univer-text-gray-200 dark:!univer-text-gray-600`});case e.PivotDataFieldDataTypeEnum.date:return/* @__PURE__ */ (0,d.jsx)(u.CalendarIcon,{className:`univer-mx-1.5 univer-my-0 univer-h-4 univer-text-xs univer-text-gray-200 dark:!univer-text-gray-600`})}}function kr(t){if(!t)return!1;if(t.type===e.PivotFilterTypeEnum.RecordFilter){var r;return t.dateRange!==void 0||(((r=t.selection)==null?void 0:r.mode)??n.FilterSelectionMode.All)!==n.FilterSelectionMode.All}return t.type!==e.PivotFilterTypeEnum.ManualFilter||t.isAll!==!0}function Ar(r){let{name:a,dataFieldId:o,checked:c,type:l,refreshHandler:p,pivotTableId:m,draggingId:h,pivotTableInfo:g,unitId:_,subUnitId:v}=r,[y,b]=(0,f.useState)(!1),x=(0,i.useDependency)(n.ICommandService),S=(0,i.useDependency)(n.LocaleService);if(!g)return null;let{rowFields:C,columnFields:w,filterFields:T,valueFields:E}=g,D=g.hiddenFields??[],O=[...C,...w,...T,...E,...D],k=D.find(e=>e.dataFieldId===o),A=async n=>{let r=!!n,i=!1;if(r){let n={pivotTableId:m,dataFieldId:o,fieldArea:l===e.PivotDataFieldDataTypeEnum.number?e.PivotTableFiledAreaEnum.Value:e.PivotTableFiledAreaEnum.Row,unitId:_,subUnitId:v,index:l===e.PivotDataFieldDataTypeEnum.number?E.length:C.length};i=await x.executeCommand(t.AddPivotFieldCommand.id,n)}else{let e=[];O.forEach(t=>{t.dataFieldId===o&&e.push(t.tableFieldId)}),i=await x.executeCommand(t.RemovePivotFieldCommand.id,{pivotTableId:m,fieldIds:e,unitId:_,subUnitId:v})}i&&p()},j=async n=>{let r=!1,i=``;if([...C,...w,...T,...D].forEach(e=>{e.dataFieldId===o&&(r=!0,i=e.tableFieldId)}),r&&n!==e.PivotTableFiledAreaEnum.Value){if(m){let e=Bt(g,n),r={pivotTableId:m,area:n,index:e,fieldId:i,unitId:_,subUnitId:v};await x.executeCommand(t.MovePivotFieldCommand.id,r)&&p()}}else if(m){let e={pivotTableId:m,dataFieldId:o,fieldArea:n,unitId:_,subUnitId:v,index:Bt(g,n)};await x.executeCommand(t.AddPivotFieldCommand.id,e)&&p()}};return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:(0,s.clsx)(`univer-group univer-relative univer-box-border univer-flex univer-h-8 univer-w-full univer-flex-row univer-items-center univer-rounded-md univer-px-1 univer-py-2 univer-leading-4 univer-transition-all hover:univer-bg-gray-100 dark:hover:!univer-bg-gray-700`,{"univer-flex univer-bg-gray-100 dark:!univer-bg-gray-700":h===o}),id:`source-${o}`,onContextMenu:e=>{e.preventDefault(),b(!0)},children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-mr-2 univer-w-4 rtl:univer-ml-2 rtl:univer-mr-0`,children:/* @__PURE__ */ (0,d.jsx)(u.GripVerticalIcon,{})}),/* @__PURE__ */ (0,d.jsx)(s.Checkbox,{className:`univer-w-[14px] univer-text-sm univer-text-gray-200`,checked:c,onChange:A}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-min-w-0 univer-flex-1 univer-items-center univer-text-xs univer-leading-4 univer-text-gray-900 dark:!univer-text-gray-0`,children:[/* @__PURE__ */ (0,d.jsx)(Or,{type:l}),/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-truncate`,children:a})]}),/* @__PURE__ */ (0,d.jsx)(s.Dropdown,{align:`end`,side:`bottom`,open:y,onOpenChange:b,overlay:/* @__PURE__ */ (0,d.jsxs)(`div`,{children:[kr(k==null?void 0:k.filterInfo)&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 hover:univer-bg-gray-100 dark:!univer-text-gray-0 dark:hover:!univer-bg-gray-700`,onClick:async()=>{k&&(b(!1),await x.executeCommand(t.SetPivotFilterCommand.id,{unitId:_,subUnitId:v,pivotTableId:m,tableFieldId:k.tableFieldId,items:[],partialItems:[],isAll:!0})&&p())},children:S.t(`sheets-pivot-ui.dialog.filter.clearFilter`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 hover:univer-bg-gray-100 dark:!univer-text-gray-0 dark:hover:!univer-bg-gray-700`,onClick:()=>{b(!1),j(e.PivotTableFiledAreaEnum.Row)},children:S.t(`sheets-pivot-ui.addToRow`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 hover:univer-bg-gray-100 dark:!univer-text-gray-0 dark:hover:!univer-bg-gray-700`,onClick:()=>{b(!1),j(e.PivotTableFiledAreaEnum.Column)},children:S.t(`sheets-pivot-ui.addToColumn`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 hover:univer-bg-gray-100 dark:!univer-text-gray-0 dark:hover:!univer-bg-gray-700`,onClick:()=>{b(!1),j(e.PivotTableFiledAreaEnum.Value)},children:S.t(`sheets-pivot-ui.addToValue`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-flex univer-cursor-pointer univer-items-center univer-self-stretch univer-px-2 univer-py-1.5 univer-text-sm univer-leading-5 univer-text-gray-900 hover:univer-bg-gray-100 dark:!univer-text-gray-0 dark:hover:!univer-bg-gray-700`,onClick:()=>{b(!1),j(e.PivotTableFiledAreaEnum.Filter)},children:S.t(`sheets-pivot-ui.addToFilter`)})]}),children:/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-ml-1 univer-flex univer-size-5 univer-cursor-pointer univer-items-center univer-justify-center univer-rounded-md univer-transition-opacity hover:univer-bg-gray-200 rtl:univer-ml-0 rtl:univer-mr-1 dark:hover:!univer-bg-gray-700`,{"univer-opacity-100":y,"univer-opacity-0 group-hover:univer-opacity-100":!y}),onMouseDown:e=>e.stopPropagation(),onClick:e=>e.stopPropagation(),children:/* @__PURE__ */ (0,d.jsx)(u.MoreDownIcon,{className:`univer-text-gray-400 dark:!univer-text-gray-500`})})})]},o)}function jr(e){let{sourceList:t,refreshHandler:r,pivotTableId:a,pivotTableInfo:o,unitId:c,draggingId:l,subUnitId:p}=e,m=(0,i.useDependency)(n.LocaleService),[h,g]=(0,f.useState)(``),_=h.trim().toLocaleLowerCase(),v=_?t.filter(e=>e.name.toLocaleLowerCase().includes(_)):t;return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-4 univer-flex univer-min-h-0 univer-flex-1 univer-flex-col`,children:[/* @__PURE__ */ (0,d.jsx)(`p`,{className:`univer-mb-1 univer-mt-0 univer-text-sm univer-font-medium univer-leading-5 univer-text-gray-900 dark:!univer-text-gray-0`,children:m.t(`sheets-pivot-ui.sourceFieldTile`)}),/* @__PURE__ */ (0,d.jsx)(`p`,{className:`univer-mb-2 univer-mt-0 univer-text-xs univer-leading-4 univer-text-gray-600 dark:!univer-text-gray-200`,children:m.t(`sheets-pivot-ui.sourceFieldDescription`)}),/* @__PURE__ */ (0,d.jsx)(s.Input,{allowClear:!0,className:`univer-mb-2`,placeholder:m.t(`sheets-pivot-ui.searchFieldPlaceholder`),slot:/* @__PURE__ */ (0,d.jsx)(u.SearchIcon,{}),value:h,onChange:g}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-box-border univer-min-h-0 univer-flex-1 univer-rounded-lg univer-bg-gray-0 univer-p-2 dark:!univer-bg-gray-900`,s.borderClassName),children:/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-relative univer-h-full univer-overflow-y-auto`,s.scrollbarClassName),"data-testid":`pivot-source-field-viewport`,children:/* @__PURE__ */ (0,d.jsx)(vr,{droppableId:`source`,direction:`vertical`,isCombineEnabled:!1,ignoreContainerClipping:!1,isDropDisabled:!0,children:e=>/* @__PURE__ */ (0,d.jsxs)(`div`,{ref:e.innerRef,className:`univer-box-border univer-h-full univer-px-2`,...e.droppableProps,children:[v.map((e,t)=>/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-relative univer-flex univer-h-8 univer-w-full univer-items-center univer-gap-1 univer-self-stretch univer-border univer-border-transparent univer-p-0.5 univer-leading-4 hover:univer-bg-gray-100 dark:hover:!univer-bg-gray-700`,children:/* @__PURE__ */ (0,d.jsx)(yr,{draggableId:e.id,isDragDisabled:!1,index:t,children:({innerRef:t,draggableProps:n,dragHandleProps:i})=>/* @__PURE__ */ (0,d.jsx)(`div`,{ref:t,...n,...i,className:`univer-h-8 univer-w-full`,style:n.style,children:/* @__PURE__ */ (0,d.jsx)(Ar,{draggingId:l,unitId:c,subUnitId:p,...e,refreshHandler:r,pivotTableId:a,pivotTableInfo:o})})},e.id)},e.id)),v.length===0&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-py-4 univer-text-center univer-text-xs univer-text-gray-500`,children:m.t(`sheets-pivot-ui.noMatchingFields`)})]})})})})]})}function Mr(e){let{pivotTableId:r,version:a,unitId:c,subUnitId:l,panelInfo:u}=e,p=(0,i.useDependency)(t.SheetsPivotTableConfigModel),m=(0,f.useMemo)(()=>p.watchPivotTableChanges(c,l,r).pipe((0,o.debounceTime)(0)),[p,c,l,r]),h=(0,i.useObservable)(m),g=(0,i.useDependency)(t.SheetsPivotRPCService),_=(0,i.useDependency)(Q),v=(0,i.useObservable)(_.pivotTablePanelVisible$,!0),y=(0,i.useDependency)(n.ICommandService),b=(0,i.useDependency)(n.ILogService),[x,S]=(0,f.useState)(u),[C,w]=(0,f.useState)(null),T=(0,f.useCallback)(async()=>{if(!p.getPivotTableConfig(c,l,r))return;let e=await g.getPivotDisplayConfig({unitId:c,subUnitId:l,pivotTableId:r,params:{type:t.PivotDisplayDataType.Panel,panel:{pivotTableId:r}}});e.result.panel&&S(e.result.panel)},[r,g,p,l,c]);(0,f.useEffect)(()=>{T().catch(e=>b.error(`[PivotPanel]`,e))},[b,h,r,T,a]);let E=async e=>{let{source:t,destination:n,draggableId:i}=e;w(null);let a={pivotTableInfo:x,pivotTableId:r,commandService:y,unitId:c,subUnitId:l},o=t.droppableId;if(n&&n.droppableId){let e=n.droppableId;await Ct(a,i,o,e,n.index),await T()}n===null&&(await bt(a,i,o),await T())},D=e=>{w({draggingId:e.draggableId})},O=e=>{let{draggableId:t,destination:n,source:r}=e;w({draggingId:t,destination:n,source:r})};if(!v||!x)return null;let{sourceList:k,rowFields:A,columnFields:j,filterFields:M,valueFields:N,sourceRange:P,positionInfo:F}=x;return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:(0,s.clsx)(`univer-mt-4 univer-flex univer-h-[calc(100%-16px)] univer-min-h-0 univer-w-full univer-flex-col univer-overflow-y-auto univer-overflow-x-hidden`,s.scrollbarClassName),children:[/* @__PURE__ */ (0,d.jsx)(Dr,{sourceRangeInfo:P,pivotTableId:r,refreshHandler:T,positionInfo:F}),/* @__PURE__ */ (0,d.jsxs)(_r,{onDragEnd:E,onDragStart:D,onDragUpdate:O,children:[/* @__PURE__ */ (0,d.jsx)(jr,{draggingId:(C==null?void 0:C.draggingId)??``,sourceList:k,refreshHandler:T,pivotTableId:r,pivotTableInfo:x,unitId:c,subUnitId:l}),/* @__PURE__ */ (0,d.jsx)(Nr,{rowFields:A,columnFields:j,filterFields:M,valueFields:N,refreshHandler:T,positionInfo:F,pivotTableId:r,pivotTableInfo:x})]})]})}function Nr(e){let{rowFields:t,columnFields:n,filterFields:r,valueFields:i,refreshHandler:a,pivotTableInfo:o,positionInfo:s,pivotTableId:c}=e;return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-shrink-0`,"data-testid":`pivot-field-areas`,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-row univer-gap-4`,children:[/* @__PURE__ */ (0,d.jsx)(Tr,{areaType:`filter`,items:r,positionInfo:s,pivotTableId:c,pivotTableInfo:o,refreshHandler:a}),/* @__PURE__ */ (0,d.jsx)(Tr,{areaType:`column`,items:n,positionInfo:s,pivotTableId:c,pivotTableInfo:o,refreshHandler:a})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-row univer-gap-4`,children:[/* @__PURE__ */ (0,d.jsx)(Tr,{areaType:`row`,items:t,positionInfo:s,pivotTableId:c,pivotTableInfo:o,refreshHandler:a}),/* @__PURE__ */ (0,d.jsx)(Tr,{areaType:`value`,items:i,positionInfo:s,pivotTableId:c,pivotTableInfo:o,refreshHandler:a})]})]})}function Pr(e){let{className:t,unitId:r,subUnitId:a,pivotTableId:o}=e,[c,l]=(0,f.useState)(0),u=(0,i.useDependency)(n.LocaleService),p=u.getDirection();return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:(0,s.clsx)(`univer-flex univer-size-full univer-min-h-0 univer-flex-col`,t),dir:p,children:[/* @__PURE__ */ (0,d.jsx)(s.Segmented,{className:`univer-my-3 univer-w-full univer-shrink-0`,items:[{label:u.t(`sheets-pivot-ui.fieldPanel`),value:0},{label:u.t(`sheets-pivot-ui.configPanel`),value:1}],value:c,onChange:l}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-min-h-0 univer-flex-1`,c===0?`univer-overflow-hidden`:`univer-overflow-auto`),children:c===0?/* @__PURE__ */ (0,d.jsx)(Mr,{...e}):/* @__PURE__ */ (0,d.jsx)(ur,{unitId:r,subUnitId:a,pivotTableId:o})})]})}function Fr(e){let{unitId:t,subUnitId:n,pivotTableId:r,tableFieldId:i,type:a}=e,o=JSON.stringify([t,n,r,i,a]);return/* @__PURE__ */ (0,d.jsx)(Ir,{...e},o)}function Ir({unitId:r,subUnitId:a,pivotTableId:o,tableFieldId:c,type:l,baseItems:u,onConfirm:p,onCancel:m}){var h;let g=(0,i.useDependency)(t.SheetsPivotTableConfigModel),_=(0,i.useDependency)(n.LocaleService),v=(0,i.useObservable)(_.direction$,_.getDirection()),y=de.find(e=>e.type===l),b=g.getPivotTableConfig(r,a,o),x=b==null||(h=b.fieldsConfig.measure[c])==null?void 0:h.showDataAs,S=()=>{var t;if((x==null?void 0:x.type)!==l||!x.baseFieldId)return``;if(x.baseItemType===e.PivotShowAsBaseItemTypeEnum.previous)return`relative:previous`;if(x.baseItemType===e.PivotShowAsBaseItemTypeEnum.next)return`relative:next`;let n=((t=u[x.baseFieldId])==null?void 0:t.items.indexOf(x.baseItem))??-1;return n<0?``:`item:${n}`},[C,w]=(0,f.useState)((x==null?void 0:x.type)===l?x.baseFieldId:``),[T,E]=(0,f.useState)(S),D=(0,f.useRef)(!1),[O,k]=(0,f.useState)(!1),A=g.getPivotTableConfig(r,a,o),j=g.getCollection(r,o),M=A==null?void 0:A.fieldsConfig.measure[c],N=!!(M&&j&&(j.fields[M.dataFieldId]||j.customFields.find(e=>e.id===M.dataFieldId))),P=(A&&j&&y?pe(A,j,y):[]).filter(e=>{let t=u[e];return(y==null?void 0:y.baseItemMode)===`none`||!!(t&&((y==null?void 0:y.baseItemMode)===`item-or-relative`||t.items.length>0))}),F=P.map(e=>({label:A.fieldsConfig.dimension[e].displayName,value:e})),I=A==null?void 0:A.fieldsConfig.dimension[C],L=I&&j&&(j.fields[I.dataFieldId]??j.customFields.find(e=>e.id===I.dataFieldId)),R=u[C],z=(R==null?void 0:R.items.map((t,r)=>{var i;let a=R.itemTypes[r],o=(i=R.itemLabels)==null?void 0:i[t],s=ht(t,a),c=t;return s?c=_.t(s):o===void 0?a===e.PivotDataFieldDataTypeEnum.other?c=_.t(`sheets-pivot-ui.showValuesAs.other`):(a===e.PivotDataFieldDataTypeEnum.date||a===e.PivotDataFieldDataTypeEnum.number)&&R.format&&(c=n.numfmt.format(R.format,Number(t))):c=o,{label:c,value:`item:${r}`,itemKey:t}}))??[];(y==null?void 0:y.baseItemMode)===`item-or-relative`&&z.push({label:_.t(`sheets-pivot-ui.showValuesAs.previous`),value:`relative:previous`,itemKey:``,itemType:e.PivotShowAsBaseItemTypeEnum.previous},{label:_.t(`sheets-pivot-ui.showValuesAs.next`),value:`relative:next`,itemKey:``,itemType:e.PivotShowAsBaseItemTypeEnum.next});let B=z.find(e=>e.value===T),V=!!(y!=null&&y.baseItemMode&&y.baseItemMode!==`none`),ee=!!(y&&A&&j&&N&&A.fieldsConfig.valueFields.includes(c)&&P.includes(C)&&L&&(!V||B));return/* @__PURE__ */ (0,d.jsxs)(`div`,{dir:v,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-4`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:_.t(`sheets-pivot-ui.showValuesAs.baseField`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-full`,value:C,options:F,onChange:e=>{w(e),E(``)}})]}),V&&/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-mb-4`,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-text-sm univer-font-medium`,children:_.t(`sheets-pivot-ui.showValuesAs.baseItem`)}),/* @__PURE__ */ (0,d.jsx)(s.Select,{className:`univer-w-full`,value:T,options:z,disabled:!C,onChange:E})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-justify-end univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:m,children:_.t(`sheets-pivot-ui.showValuesAs.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:!ee||O,onClick:async()=>{let e=g.getPivotTableConfig(r,a,o),t=g.getCollection(r,o),n=e==null?void 0:e.fieldsConfig.measure[c];if(!y||!e||!t||!n||D.current||!e.fieldsConfig.valueFields.includes(c)||!(t.fields[n.dataFieldId]||t.customFields.find(e=>e.id===n.dataFieldId))||!pe(e,t,y).includes(C)||V&&!B)return;let i={type:l,baseFieldId:C,baseItem:(B==null?void 0:B.itemKey)??``};(B==null?void 0:B.itemType)!==void 0&&(i.baseItemType=B.itemType),D.current=!0,k(!0);try{await p(i)}catch{}finally{D.current=!1,k(!1)}},children:_.t(`sheets-pivot-ui.showValuesAs.ok`)})]})]})}const Lr=e=>{let[t,n]=(0,f.useState)(!1),r=(0,i.useDependency)(Q);return(0,f.useEffect)(()=>{r.setIsRangeSelector2Focus(t)},[t,r]),/* @__PURE__ */ (0,d.jsx)(m.RangeSelector,{...e,onFocusChange:t=>{var r;(r=e.onFocusChange)==null||r.call(e,t),n(t)}})},Rr=(e,t,n,r,i,a)=>/* @__PURE__ */ (0,d.jsx)(Lr,{unitId:e,subUnitId:t,initialValue:r,onChange:(e,t)=>n(t),maxRangeCount:1,supportAcrossSheet:!0,keepSheetReference:!0}),zr=(e,t)=>{switch(e){case 1:return t.t(`sheets-pivot-ui.msg.invalidSize`);case 0:return t.t(`sheets-pivot-ui.msg.emptySourceRange`);default:return``}};function Br(e){let{unitId:r,sourceRange:a,positionRange:o,sourceSheetName:c,positionSheetName:l,onConfirm:u,onCancel:m}=e,h=(0,i.useDependency)(n.LocaleService),g=(0,i.useDependency)(n.IUniverInstanceService),_=h.getDirection(),v=g.getUnit(r),y=(0,f.useMemo)(()=>v.getActiveSheet().getSheetId(),[v]),[b,x]=(0,f.useState)(a),[S,C]=(0,f.useState)(o),[w,T]=(0,f.useState)(c),[E,D]=(0,f.useState)(l),O=(0,f.useRef)(void 0),[k,A]=(0,f.useState)(),[j,M]=(0,f.useState)(t.PositionType.New),[N,P]=(0,f.useState)(!0),[F,I]=(0,f.useState)(!0),[L,R]=(0,f.useState)(void 0);(0,f.useEffect)(()=>{let e=yt(v.getSheetBySheetName(w),b);I(e===2),R(zr(e,h))},[b,w,v,h]);let z=e=>{if((0,p.serializeRangeWithSheet)(w,b)===e)return;let t=e.split(`,`).filter(e=>!!e).map(p.deserializeRangeWithSheet);if(Array.isArray(t)&&t.length>0){let{range:e,sheetName:n}=t[0];x(e),T(n)}t[0]||R(h.t(`sheets-pivot-ui.msg.emptySourceRange`))},B=Rr(r,y,e=>{if(S&&(0,p.serializeRangeWithSheet)(E,S)===e)return;let t=e.split(`,`).filter(e=>!!e).map(p.deserializeRangeWithSheet);if(t.length===0){C(null);return}let r=t[0];C(r.range),D(r.sheetName||c),r.sheetName===c&&n.Rectangle.intersects(r.range,b)?P(!1):P(!0)},S?(0,p.serializeRangeWithSheet)(E,S):``,!1,k),V=()=>!!(b&&S&&w&&E&&j===t.PositionType.Existing&&N&&F)||!!(b&&w&&j===t.PositionType.New&&F);return(0,f.useEffect)(()=>{A(O.current)},[]),/* @__PURE__ */ (0,d.jsxs)(`div`,{ref:O,dir:_,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-h-5 univer-text-sm univer-font-medium`,children:h.t(`sheets-pivot-ui.dialog.source.sourceData`)}),Rr(r,y,z,(0,p.serializeRangeWithSheet)(w,b),!0,k),L&&/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mt-2 univer-h-5 univer-text-xs univer-text-red-400`,children:L}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-mb-2 univer-mt-5 univer-h-5 univer-text-sm univer-font-medium`,children:h.t(`sheets-pivot-ui.dialog.source.position`)}),/* @__PURE__ */ (0,d.jsxs)(s.RadioGroup,{value:j,onChange:e=>M(e),className:`univer-flex univer-flex-col univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Radio,{value:t.PositionType.New,children:h.t(`sheets-pivot-ui.dialog.source.newSheet`)}),/* @__PURE__ */ (0,d.jsx)(s.Radio,{value:t.PositionType.Existing,children:h.t(`sheets-pivot-ui.dialog.source.existingSheet`)})]}),j===t.PositionType.Existing&&/* @__PURE__ */ (0,d.jsxs)(d.Fragment,{children:[B,!N&&/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-h-5 univer-text-sm univer-text-red-400`,children:h.t(`sheets-pivot-ui.dialog.source.intersect`)})]}),/* @__PURE__ */ (0,d.jsx)(`footer`,{className:`univer-mt-5`,children:/* @__PURE__ */ (0,d.jsxs)(s.ActionRow,{className:`univer-flex univer-justify-end univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:()=>m(),children:h.t(`sheets-pivot-ui.dialog.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,onClick:()=>{V()&&(j===t.PositionType.Existing?u({sourceRange:b,sourceSheetName:w,positionRange:S,positionSheetName:E,positionType:j}):u({sourceRange:b,sourceSheetName:w,positionType:j}))},children:h.t(`sheets-pivot-ui.dialog.ok`)})]})})]})}Br.componentKey=j;const Vr=[{id:`equal`,localeKey:`sheets-pivot-ui.valueFilter.equal`,operator:e.ST_PivotFilterOperatorEnum.valueEqual},{id:`notEqual`,localeKey:`sheets-pivot-ui.valueFilter.notEqual`,operator:e.ST_PivotFilterOperatorEnum.valueNotEqual},{id:`greaterThan`,localeKey:`sheets-pivot-ui.valueFilter.greaterThan`,operator:e.ST_PivotFilterOperatorEnum.valueGreaterThan},{id:`greaterThanOrEqual`,localeKey:`sheets-pivot-ui.valueFilter.greaterThanOrEqual`,operator:e.ST_PivotFilterOperatorEnum.valueGreaterThanOrEqual},{id:`lessThan`,localeKey:`sheets-pivot-ui.valueFilter.lessThan`,operator:e.ST_PivotFilterOperatorEnum.valueLessThan},{id:`lessThanOrEqual`,localeKey:`sheets-pivot-ui.valueFilter.lessThanOrEqual`,operator:e.ST_PivotFilterOperatorEnum.valueLessThanOrEqual},{id:`between`,localeKey:`sheets-pivot-ui.valueFilter.between`,operator:e.ST_PivotFilterOperatorEnum.valueBetween,isBetween:!0},{id:`notBetween`,localeKey:`sheets-pivot-ui.valueFilter.notBetween`,operator:e.ST_PivotFilterOperatorEnum.valueNotBetween,isBetween:!0},{id:`topCount`,localeKey:`sheets-pivot-ui.valueFilter.topCount`,operator:e.ST_PivotFilterOperatorEnum.count,isCount:!0},{id:`bottomCount`,localeKey:`sheets-pivot-ui.valueFilter.bottomCount`,operator:e.ST_PivotFilterOperatorEnum.count,isBottom:!0,isCount:!0},{id:`topPercent`,localeKey:`sheets-pivot-ui.valueFilter.topPercent`,operator:e.ST_PivotFilterOperatorEnum.percent,isPercent:!0},{id:`bottomPercent`,localeKey:`sheets-pivot-ui.valueFilter.bottomPercent`,operator:e.ST_PivotFilterOperatorEnum.percent,isBottom:!0,isPercent:!0}];function Hr({targetFieldId:t,targetFieldName:r,valueFields:a,defaultValueFieldId:o,currentFilter:c,onConfirm:l,onClear:u,onCancel:p}){function m(t){var n;return(t==null?void 0:t.operator)===e.ST_PivotFilterOperatorEnum.count?t.isBottom?`bottomCount`:`topCount`:(t==null?void 0:t.operator)===e.ST_PivotFilterOperatorEnum.percent?t.isBottom?`bottomPercent`:`topPercent`:((n=Vr.find(e=>e.operator===(t==null?void 0:t.operator)))==null?void 0:n.id)??`greaterThan`}let h=(0,i.useDependency)(n.LocaleService),g=(0,i.useObservable)(h.direction$,h.getDirection()),_=c==null?void 0:c.expected,[v,y]=(0,f.useState)(()=>{let e=c==null?void 0:c.valueFieldId;return e&&a.some(t=>t.id===e)?e:o&&a.some(e=>e.id===o)?o:a.length===1?a[0].id:``}),[b,x]=(0,f.useState)(()=>m(c)),[S,C]=(0,f.useState)(()=>String(Array.isArray(_)?_[0]:_??0)),[w,T]=(0,f.useState)(()=>String(Array.isArray(_)?_[1]:0)),E=(0,f.useRef)(!1),[D,O]=(0,f.useState)(!1),k=Vr.find(e=>e.id===b),A=S.trim()!==``&&Number.isFinite(Number(S)),j=!k.isBetween||w.trim()!==``&&Number.isFinite(Number(w)),M=!k.isCount||Number.isInteger(Number(S))&&Number(S)>=1,N=!k.isPercent||Number(S)>=0&&Number(S)<=100,P=!!(a.some(e=>e.id===v)&&A&&j&&M&&N),F=async e=>{if(!E.current){E.current=!0,O(!0);try{await e()}catch{}finally{E.current=!1,O(!1)}}},I=()=>F(async()=>{if(!P)return;let e={operator:k.operator,expected:k.isBetween?[Number(S),Number(w)]:Number(S),valueFieldId:v};k.isBottom&&(e.isBottom=!0),await l(e)}),L=()=>F(u);return/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-flex-col univer-gap-4`,"data-target-field-id":t,dir:g,children:[/* @__PURE__ */ (0,d.jsx)(`div`,{className:`univer-text-sm univer-font-medium`,children:/* @__PURE__ */ (0,d.jsx)(`bdi`,{dir:`auto`,children:r})}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-flex-col univer-gap-2 univer-text-sm`,children:[h.t(`sheets-pivot-ui.valueFilter.valueField`),/* @__PURE__ */ (0,d.jsx)(s.Select,{"aria-label":h.t(`sheets-pivot-ui.valueFilter.valueField`),"data-testid":`pivot-value-filter-value-field`,value:v,options:[{value:``,label:h.t(`sheets-pivot-ui.valueFilter.selectValueField`)},...a.map(e=>({value:e.id,label:e.label}))],onChange:y})]}),/* @__PURE__ */ (0,d.jsxs)(`label`,{className:`univer-flex univer-flex-col univer-gap-2 univer-text-sm`,children:[h.t(`sheets-pivot-ui.valueFilter.condition`),/* @__PURE__ */ (0,d.jsx)(s.Select,{"aria-label":h.t(`sheets-pivot-ui.valueFilter.condition`),"data-testid":`pivot-value-filter-rule`,value:b,options:Vr.map(e=>({value:e.id,label:h.t(e.localeKey)})),onChange:e=>x(e)})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-gap-2`,children:[/* @__PURE__ */ (0,d.jsx)(s.Input,{"aria-label":h.t(`sheets-pivot-ui.valueFilter.condition`),"aria-invalid":!A||!M||!N,className:`univer-min-w-0 univer-flex-1`,"data-testid":`pivot-value-filter-expected`,inputMode:`decimal`,value:S,onChange:C}),k.isBetween&&/* @__PURE__ */ (0,d.jsx)(s.Input,{"aria-label":h.t(`sheets-pivot-ui.valueFilter.condition`),"aria-invalid":!j,className:`univer-min-w-0 univer-flex-1`,"data-testid":`pivot-value-filter-expected`,inputMode:`decimal`,value:w,onChange:T})]}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-justify-between univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{"data-testid":`pivot-value-filter-clear`,disabled:D,onClick:L,children:h.t(`sheets-pivot-ui.valueFilter.clear`)}),/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-gap-3`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{disabled:D,onClick:p,children:h.t(`sheets-pivot-ui.valueFilter.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{"data-testid":`pivot-value-filter-confirm`,variant:`primary`,disabled:!P||D,onClick:I,children:h.t(`sheets-pivot-ui.valueFilter.ok`)})]})]})]})}function Ur(e){let{pivotTableId:r,tableFieldId:a,closeFn:o,unitId:c,subUnitId:l,format:p,tableFieldInfo:m,dataFieldInfo:h,version:g}=e,[_,v]=(0,f.useState)(m.displayName),[y,b]=(0,f.useState)(m.subTotalType),x=(0,i.useDependency)(n.LocaleService),S=(0,i.useDependency)(n.ICommandService),T=(0,i.useDependency)(i.IDialogService),E={...(0,i.useDependency)(t.SheetsPivotTableConfigModel).getTextInfo()};for(let e of[`sheets-pivot-ui.averageName`,`sheets-pivot-ui.countName`,`sheets-pivot-ui.countNumsName`,`sheets-pivot-ui.maxName`,`sheets-pivot-ui.minName`,`sheets-pivot-ui.productName`,`sheets-pivot-ui.stdDevName`,`sheets-pivot-ui.stdDevpName`,`sheets-pivot-ui.sumName`,`sheets-pivot-ui.varName`,`sheets-pivot-ui.varpName`])E[e]=x.t(e);if((0,f.useEffect)(()=>{v(m.displayName),b(m.subTotalType)},[g]),!m||!h)return;let D=h.name,O=x.getDirection(),k=p??m.format,A=kt.map(e=>({value:`${e.type}`,type:e.type,label:x.t(e.textKey)})),j=e=>{let t=e;if(Array.isArray(e)&&(t=e[0]),t!==void 0){let e=Number(t);b(e),Vt(D,_,E)&&v(Ht(D,E,e))}};return/* @__PURE__ */ (0,d.jsxs)(`div`,{dir:O,children:[/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-justify-between univer-self-stretch univer-leading-5`,children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-sm univer-font-medium univer-leading-5 univer-text-gray-900`,children:x.t(`sheets-pivot-ui.dialog.fieldName`)}),/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-text-xs univer-leading-4 univer-text-gray-600`,children:gt(x.t(`sheets-pivot-ui.dialog.sourceName`),D)})]}),/* @__PURE__ */ (0,d.jsx)(`div`,{children:/* @__PURE__ */ (0,d.jsx)(s.Input,{className:`univer-mb-5 univer-mt-2 univer-box-border univer-h-8 univer-w-full univer-rounded-md univer-py-1.5 univer-text-sm univer-leading-5`,value:_,onChange:e=>v(e)})}),/* @__PURE__ */ (0,d.jsx)(`label`,{className:`univer-mb-2 univer-text-sm univer-font-medium univer-text-gray-900 dark:!univer-text-gray-0`,children:x.t(`sheets-pivot-ui.dialog.summarizeBy`)}),/* @__PURE__ */ (0,d.jsx)(`div`,{className:(0,s.clsx)(`univer-h-40 univer-flex-1 univer-overflow-y-auto univer-rounded-lg`,s.borderClassName,s.scrollbarClassName),children:A.map(e=>/* @__PURE__ */ (0,d.jsxs)(`div`,{className:`univer-flex univer-items-center univer-gap-1.5 univer-self-stretch univer-rounded-md univer-px-2 univer-py-1.5 univer-text-sm univer-capitalize univer-leading-5`,onClick:()=>j(e.value),children:[/* @__PURE__ */ (0,d.jsx)(`span`,{className:`univer-w-4 univer-text-base univer-text-primary-600`,children:/* @__PURE__ */ (0,d.jsx)(u.CheckMarkIcon,{className:(0,s.clsx)({"univer-hidden":y!==e.type})})}),/* @__PURE__ */ (0,d.jsx)(`span`,{children:e.label})]},e.type))}),/* @__PURE__ */ (0,d.jsx)(`footer`,{className:`univer-mt-5`,children:/* @__PURE__ */ (0,d.jsxs)(s.ActionRow,{className:`univer-flex univer-items-center univer-justify-between univer-self-stretch`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{className:`univer-flex univer-h-8 univer-cursor-pointer univer-items-center univer-justify-center univer-gap-1.5 univer-rounded univer-border-none univer-bg-gray-0 univer-px-3 univer-text-primary-600`,onClick:()=>{let e={id:C,draggable:!0,mask:!1,maskClosable:!1,width:400,children:{label:w,unitId:c,subUnitId:l,pivotTableId:r,tableFieldId:a,originPattern:k,tableFieldInfo:m,dataFieldInfo:h},dialogStyles:{header:{padding:`20px 24px 0`,height:0},body:{padding:`0 24px 20px`}},closable:!1,onClose:()=>{T.close(C)}};T.open(e)},children:x.t(`sheets-pivot-ui.dialog.format`)}),/* @__PURE__ */ (0,d.jsxs)(s.ActionRow,{className:`univer-flex univer-flex-1 univer-gap-2 univer-text-sm rtl:univer-flex-row-reverse`,children:[/* @__PURE__ */ (0,d.jsx)(s.Button,{onClick:()=>{o&&o()},children:x.t(`sheets-pivot-ui.dialog.cancel`)}),/* @__PURE__ */ (0,d.jsx)(s.Button,{variant:`primary`,disabled:_===``,onClick:()=>{let e=m.displayName,n=m.subTotalType,i=m.format,s=e===_?void 0:_,u=n===y?void 0:y,d=i===k?void 0:k;if(e!==_||n!==y||i!==d){let e={displayName:s,subtotalType:u,format:d,tableFieldId:a,pivotTableId:r,unitId:c,subUnitId:l,tableFieldInfo:m,dataFieldInfo:h};S.executeCommand(t.UpdatePivotFieldSettingCommand.id,e)}o&&o()},children:x.t(`sheets-pivot-ui.dialog.ok`)})]})]})})]})}let Wr=class extends n.Disposable{constructor(e,t){super(),this._componentManager=e,this._iconManager=t,this._registerComponents(),this._registerIcons()}_registerComponents(){[[j,Br],[v,Pr],[b,Yn],[S,Ur],[w,ir],[E,Fr],[D,Hr],[O,Hn],[k,Qn],[A,Xn]].forEach(([e,t])=>{this.disposeWithMe(this._componentManager.register(e,t))})}_registerIcons(){this.disposeWithMe(this._iconManager.register({PivotTableIcon:u.PivotTableIcon}))}};Wr=q([K(0,(0,n.Inject)(i.ComponentManager)),K(1,(0,n.Inject)(i.IconManager))],Wr);let Gr=class extends n.Disposable{constructor(e){super(),this._menuManagerService=e,this._initMenu()}_initMenu(){this._menuManagerService.mergeMenu(Qe)}};Gr=q([K(0,i.IMenuManagerService)],Gr);let Kr=class extends n.Disposable{constructor(e,t,n,r,i,a,o,s,c,l,u){super(),this._context=e,this._injector=t,this._sheetsPivotTableAdaptorModel=n,this._sheetSkeletonManagerService=r,this._sheetsPivotTableConfigModel=i,this._commandService=a,this._sheetPermissionCheckController=o,this._permissionService=s,this._localeService=c,this._selectionsService=l,this._cellAlerts=u,G(this,`_pivotButtonShapesArr`,[]),this._initButtonRender(),this._initSkeleton(),this._initListener(),this._initBlockedPivotAlert()}_initBlockedPivotAlert(){let e=`SHEET_PIVOT_BLOCKED:${this._context.unitId}`;this.disposeWithMe((0,o.merge)(this._selectionsService.selectionChanged$,this._context.unit.activeSheet$,this._sheetsPivotTableAdaptorModel.viewUpdate$).pipe((0,o.auditTime)(0)).subscribe(()=>{var t,n,r;let{unitId:i,unit:o}=this._context,s=this._selectionsService.currentSelectionParam,c=(t=o.getActiveSheet())==null?void 0:t.getSheetId(),l=(n=this._selectionsService.getCurrentSelections())==null||(n=n.slice(-1)[0])==null?void 0:n.primary;if(!c||!l||(s==null?void 0:s.unitId)!==i||s.sheetId!==c){this._cellAlerts.removeAlert(e);return}let{actualRow:u,actualColumn:d}=l,f=this._sheetsPivotTableAdaptorModel,m=f.getPivotTableIdByCell(i,c,u,d),h=m&&((r=f.getPivotItemRenderInfoCache(i,c,m))==null?void 0:r.blockedRange);if(!h){this._cellAlerts.removeAlert(e);return}let g=`sheets-pivot-ui.perform.blocked`;h.reason===`pivot`?g=`sheets-pivot-ui.perform.blockedByPivot`:h.reason===`merge`&&(g=`sheets-pivot-ui.perform.blockedByMerge`),this._cellAlerts.showAlert({key:e,type:a.CellAlertType.ERROR,title:this._localeService.t(`sheets-pivot-ui.perform.tips`),message:this._localeService.t(g,(0,p.serializeRange)(h)),location:{unitId:i,subUnitId:c,row:u,col:d},width:260,height:96})})),this.disposeWithMe(()=>this._cellAlerts.removeAlert(e))}_initButtonRender(){this.disposeWithMe((0,o.merge)(this._sheetsPivotTableAdaptorModel.viewUpdate$.pipe((0,o.auditTime)(0)),this._sheetSkeletonManagerService.currentSkeleton$,this._sheetsPivotTableConfigModel.pivotConfigChange$,this._permissionService.permissionPointUpdate$.pipe((0,o.auditTime)(100))).subscribe(()=>{if(!this._context.unit)return;let e=this._context.unitId,t=this._context.unit.getActiveSheet().getSheetId(),{scene:n}=this._context,i=this._localeService.getDirection(),o=this._sheetSkeletonManagerService.getCurrentSkeleton();if(!o)return;let s=this._pivotButtonShapesArr;s.forEach(e=>e.dispose()),s.splice(0,s.length),this._sheetsPivotTableAdaptorModel.getSubUnitPivotMatrix(e,t).forValue((c,l,u)=>{if(u!=null&&u.pbt){let d=`sheets-pivot-button-${c}-${l}-${e}-${t}`,{startX:f,startY:p,endX:m,endY:h}=(0,a.getCoordByCell)(c,l,n,o),g=m-f,_=h-p;if(_<8||g<8||this._sheetPermissionCheckController.permissionCheckWithRanges({workbookTypes:[r.WorkbookEditablePermission],worksheetTypes:[r.WorksheetViewPermission,r.WorksheetEditPermission,r.WorksheetPivotTablePermission],rangeTypes:[r.RangeProtectionPermissionViewPoint,r.RangeProtectionPermissionEditPoint]},[{startRow:c,endRow:c,startColumn:l,endColumn:l}],e,t)===!1)return;let v;if(Tn.has(u.pbt)){let{left:n,top:r}=kn(i,`filter`,{startX:f,startY:p,endX:m,endY:h});v={left:n,top:r,height:16,width:16,zIndex:5e3,cellWidth:g,cellHeight:_,pivotParams:{col:l,row:c,unitId:e,subUnitId:t,buttonType:u.pbt}}}else if(En.has(u.pbt)){let{left:n,top:r}=kn(i,`collapse`,{startX:f,startY:p,endX:m,endY:h});v={left:n,top:r,height:16,width:16,zIndex:5e3,cellWidth:g,cellHeight:_,pivotParams:{col:l,row:c,unitId:e,subUnitId:t,buttonType:u.pbt}}}else return;let y=this._injector.createInstance(Dn,d,v);s.push(y)}}),s.length&&n.addObjects(s),n.makeDirty()}))}_initSkeleton(){var e;this.disposeWithMe(this._sheetsPivotTableAdaptorModel.markDirty$.subscribe(({unitId:e,subUnitId:t})=>{if(e===this._context.unitId&&(this._sheetSkeletonManagerService.makeDirty({sheetId:t}),this._context.unit.getActiveSheet().getSheetId()===t)){var n;this._sheetSkeletonManagerService.reCalculate(),(n=this._context.mainComponent)==null||n.makeDirty()}}));let t=this._context.unitId;this._context.unit.getSheets().forEach(e=>{var n;let r=e.getSheetId();(n=this._sheetsPivotTableAdaptorModel.getSubUnitPivotRenderCache(t,r))!=null&&n.size&&this._sheetSkeletonManagerService.makeDirty({sheetId:r})}),this._sheetSkeletonManagerService.reCalculate(),(e=this._context.mainComponent)==null||e.makeDirty()}_initListener(){let e=new n.DisposableCollection,{spreadsheet:r}=(0,a.getSheetObject)(this._context.unit,this._context);e.add(r.onDblclick$.subscribeEvent(e=>{if(e.button===2)return;let n=this._sheetSkeletonManagerService.getCurrentSkeleton();if(n==null)return;let r=this._context.scene,{row:i,column:o}=(0,a.getCoordByOffset)(e.offsetX,e.offsetY,r,n);this._commandService.executeCommand(t.PivotDrillDownCommand.id,{row:i,col:o})}))}};Kr=q([K(1,(0,n.Inject)(n.Injector)),K(2,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),K(3,(0,n.Inject)(a.SheetSkeletonManagerService)),K(4,(0,n.Inject)(t.SheetsPivotTableConfigModel)),K(5,(0,n.Inject)(n.ICommandService)),K(6,(0,n.Inject)(r.SheetPermissionCheckController)),K(7,(0,n.Inject)(n.IPermissionService)),K(8,(0,n.Inject)(n.LocaleService)),K(9,(0,n.Inject)(r.SheetsSelectionsService)),K(10,(0,n.Inject)(a.CellAlertManagerService))],Kr);var qr=class e extends l.Shape{constructor(e){super(`SHEET_PIVOT_MOVE_KEY`),G(this,`_moveType`,void 0),this._moveType=e}_draw(t){this.width!==0&&this.height!==0&&e.drawWith(t,this)}static drawWith(e,t){let n=t._moveType;if(e.save(),e.strokeStyle=`#8bc34a`,e.lineWidth=2,n===`row`){let n=t.width;if(!n)return;e.beginPath(),e.moveTo(0,0),e.lineTo(0+n,0),e.moveTo(0,-10),e.lineTo(0,10),e.moveTo(0+n,-10),e.lineTo(0+n,10)}else{let n=t.height;if(!n)return;e.moveTo(0,0),e.lineTo(0,0+n),e.moveTo(-10,0),e.lineTo(10,0),e.moveTo(-10,0+n),e.lineTo(10,0+n)}e.stroke(),e.restore()}};let Jr=class extends n.Disposable{constructor(e,t,n,r,i,a){super(),this._context=e,this._sheetsPivotTableAdaptorModel=t,this._sheetSkeletonManagerService=n,this._commandService=r,this._selectionRenderService=i,this._sheetsSelectionsService=a,G(this,`_pivotSelectionMoveShape`,void 0),G(this,`_scenePointerMoveSubscribe`,void 0),G(this,`_scenePointerUpSubscribe`,void 0),G(this,`_selectionControl`,void 0),G(this,`_moveStartRange`,void 0),G(this,`_moveStartCellIndex`,void 0),G(this,`_moveRowOrCol`,`row`),G(this,`_targetRange`,void 0),G(this,`_movePivotId`,void 0),this._initControl()}_controlMoving(e,t){let n=this._context.scene,r=n.getScrollXYInfoByViewport(l.Vector2.FromArray([e,t])),{scaleX:i,scaleY:a}=n.getAncestorScale(),o=this._sheetSkeletonManagerService.getCurrentSkeleton();if(!o||!this._moveStartRange||!this._moveStartCellIndex)return;let{row:s,column:c}=o.getCellIndexByOffset(e,t,i,a,r);if(this._moveRowOrCol===`row`){var u;let e=0,t=0,n=this._moveStartRange.endColumn-this._moveStartRange.startColumn+1;if(this._moveStartCellIndex.column===this._moveStartRange.startColumn)e=c,t=c+n-1;else if(this._moveStartCellIndex.column===this._moveStartRange.endColumn)t=c,e=c-n+1;else{let r=this._moveStartCellIndex.column-this._moveStartRange.startColumn;e=c-r,t=c-r+n-1}let r=o.getCellWithCoordByIndex(s,e),i=o.getCellWithCoordByIndex(s,t),{startX:a,endY:l}=r,{endX:d}=i;this._targetRange={startRow:s,endRow:s,startColumn:e,endColumn:t},(u=this._pivotSelectionMoveShape)==null||u.transformByState({left:a,top:l,width:d-a,height:20})}else{var d;let e=0,t=0,n=this._moveStartRange.endRow-this._moveStartRange.startRow+1;if(this._moveStartCellIndex.row===this._moveStartRange.startRow)e=s,t=s+n-1;else if(this._moveStartCellIndex.row===this._moveStartRange.endRow)t=s,e=s-n+1;else{let r=this._moveStartCellIndex.row-this._moveStartRange.startRow;e=s-r,t=s-r+n-1}let r=o.getCellWithCoordByIndex(e,c),i=o.getCellWithCoordByIndex(t,c),{startX:a,startY:l}=r,{endY:u}=i;this._targetRange={startColumn:c,endColumn:c,startRow:e,endRow:t},(d=this._pivotSelectionMoveShape)==null||d.transformByState({left:a,top:l,width:20,height:u-l})}}_controlPointerDownHandler(e){var t,r,i;(t=this._selectionControl)==null||t.setControlExtensionDisable(!1),this._context.scene.setCursor(l.CURSOR_TYPE.DEFAULT);let a=this._sheetsSelectionsService.getCurrentSelections();if(!a.length||a.length>1)return;let o=a[0].range,s=this._context.unit,c=s.getActiveSheet(),u=s.getUnitId(),d=c.getSheetId(),f=this._sheetsPivotTableAdaptorModel.getSubUnitPivotRenderCache(u,d);if(!(f!=null&&f.size))return;let p=Array.from(f).find(([e,t])=>{let{rangesInfo:r}=t;if(!r)return!1;let{rowRanges:i,colRanges:a}=r,s=i[0],c=a[0],l=!1;return s&&(l=n.Rectangle.realContain(s,o),this._moveRowOrCol=`row`),!l&&c&&(l=n.Rectangle.realContain(c,o),this._moveRowOrCol=`col`),l});if(!p)return;this._movePivotId=p[0],this._pivotSelectionMoveShape||(this._pivotSelectionMoveShape=new qr(this._moveRowOrCol),this._context.scene.addObject(this._pivotSelectionMoveShape));let m=this._context.scene,h=this._sheetSkeletonManagerService.getCurrentSkeleton();if(!h)return;let{offsetX:g,offsetY:_}=e,v=m.getCoordRelativeToViewport(l.Vector2.FromArray([g,_])),{x:y,y:b}=v,x=m.getScrollXYInfoByViewport(v),{scaleX:S,scaleY:C}=m.getAncestorScale(),{row:w,column:T}=h.getCellIndexByOffset(y,b,S,C,x),E=(r=this._selectionControl)==null?void 0:r.model.getRange();if(this._moveStartRange=E,!E)return;let D=0;D=w<E.startRow?E.startRow:w>E.endRow?E.endRow:w;let O=0;O=T<E.startColumn?E.startColumn:T>E.endColumn?E.endColumn:T,this._moveStartCellIndex={row:D,column:O},(i=this._selectionControl)==null||i.setControlExtensionDisable(!0),this._scenePointerMoveSubscribe=m.onPointerMove$.subscribeEvent(e=>{let{offsetX:t,offsetY:n}=e,{x:r,y:i}=m.getCoordRelativeToViewport(l.Vector2.FromArray([t,n]));this._controlMoving(r,i),m.setCursor(l.CURSOR_TYPE.MOVE)}),this._scenePointerUpSubscribe=m.onPointerUp$.subscribeEvent(e=>{var t,n,r;(t=this._scenePointerMoveSubscribe)==null||t.unsubscribe(),this._scenePointerMoveSubscribe=null,(n=this._selectionControl)==null||n.setControlExtensionDisable(!1),this._selectionControl=null,m.setCursor(l.CURSOR_TYPE.DEFAULT),m.removeObject(this._pivotSelectionMoveShape),this._pivotSelectionMoveShape=null,this._updatePivotSort(),(r=this._scenePointerUpSubscribe)==null||r.unsubscribe(),this._scenePointerUpSubscribe=null})}_updatePivotSort(){if(!this._moveStartRange||!this._targetRange||!this._movePivotId)return;let n=this._context.unit.getUnitId(),r=this._context.unit.getActiveSheet().getSheetId();if(this._moveRowOrCol===`row`){var i;let o=(i=this._sheetsPivotTableAdaptorModel.getSubUnitPivotRenderCache(n,r))==null?void 0:i.get(this._movePivotId);if(!o)return;let s=this._sheetsPivotTableAdaptorModel.getSubUnitPivotMatrix(n,r);if(!s)return;let c=[],l=``,{headerMap:u,rangesInfo:d}=o;for(let t=this._moveStartRange.startColumn;t<=this._moveStartRange.endColumn;t++){var a;let n=u==null||(a=u.rowHeaderMap)==null?void 0:a[t];if(!n)continue;l=n.tableFieldId;let r=d==null?void 0:d.rowRanges,i=r==null?void 0:r[0];if(!i)continue;let o={...i,startColumn:t,endColumn:t};for(let n=o.startRow;n<=o.endRow;n++){let r=s.getValue(n,t);if(!r)continue;let{v:i,pbt:a}=r;i&&a!==e.PivotCellStyleTypeEnum.Subtotal&&a!==e.PivotCellStyleTypeEnum.GrandTotal&&(typeof i==`string`?c[n]=i:typeof i==`number`&&(c[n]=i.toString()))}break}if(!l)return;let f=this._moveStartRange.startRow,p=this._moveStartRange.endRow-this._moveStartRange.startRow+1,m=this._targetRange.startRow;if(f<m){let e=c.splice(f,p);c.splice(m-p,0,...e)}else{let e=c.splice(f,p);c.splice(m,0,...e)}let h=c.filter(e=>e!==void 0);this._commandService.executeCommand(t.SetPivotSortCommand.id,{unitId:n,subUnitId:r,pivotTableId:this._movePivotId,tableFieldId:l,info:{type:e.PivotDataFieldSortOperatorEnum.custom,customOrder:h}})}else{var o;let i=(o=this._sheetsPivotTableAdaptorModel.getSubUnitPivotRenderCache(n,r))==null?void 0:o.get(this._movePivotId);if(!i)return;let a=this._sheetsPivotTableAdaptorModel.getSubUnitPivotMatrix(n,r);if(!a)return;let c=[],l=``,{headerMap:u,rangesInfo:d}=i;for(let n=this._moveStartRange.startRow;n<=this._moveStartRange.endRow;n++){var s;let r=u==null||(s=u.colHeaderMap)==null?void 0:s[n];if(!r)continue;l=r.tableFieldId;let o=d==null?void 0:d.colRanges,f=o==null?void 0:o[0];if(!f)continue;let p={...f,startRow:n,endRow:n};for(let r=p.startColumn;r<=p.endColumn;r++){let o=a.getValue(n,r);if(!o)continue;let{v:s,pbt:l}=o;if(s&&l!==e.PivotCellStyleTypeEnum.Subtotal&&l!==e.PivotCellStyleTypeEnum.GrandTotal){if(typeof s==`string`){let e=i.projection?(0,t.mapPivotSheetColumn)(i.projection,r):r;c[e]=s}else if(typeof s==`number`){let e=i.projection?(0,t.mapPivotSheetColumn)(i.projection,r):r;c[e]=s.toString()}}}break}if(!l)return;let f=i.projection?(0,t.mapPivotSheetRange)(i.projection,this._moveStartRange):this._moveStartRange,p=i.projection?(0,t.mapPivotSheetRange)(i.projection,this._targetRange):this._targetRange,m=f.startColumn,h=this._moveStartRange.endColumn-this._moveStartRange.startColumn+1,g=p.startColumn;if(m<g){let e=c.splice(m,h);c.splice(g-h,0,...e)}else{let e=c.splice(m,h);c.splice(g,0,...e)}let _=c.filter(e=>e!==void 0);this._commandService.executeCommand(t.SetPivotSortCommand.id,{unitId:n,subUnitId:r,pivotTableId:this._movePivotId,tableFieldId:l,info:{type:e.PivotDataFieldSortOperatorEnum.custom,customOrder:_}})}}_initControl(){this.disposeWithMe(this._sheetsSelectionsService.selectionMoveEnd$.subscribe(()=>{let e=this._selectionRenderService.getSelectionControls();if(this._selectionControl=e[e.length-1],this._selectionControl&&this._selectionControl.setControlExtensionDisable(!1),!this._selectionControl)return;let{leftControl:t,rightControl:n,topControl:r,bottomControl:i}=this._selectionControl;[t,n,r,i].forEach(e=>{e.onPointerDown$.subscribeEvent(this._controlPointerDownHandler.bind(this))})}))}dispose(){var e,t;(e=this._scenePointerMoveSubscribe)==null||e.unsubscribe(),this._scenePointerMoveSubscribe=null,(t=this._scenePointerUpSubscribe)==null||t.unsubscribe(),this._scenePointerUpSubscribe=null,this._selectionControl=null,this._moveStartRange=null,this._moveStartCellIndex=null,this._targetRange=null,super.dispose()}};Jr=q([K(1,(0,n.Inject)(t.SheetsPivotTableAdaptorModel)),K(2,(0,n.Inject)(a.SheetSkeletonManagerService)),K(3,(0,n.Inject)(n.ICommandService)),K(4,a.ISheetSelectionRenderService),K(5,(0,n.Inject)(r.SheetsSelectionsService))],Jr);let $=class extends n.Plugin{constructor(e=ot,t,r,i,a){super(),this._config=e,this._injector=t,this._commandService=r,this._renderManagerService=i,this._configService=a;let{menu:o,...s}=(0,n.merge)({},ot,this._config);o&&this._configService.setConfig(`menu`,o,{merge:!0}),this._configService.setConfig(at,s),this._initRegisterCommand()}onStarting(){this._injector.add([Wr]),this._injector.get(Wr),[[Gr],[Rn],[Nn],[Q],[Ln],[Fn],[In],[J],[jn]].forEach(e=>this._injector.add(e)),this._injector.get(J)}onReady(){this._registerRenderModules(),this._injector.get(Gr)}onRendered(){this._injector.get(Nn),this._injector.get(Q),this._injector.get(Ln),this._injector.get(Fn),this._injector.get(In),this._injector.get(jn)}onSteady(){this._injector.get(Rn)}_registerRenderModules(){[[Kr],[Jr]].forEach(e=>{this.disposeWithMe(this._renderManagerService.registerRenderModule(n.UniverInstanceType.UNIVER_SHEET,e))})}_initRegisterCommand(){[it,P,tt,nt,I,B,ie,ae,oe,ue,fe,xe].forEach(e=>this._commandService.registerCommand(e))}};G($,`pluginName`,`${t.SHEET_PIVOT_TABLE_PLUGIN}_UI_PLUGIN`),G($,`packageName`,$e),G($,`version`,et),G($,`type`,n.UniverInstanceType.UNIVER_SHEET),$=q([(0,n.DependentOn)(c.UniverLicensePlugin,l.UniverRenderEnginePlugin,r.UniverSheetsPlugin,t.UniverSheetsPivotTablePlugin,a.UniverSheetsUIPlugin),K(1,(0,n.Inject)(n.Injector)),K(2,(0,n.Inject)(n.ICommandService)),K(3,l.IRenderManagerService),K(4,n.IConfigService)],$),exports.DragDropContext=_r,exports.Draggable=yr,exports.Droppable=vr,exports.OpenPivotTableRangeSelectorPanelOperation=P,exports.PivotFilterOptions=ar,exports.PivotFilterPanel=Yn,exports.SheetsPivotTableUIMenuSchema=Qe,Object.defineProperty(exports,"UniverSheetsPivotTableMobileUIPlugin",{enumerable:!0,get:function(){return Z}}),Object.defineProperty(exports,"UniverSheetsPivotTableUIPlugin",{enumerable:!0,get:function(){return $}});